json-literal
superset of `JSON` adding date, regex, null and octal literals
Last updated 11 years ago by forbeslindesay .
MIT · Repository · Original npm · Tarball · package.json
$ npm install json-literal 
SYNC missed versions from official npm registry.

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 */

Build Status Dependency Status NPM version

Installation

npm install json-literal

Example

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$/ }

API

var JSONL = require('json-literal')

var obj = JSONL.parse(str)

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.

var str = JSONL.stringify(obj)

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.

License

MIT

Current Tags

  • 1.1.0                                ...           latest (11 years ago)

2 Versions

  • 1.1.0                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE