Please support this book: buy it or donate

20. JSON superset

This chapter covers the ES2019 feature “JSON superset” by Richard Gibson) is at [stage 4](http://exploringjs.com/es2016-es2017/ch_tc39-process.html.

At the moment, JSON (as standardized via ECMA-404) is not a subset of ECMAScript:

  • Until recently, ECMAScript string literals couldn’t contain the characters U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR (you had to use an escape sequence to put them into a string). That is, the following source code produced a syntax error:
  1. const sourceCode = '"\u2028"';
  2. eval(sourceCode); // SyntaxError
  • JSON string literals can contain these two characters:
  1. const json = '"\u2028"';
  2. JSON.parse(json); // OK

Given that the syntax of JSON is fixed, a decision was made to remove the restriction for ECMAScript string literals. That simplifies the grammar of the specification, because you don’t need separate rules for ECMAScript string literals and JSON string literals.