【发布时间】:2019-01-19 23:07:25
【问题描述】:
基于属性的测试发现了很多错误。在用 PostgreSQL 修复一个错误后,它最终表现为一个 JavaScript 错误。这是在 Chrome 中测试的:
JSON.stringify("wee"); // "'wee'"
JSON.stringify("we\0e"); // ""we\u0000e""
JSON.parse("'we\u0000e'"); // Uncaught SyntaxError: Unexpected token in JSON at position 0
JSON.parse('"we\u0000e"'); // Uncaught SyntaxError: Unexpected token in JSON at position 3
JSON.parse("\"we\u0000e\""); // Uncaught SyntaxError: Unexpected token in JSON at position 3
JSON.parse(JSON.stringify("we\u0000e")); // "we e" !!
问题是 JSON 是否应该支持字符串中的 '\0'。 PostgreSQL 和 C/C++ 说不。其他人说肯定的。其他人说也许...
无论哪种方式,Chrome JS 中的 JSON.parse/stringify 之间肯定存在一些不一致。其他json解析器对'\u0000'没有问题。
最后一行让我完全困惑!
【问题讨论】:
-
"'we\u0000e'"是一个 Javascript 字符串文字,其 NUL 字符编码为反斜杠转义..."'we\\u0000e'"怎么样?这会解析吗? -
第三个示例令人困惑,无论是否支持空字符,它都不会是有效的 JSON (
'foo')。此外,字符的呈现方式和它们在内存中的表示方式是两件不同的事情。您确定在上一个示例中呈现代码点的代码尚未决定将 null 呈现为空格吗?
标签: javascript json null