circular
Tiny utility to safely stringify objects with circular references
Last updated 10 years ago by muji .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install circular 
SYNC missed versions from official npm registry.

Circular

Tiny utility to safely stringify objects with circular references.

Usage

Replace all circular references with the string [Circular];

var circular = require('circular');
var obj = {}; var child = {parent: obj}; obj.child = child;
var str = JSON.stringify(obj, circular());
// => {"child":{"parent":"[Circular]"}}

If you prefer you can pass an alternative string to use:

var str = JSON.stringify(obj, circular('#ref'));

Or a function that returns a string:

function ref(value){return '' + value};
var str = JSON.stringify(obj, circular(ref));

As of version 1.0.4 you can also pass an additional boolean that will convert function references to strings, useful for converting javascript modules to couchdb design documents.

var str = JSON.stringify(obj, circular(null, true));

License

Everything is MIT. Read the license if you feel inclined.

Current Tags

  • 1.0.5                                ...           latest (10 years ago)

4 Versions

  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           11 years ago
  • 1.0.2                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE