0.1.3 • Published 6 years ago

@brillout/jpp v0.1.3

Weekly downloads
1
License
-
Repository
github
Last release
6 years ago

JSON++

Same as JSON but with added support for:

  • Date
  • undefined
  • NaN
  • Inifinity
  • RegExp

JSON is great but is lacking for some (crucial) JavaScript types such as Date:

const assert = require('assert');

let obj = {
  time: new Date(),
};

assert(obj.time.constructor===Date);

// JSON converts dates to strings
obj = JSON.parse(JSON.stringify(obj));
assert(obj.time.constructor===String);

Whereas JSON++ supports Date:

const assert = require('assert');
const parse = require('@brillout/jpp/parse');
const stringify = require('@brillout/jpp/stringify');

let obj = {
  time: new Date(),
};

assert(obj.time.constructor===Date);

// JSON++ preserves Date
obj = parse(stringify(obj));
assert(obj.time.constructor===Date);

Usage

// npm install @brillout/jpp
const parse = require('@brillout/jpp/parse');
const stringify = require('@brillout/jpp/stringify');

const obj = {
  hello: 'from the future',
  time: new Date('2042-01-01'),
};

// Serialize with JSON++
const obj_serialized = stringify(obj);

// Deserialize a JSON++ string
const obj_deserialized = parse(obj_serialized);

JSON++'s stringify and parse have the exact same interface than JSON.stringify and JSON.parse. So you can use all JSON's options.

Full example

Example exposing all differences between JSON and JSON++.

// /examples/jpp.js

const assert = require('assert');

const parse = require('@brillout/jpp/parse');
const stringify = require('@brillout/jpp/stringify');

const obj = {
  date: new Date(),
  undefined: undefined,
  NaN: NaN,
  Infinity: Infinity,
  regexp: /^\d+$/g,
};

// All of `obj` can be serialized with JSON++
const obj_jpp = parse(stringify(obj))
assert(obj_jpp.date.getTime()===obj.date.getTime());
assert(obj_jpp.undefined===undefined && 'undefined' in obj_jpp);
assert(isNaN(obj_jpp.NaN));
assert(obj_jpp.Infinity===Infinity);
assert(obj_jpp.regexp.toString()===obj.regexp.toString());

// JSON cannot serialize any of `obj`
const obj_json = JSON.parse(JSON.stringify(obj))
// JSON converts dates to strings
assert(obj_json.constructor!==Date);
// JSON removes properties with a value of `undefined`
assert(!('undefined' in obj_json));
// JSON converts `NaN` to `null`
assert(obj_json.NaN===null);
// JSON converts `Infinity` to `null`
assert(obj_json.Infinity===null);
// JSON converts RegExp to an empty object
assert(obj_json.regexp.constructor===Object && Object.keys(obj_json.regexp).length===0);

To run the example:

$ git clone git@github.com:brillout/jpp.git
$ cd jpp
$ npm install
$ npm run link
$ node ./examples/jpp.js

The npm run link is required to be able to self require('@brillout/jpp').

How it works

Let's see how JSON++ serializes an object:

// /examples/inspect.js

const stringify = require('@brillout/jpp/stringify');

const obj = {
  date: new Date(),
  undefined: undefined,
  NaN: NaN,
  Infinity: Infinity,
  regexp: /^\d+$/g,
};

// We use the second argument `2` to have a prettified JSON++ string.
// (Same as in `JSON.stringify(obj, undefined, 2)`).
console.log(stringify(obj, undefined, 2));
// Prints:
/*
{
  "date": "@brillout/jpp:tYpE|Date|2018-11-14T17:39:09.245Z",
  "undefined": "@brillout/jpp:tYpE|undefined",
  "NaN": "@brillout/jpp:tYpE|NaN",
  "Infinity": "@brillout/jpp:tYpE|Infinity",
  "regexp": "@brillout/jpp:tYpE|RegExp|/^\\d+$/g"
}
*/

JSON++ is based on JSON while using prefixed strings for unsupported types. The string @brillout/jpp:tYpE is used as a unique prefix to denote our special strings and make sure that regular strings are not converted.

@brillout/jpp uses the native JSON.parse and JSON.stringify functions while modifying the serialization of unsupported types.

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago