JSON5 is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files). It is not intended to be used for machine-to-machine communication. (Keep using JSON or other file formats for that. 🙂)
{ // comments "unquoted":"and you can quote me on that", "singleQuotes":"I can use \"double quotes\" here", "lineBreaks":"Look, Mom! \ No \\n's!", "hexadecimal":0xdecaf, "leadingDecimalPoint":0.8675309, "andTrailing":8675309, "positiveSign": +1, "trailingComma":"in objects", "andIn":["arrays"], "backwardsCompatible":"with JSON" }
In the JSON5 specification, the ECMAScript 5.1 features have been extended to JSON5. In antoher words, this tells us that the json5 config content is a javascript object. If we can export the object safely, the parse problem will been resolved.
The first tool that comes to my mind is the NodeJS core library VM, it provides a sandbox for run unsafe code. We can run the json5 string in vm sandbox environment and export the object with vm context.
The result looks good. So the vm way is simple solution for parse json5. Compared to json5 package, export the javascript object with vm core pacakge is more simple. Applying more ECMAScript feature in json5 config also will be ok if you parse with the vm parse solution.
4. Summary
JSON5 make json config easier for humans to write and maintain by hand. For json5 config file, you can parse it by json5 npm pacakge or custom solution. The vm way which is tested in chapter 3 is a good custom solution for parse json5. You can copy/paste the json5.ts file in your project and import JSON5 class to test and check.