Like JSON.stringify, but enhanced, supports:
- offset for better typo when substituting the stringified json string.
- handle circular object, and doesn't blow up on circular refs.
Install
$ npm install json-stringify --save
Usage
var stringify = ;var array = 1 '2'; ;
You will get
We might encounter
So, if we have an template
{
"foo": <bar>,
"foo2": <bar2>
}
And there's an object obj
var bar = ; var bar2 = JSON; var obj = bar: bar bar2: bar2;
And the renderered result is:
"foo": "bee": "boo" // well formatted "foo2": "bee": "boo" // a little ugly
You must found the difference.
Circular Object
var circular = {};circularcircular = circular; var stringify = ;console;
output:
{
"circular": "[Circular ~]"
}
stringify(obj, replacer, indent, [options])
The first three arguments are the same as to JSON.stringify.
options.offset
defines the offset which described aboveoptions.decycler
the decycler method of json-stringify-safe
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.
Licence
MIT