How to parse JSON string containing “NaN” in Node.js

后端 未结 4 1407
盖世英雄少女心
盖世英雄少女心 2020-12-17 08:24

Have a node.js app that is receiving JSON data strings that contain the literal NaN, like

 \"[1, 2, 3, NaN, 5, 6]\"

This crashes JSON

4条回答
  •  -上瘾入骨i
    2020-12-17 09:17

    When you deal with about anything mathematical or with industry data, NaN is terribly convenient (and often infinities too are). And it's an industry standard since IEEE754.

    That's obviously why some libraries, notably GSON, let you include them in the JSON they produce, losing standard purity and gaining sanity.

    Revival and regex solutions aren't reliably usable in a real project when you exchange complex dynamic objects.

    And eval has problems too, one of them being the fact it's prone to crash on IE when the JSON string is big, another one being security risks.

    That's why I wrote a specific parser (used in production) : JSON.parseMore

提交回复
热议问题