Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# json-stringify-safe

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

## Usage

Expand All @@ -18,10 +18,10 @@ Output:

```json
{
"circularRef": "[Circular]",
"circularRef": "[Circular ~]",
"list": [
"[Circular]",
"[Circular]"
"[Circular ~]",
"[Circular ~]"
]
}
```
Expand All @@ -35,11 +35,13 @@ 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.
The default `decycler` function returns a string like `'[Circular ~.a.b]'`
where `~.a.b` represents where the circular reference comes from (`~` being
the stringified object). If, for example, you pass an empty function `(k,v) => {}`
then it will prune cycles. If you pass in `(k,v) => ({ foo: 'bar' })`, then
cyclical objects will always be represented as `{"foo":"bar"}` in the result.
If you pass `() => '[Circular]'`, then every circular reference will be just
`[Circular]` without the `~`-path.

```
stringify.getSerialize(serializer, decycler)
Expand Down