BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / json-stringify-safe / README.md

README.md

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
README.md 1.2 KiB Download Raw

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.

Source
1# json-stringify-safe
2
3Like JSON.stringify, but doesn't throw on circular references.
4
5## Usage
6
7Takes the same arguments as `JSON.stringify`.
8
9```javascript
10var stringify = require('json-stringify-safe');
11var circularObj = {};
12circularObj.circularRef = circularObj;
13circularObj.list = [ circularObj, circularObj ];
14console.log(stringify(circularObj, null, 2));
15```
16
17Output:
18
19```json
20{
21 "circularRef": "[Circular]",
22 "list": [
23 "[Circular]",
24 "[Circular]"
25 ]
26}
27```
28
29## Details
30
31```
32stringify(obj, serializer, indent, decycler)
33```
34
35The first three arguments are the same as to JSON.stringify. The last
36is an argument that's only used when the object has been seen already.
37
38The default `decycler` function returns the string `'[Circular]'`.
39If, for example, you pass in `function(k,v){}` (return nothing) then it
40will prune cycles. If you pass in `function(k,v){ return {foo: 'bar'}}`,
41then cyclical objects will always be represented as `{"foo":"bar"}` in
42the result.
43
44```
45stringify.getSerialize(serializer, decycler)
46```
47
48Returns a serializer that can be used elsewhere. This is the actual
49function that's passed to JSON.stringify.
50
51**Note** that the function returned from `getSerialize` is stateful for now, so
52do **not** use it more than once.