Skip to content

JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances

Notifications You must be signed in to change notification settings

Stephanemw/telejson

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TeleJSON

A library for teleporting rich data to another place.

Install

yarn add telejson

What can it do, what can't it do:

JSON.parse & JSON.stringify have limitation by design, because there are no data formats for things like

  • Date
  • Function
  • Class
  • Symbol
  • etc.

Also JSON doesn't support cyclic data structures.

This library allows you to pass in data with all all the above properties. It will transform the properties to something that's allowed by the JSON spec whilst stringifying, and then convert back to the cyclic data structure when parsing.

When parsing, class instances will be given the Class's name again. The prototype isn't copied over.

Functions are supported, they are stringified and will be eval-ed when called. This lazy eval is important for performance. The eval happens via safe-eval Functions are stripped of comments and whitespace.

Obviously calling the function will only really work as expected if the functions were pure the begin with.

Regular expressions just work.

Symbol will be re-created with the same string. (resulting in a similar, but different symbol)

Dates are parsed back into actual Date objects.

API

You have 2 choices:

import { stringify, parse } from 'telejson';

const Foo = function(){};

const root = {
  date: new Date('2018'),
  regex1: /foo/,
  regex2: /foo/g,
  regex2: new RegExp('foo','i'),
  fn1: () => 'foo',
  fn2: function fn2() { return 'foo'; },
  Foo: new Foo(),
};

// something cyclic
root.root = root;

const stringified = stringify(root);
const parsed = parse(stringified);

stringify and parse do not conform to the JSON.stringify or JSON.parse api. they take an data object and a option object.

OR you can use use the replacer and reviver:

import { replacer, reviver } from 'telejson';
import data from 'somewhere';

const stringified = JSON.stringify(data, reviver(), 2);
const parsed = JSON.parse(stringified, reviver(), 2);

notice that both replacer and reviver need to be called! doing the following will NOT WORK:

const stringified = JSON.stringify(data, reviver, 2);
const parsed = JSON.parse(stringified, reviver, 2);

options

You either pass the options-object to replacer or as a second argument to stringify:

replacer({ maxDepth: 10 });
stringify(date, { maxDepth: 10 });

replacer

maxDepth: controls how deep to keep stringifying. When max depth is reach, objects will be replaced with "[Object]", arrays will be replaced with "[Array(<lenght>)]". default value is 10 This option is really useful if your object is huge/complex, and you don't care deeply nested data,

space: controls how to prettify the output string. default value is undefined, no white space is use. Only relevant when using stringify.

reviver

Doesn't take any options right now.

Contributing

If you have any suggestions, please open an issue.

All contributions are welcome!

run tests:

yarn test

About

JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%