Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

JSON superset #12

Closed
uhyo opened this issue Jul 3, 2019 · 3 comments
Closed

JSON superset #12

uhyo opened this issue Jul 3, 2019 · 3 comments

Comments

@uhyo
Copy link
Contributor

uhyo commented Jul 3, 2019

I'm not sure this is a bug, but this behavior is not an ideal one, I think.

This plugin generates a source code that old JavaScript engine cannot parse when object includes U+2028 or U+2029 in a string. cf. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON#JavaScript_and_JSON_differences

before

const obj = {
  foo: "\u2028\u2029"
};

console.log(obj.foo);

after

const obj = JSON.parse('{"foo":"

"}');
console.log(obj.foo);

When the code after conversion is run in node.js v8:

(function (exports, require, module, __filename, __dirname) { const obj = JSON.parse('{"foo":"
                                                                                     ^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

According to MDN, the latest Edge seems to fail too.

@mathiasbynens
Copy link

Relevant write-up: https://v8.dev/features/subsume-json

If you want optimal backwards compatibility, you could indeed escape those two characters. Alternatively you could document that this plugin produces output that only works in modern environments (e.g. Node.js v10+).

@talentedandrew
Copy link

@nd-02110114 @mathiasbynens is this plugin production ready?

@nissy-dev
Copy link
Owner

@mathiasbynens
Thank you for your advice 👍

Alternatively you could document that this plugin produces output that only works in modern environments (e.g. Node.js v10+).

I decided not to care about this backwards compatibility... (This is because Node.js v8 LTS is until 2019/12/31). I just documented this issue in README.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants