ES2019: JSON superset

[2019-01-29] dev, javascript, es2019, json
(Ad, please don’t block)

The proposal “JSON superset” (by Richard Gibson) is at stage 4 and therefore part of ECMAScript 2019. This blog post explains how it works.

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:
    const sourceCode = '"\u2028"';
    eval(sourceCode); // SyntaxError
    
  • JSON string literals can contain these two characters:
    const json = '"\u2028"';
    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.