admin管理员组

文章数量:1410674

Property-based testing uncovering a lot of bugs. After fixing a bug with PostgreSQL, it ends up manifesting as a JavaScript bug. This was tested in 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" !!

The question is whether JSON should support '\0' in strings. PostgreSQL & C/C++ say no. Others say sure. And others say maybe...

Either way, there is definitely some inconsistency between JSON.parse/stringify in Chrome JS. Other json parsers have no problem with '\u0000'.

And I'm totally baffled by the last line!

Property-based testing uncovering a lot of bugs. After fixing a bug with PostgreSQL, it ends up manifesting as a JavaScript bug. This was tested in 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" !!

The question is whether JSON should support '\0' in strings. PostgreSQL & C/C++ say no. Others say sure. And others say maybe...

Either way, there is definitely some inconsistency between JSON.parse/stringify in Chrome JS. Other json parsers have no problem with '\u0000'.

And I'm totally baffled by the last line!

Share Improve this question edited Jan 19, 2019 at 23:09 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Jan 19, 2019 at 23:07 RayRay 3,10923 silver badges29 bronze badges 2
  • 2 "'we\u0000e'" is a Javascript string literal with a NUL character encoded as a backslash escape... What about "'we\\u0000e'"? Does that parse? – AKX Commented Jan 19, 2019 at 23:13
  • The third example is confusing, that would never be valid JSON ('foo') regardless of null character support. Also, how characters are rendered and how they are represented in memory are two different things. Are you sure the code that renders out codepoints hasn't decided to render null as a space in your last example? – Qix - MONICA WAS MISTREATED Commented Jan 19, 2019 at 23:13
Add a ment  | 

2 Answers 2

Reset to default 5

JSON.stringify with such strings results in a string with literal backslash characters:

JSON.stringify("we\0e");

results in a string containing

"we\u0000e"

(including the "s). This string does not contain an actual nul character - rather, it contains a \ character, followed by u, followed by 4 zeros.

The problem with your first

JSON.parse("'we\u0000e'");

is that JSON.parse accepts only double-quoted strings, not single-quoted strings. (JSON.parse(`'foo'`) throws an error too)

The problem with the second

JSON.parse('"we\u0000e"');

is that the interpreter first de-escapes the string passed to JSON.parse to one containing an actual nul character, and JSON.parse doesn't understand such characters. The parsed string doesn't contain 11 characters, it only contains 6.

console.log('"we\u0000e"');
console.log('"we\u0000e"'.length);

You need to indicate a literal backslash, followed by u0000, for the JSON.parse to work correctly:

console.log('"we\\u0000e"'.length);
console.log(JSON.parse('"we\\u0000e"'));

If you chain together JSON.parse with JSON.stringify, it works just fine (things are less confusing when you don't have to escape everything manually):

console.log(JSON.stringify("we\0e"));
console.log(JSON.parse(JSON.stringify("we\0e")));

JSON cannot contain NUL characters but it can contain the escape sequence \u0000 to represent a NUL character. In JavaScript that would be represented as '\\u0000' ('\u0000' is just a string with length one; a single NUL character).

console.log( JSON.parse('"we\\u0000e"') === "we\0e" );

本文标签: Javascript JSON strings with nul 3939Stack Overflow