$ npm install json-literal
Superset of JSON
adding circular references, date, regex, null, undefined and octal literals while also making it more flexible so as to be easier to read and write. Inspired by @substack's json-literal-parse.
Key Points:
JSON.stringify
to sanitize it.new Circular("path", "to", "canonical", "instance", "of", "object")
new Date(...args)
/regexp/gi
{a: 5, b: "foo"}
)"
or '
as quotes for strings// 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:
new Date(...args)
)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
© 2010 - cnpmjs.org x YWFE | Home | YWFE