npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-literal

v1.1.0

Published

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

Downloads

452

Readme

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