json-literal

superset of JSON adding date, regex, null and octal literals

json-literal

Superset of JSON adding date, regex, null, undefined and octal literals. Inspired by @substack's json-literal-parse.

Key Points:

  • Secure Parser, it does not use 'eval', except on something that's just been through JSON.stringify to sanitize it.
  • Stringifier does not produce valid JSON, it writes un-quoted keys when possible, and includes RegExp and Date Literals.
  • The parser understands any Date of the form new Date(...args)
  • The parser understands any RegExp of the form /regexp/gi
  • The parser accepts un-quoted keys, providing they are valid identifiers (e.g. {a: 5, b: "foo"})
  • The parser accepts either " or ' as quotes for strings
  • The parser allows comments as both // line comment and /* inline comment */

npm install json-literal

e.g.

var JSONL = require('json-literal')
var str = JSONL.stringify({
  str: 'This is a string',
  'some-attributes-require-quotes': 10,
  updated: new Date('2013-07-12T15:42:00.000Z'),
  match: /^\d\d\d\d\-\d\d\-\d\d$/
})
// => '({str:"This is a string","some-attributes-require-quotes":10,updated:new Date("2013-07-12T15:42:00.000Z"),match:/^\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d$/})' 
var obj = JSONL.parse(str)
// => { str: 'This is a string', 
//      'some-attributes-require-quotes': 10, 
//      updated: new Date('2013-07-12T15:42:00.000Z'), 
//      match: /^\d\d\d\d\-\d\d\-\d\d$/ } 
var JSONL = require('json-literal')

Parse the input string str, returning the parsed representation obj.

JSONL.parse() is just like JSON.parse() except that the input may have additional "literal" types not in the JSON spec, which are:

  • date (as new Date(...args))
  • regex
  • null
  • undefined
  • octal

and input can contain comments of the form:

  • // line comment
  • /* inline comment */

You may optionally denote a JSONL string as not being a JSON string by surrounding it with parentheses, which will be stripped during parsing.

Stringify the input object obj, returning the string representation str.

JSONL.stringify() is just like JSON.stringify() except that it supports additional "literal" types not in the JSON spec, and will NOT return a valid JSON object.

To differentiate the JSONL string from a JSON string, it is placed in parentheses.

MIT