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

后端 未结 4 1405
盖世英雄少女心
盖世英雄少女心 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 09:01

    The correct solution is to recompile the parser, and contribute an "allowNan" boolean flag to the source base. This is the solution other libraries have (python's comes to mind).

    Good JSON libraries will permissively parse just about anything vaguely resembling JSON with the right flags set (perl's JSON.pm is notably flexible)... but when writing a message they produce standard JSON.

    IE: leave the room cleaner than you found it.

提交回复
热议问题