Big update!Introducing GitHub Bot Commands. Learn more
Socket
Log inBook a demo

json-stringify-safe

Package Overview
Dependencies
0
Maintainers
2
Versions
5
Issues
File Explorer

Advanced tools

json-stringify-safe

Like JSON.stringify, but doesn't blow up on circular refs.

    5.0.1latest

Version published
Maintainers
2
Weekly downloads
22,431,796
decreased by-1.8%

Weekly downloads

Changelog

Source

5.0.1 (May 19, 2015)

  • Fixes stringify to only take ancestors into account when checking circularity.
    It previously assumed every visited object was circular which led to [false positives][issue9].
    Uses the tiny serializer I wrote for [Must.js][must] a year and a half ago.
  • Fixes calling the replacer function in the proper context (thisArg).
  • Fixes calling the cycleReplacer function in the proper context (thisArg).
  • Speeds serializing by a factor of Big-O(h-my-god-it-linearly-searched-every-object) it had ever seen. Searching only the ancestors for a circular references speeds up things considerably.

Readme

Source

json-stringify-safe

Like JSON.stringify, but doesn't throw on circular references.

Usage

Takes the same arguments as JSON.stringify.

var stringify = require('json-stringify-safe'); var circularObj = {}; circularObj.circularRef = circularObj; circularObj.list = [ circularObj, circularObj ]; console.log(stringify(circularObj, null, 2));

Output:

{ "circularRef": "[Circular]", "list": [ "[Circular]", "[Circular]" ] }

Details

stringify(obj, serializer, indent, decycler)

The first three arguments are the same as to JSON.stringify. The last is an argument that's only used when the object has been seen already.

The default decycler function returns the string '[Circular]'. If, for example, you pass in function(k,v){} (return nothing) then it will prune cycles. If you pass in function(k,v){ return {foo: 'bar'}}, then cyclical objects will always be represented as {"foo":"bar"} in the result.

stringify.getSerialize(serializer, decycler)

Returns a serializer that can be used elsewhere. This is the actual function that's passed to JSON.stringify.

Note that the function returned from getSerialize is stateful for now, so do not use it more than once.

Keywords

FAQs

What is json-stringify-safe?

Like JSON.stringify, but doesn't blow up on circular refs.

Is json-stringify-safe popular?

The npm package json-stringify-safe receives a total of 19,473,618 weekly downloads. As such, json-stringify-safe popularity was classified as popular.

Is json-stringify-safe well maintained?

We found that json-stringify-safe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 2 open source maintainers collaborating on the project.

Last updated on 19 May 2015

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc