Skip to content

BigInt support #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 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 and BigInt values.

## Usage

Expand All @@ -11,6 +11,7 @@ var stringify = require('json-stringify-safe');
var circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [ circularObj, circularObj ];
circularObj.bigint = 1n;
console.log(stringify(circularObj, null, 2));
```

Expand All @@ -22,7 +23,8 @@ Output:
"list": [
"[Circular]",
"[Circular]"
]
],
"bigint": "1"
}
```

Expand All @@ -41,6 +43,8 @@ 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.

`BigInt` values are serialized as strings.

```
stringify.getSerialize(serializer, decycler)
```
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "json-stringify-safe",
"version": "5.0.1",
"description": "Like JSON.stringify, but doesn't blow up on circular refs.",
"version": "5.1.0",
"description": "Like JSON.stringify, but doesn't blow up on circular refs and BigInts.",
"keywords": [
"json",
"stringify",
"circular",
"safe"
"safe",
"bigint"
],
"homepage": "https://github.com/isaacs/json-stringify-safe",
"bugs": "https://github.com/isaacs/json-stringify-safe/issues",
"author": "Isaac Z. Schlueter <[email protected]> (http://blog.izs.me)",
"contributors": [
"Andri Möll <[email protected]> (http://themoll.com)"
"Andri Möll <[email protected]> (http://themoll.com)",
"Luka Maljic <[email protected]> (https://codecraft.dev)"
],
"license": "ISC",
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function serializer(replacer, cycleReplacer) {
}
else stack.push(value)

if (typeof value === "bigint") value = value.toString()

return replacer == null ? value : replacer.call(this, key, value)
}
}
13 changes: 13 additions & 0 deletions test/stringify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ describe("Stringify", function() {
json.must.eql(jsonify([{name: "Alice"}, {name: "Alice"}]))
})

it("must stringify BigInts", function() {
var bigint = 1n
var json = stringify(bigint, null, 2)
json.must.eql(jsonify("1"))
})

it("must stringify circular objects with BigInts", function() {
var obj = {n: 1n}
obj.identity = {self: obj}
var json = stringify(obj, null, 2)
json.must.eql(jsonify({n: "1", identity: {self: "[Circular ~]"}}))
})

it("must call given decycler and use its output", function() {
var obj = {}
obj.a = obj
Expand Down