Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json5-utils

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json5-utils

JSON and JSON5 parsers done right

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29
decreased by-17.14%
Maintainers
1
Weekly downloads
 
Created
Source

This JSON5 version is a subset of ES5 language, specification is here:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

This is a language that defines data structures only, so following notes/restrictions are applied:

  • Literals (NullLiteral, BooleanLiteral, NumericLiteral, StringLiteral) are allowed.
  • Compatibility syntax is not supported, which means octal literals are forbidden.
  • ArrayLiterals and allowed, but instead of AssignmentExpressions you can only use other allowed Literals, ArrayLiterals and ObjectLiterals. Elisions are currently not supported.
  • ObjectLiterals and allowed, but instead of AssignmentExpressions you can only use other allowed Literals, ArrayLiterals and ObjectLiterals. Setters and getters are forbidden.
  • All other primary expressions ("this", Identifier, Expression) are forbidden.
  • Two unary expressions ('-' and '+') allowed before NumericLiterals.
  • Any data that has a number type can be represented, including +0, -0, +Infinity, -Infinity and NaN.
  • "undefined" is forbidden, use null instead if applicable.
  • Comments and whitespace are defined according to spec.

Main authority here is ES5 spec, so strict backward JSON compatibility is not guaranteed.

If you're unsure whether a behaviour of this library is a bug or not, you can run this test:

JSON5.parse(String(something))

Should always be equal to:

eval('(function(){"use strict"\nreturn ('+String(something)+'\n)\n})()')

If something meets all rules above. Parens and newlines in the example above are carefully placed so comments and another newlines will work properly, so don't look so impressed about that.

Options

parse(string, options):

stringify(object, options):

  • ascii - output ascii only (bool, default=false)
  • indent - indentation (string or number, default='\t')
  • quote - enquoting char (string, "'" or '"', default="'")

Modes of operation

TODO: not yet working

  • simple: no custom datatypes no .toJSON/.toJSON5 for serializing simple json-compatible objects only (i.e. user-generated data)

  • full custom datatypes .toJSON/.toJSON5 present (prototype only) for representing arbitrary data structures as close as possible

  • json: no custom datatypes .toJSON present json compatible

Weirdness of JSON5

  • no elisions, [,,,] -> [null,null,null]
  • [Object], [Circular] aren't parsed
  • no way of nicely representing multiline strings
  • unicode property names are way to hard to implement

Unicode support

// 27 whitespace characters (unicode defines 26 characters, and ES5 spec also adds \uFEFF as a whitespace)
var whitespace = /[\u0009-\u000D\u0020\u0085\u00A0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/

// 4 line terminators
var lineterminator = /[\u000A\u000D\u2028\u2029]/

All json5 libraries MUST support 4 line terminator characters and 27 whitespace characters described above. Some libraries MAY choose not to include support for other unicode characters and apply following restrictions:

Parsers that do not fully support unicode SHOULD treat all unknown characters starting with \x80 as unicode_letter except for line terminators and whitespace characters. This way all valid json5 objects will be parsed correctly, but some invalid json5 objects will be parsed as well.

Serializers that do not fully support unicode SHOULD treat all unknown characters starting with \x80 as control characters, and quote property names if they happen to be in there. This way all data will be serialized correctly.

Keywords

FAQs

Package last updated on 03 Nov 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc