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

jsan

v3.1.14

Published

handle circular references when stringifying and parsing

Downloads

273,430

Readme

jsan

Build Status

JavaScript "All The Things" Notation

jsan

Easily stringify and parse any object including objects with circular references, self references, dates, regexes, undefined, errors, and even functions 1, using the familar parse and stringify methods.

There are two ways to use this library, the first is to be able to serialize without having to worry about circular references, the second way is be able to handle dates, regexes, errors, functions 1, errors, and undefined (normally JSON.stringify({ u: undefined }) === '{}')

The usage reflect these two approaches. If you just want to be able to serialize an object then use jsan.stringify(obj), if you want to JSON all the things then use it like jsan.stringify(obj, null, null, true), the first three arguments are the same as JSON.stringify (yup, JSON.stringify takes three arguments)

Note that jsan.stringify(obj, null, null, true) will also deal with circular references

Usage

var jsan = require('jsan');

var obj = {};
obj['self'] = obj;
obj['sub'] = {};
obj['sub']['subSelf'] = obj['sub'];
obj.now = new Date(2015, 0, 1);

var str = jsan.stringify(obj);
str === '{"self":{"$jsan":"$"},"sub":{"subSelf":{"$jsan":"$.sub"}},"now":"2015-01-01T05:00:00.000Z"}'; // true
var str2 = jsan.stringify(obj, null, null, true);
str2 === '{"self":{"$jsan":"$"},"sub":{"subSelf":{"$jsan":"$.sub"}},"now":{"$jsan":"d1420088400000"}}'; // true

var newObj1 = jsan.parse(str);
newObj1 === newObj1['self']; // true
newObj1['sub']['subSelf'] === newObj1['sub']; // true
typeof newObj1.now === 'string'; // true

var newObj2 = jsan.parse(str2);
newObj2 === newObj2['self']; // true
newObj2['sub']['subSelf'] === newObj2['sub']; // true
newObj2.now instanceof Date; // true

Notes

This ulitilty has been heavily optimized and performs as well as the native JSON.parse and JSON.stringify, for usages of jsan.stringify(obj) when there are no circular references. It does this by first try { JSON.stringify(obj) } and only when that fails, will it walk the object. Because of this it won't property handle self references that aren't circular by default. You can work around this by passing false as the fourth argument, or pass true and it will also handle dates, regexes, undefeined, errors, and functions

var obj = { r: /test/ };
var subObj = {};
obj.a = subObj;
obj.b = subObj;
var str1 = jsan.stringify(obj) // '{"r":{},a":{},"b":{}}'
var str2 = jsan.stringify(obj, null, null, false) // '{"r":{},"a":{},"b":{"$jsan":"$.a"}}'
var str3 = jsan.stringify(obj, null, null, true) // '{"r":{"$jsan":"r,test"},"a":{},"b":{"$jsan":"$.a"}}'
Functions

You can't execute the functions after stringify() and parse(), they are just functions that throw which have a toString() method similar to the original function

Advance Usage

You can specify how and what should be handled by passing an object as the fourth argument:

var obj = { u: undefined, r: /test/, f: function bar() {} };
var str = jsan.stringify(obj, null, null, { undefined: true, function: true }); // '{"u":{"$jsan":"u"},"r":{},"f":{"$jsan":"ffunction bar() { /* ... */ }"}}'

The function property of options can also take a function which will be used as the function stringifyer:

var obj = { u: undefined, r: /test/, f: function(x) { return x + 1 } };
var str = jsan.stringify(obj, null, null, {
  undefined: true,
  function: function(fn) { return fn.toString() }
});
str === '{"u":{"$jsan":"u"},"r":{},"f":{"$jsan":"ffunction (x) { return x + 1 }"}}'; // true