Socket
Book a DemoInstallSign in
Socket

json-literal

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-literal

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

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

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:

  • 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 stringifier and parser both represent circular refernces internally as new Circular("path", "to", "canonical", "instance", "of", "object")
  • 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

FAQs

Package last updated on 06 Aug 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