• Home
Name
Date
Size
#Lines
LOC

..--

test/12-May-2024-250204

.npmignoreD12-May-20247 21

CHANGELOG.mdD12-May-2024734 1513

LICENSED12-May-2024765 1612

MakefileD12-May-2024675 3625

README.mdD12-May-20241.2 KiB5339

package.jsonD12-May-20241.7 KiB6766

stringify.jsD12-May-2024907 2822

README.md

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.
53