Skip to content

Commit dec0e8e

Browse files
committed
v0.0.9
1 parent 68a53f3 commit dec0e8e

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
os: [ubuntu-latest, windows-latest]
19-
node_version: [10, 12, 13]
19+
node_version: [18, 20, 25]
2020
steps:
2121
- name: Checkout Master
2222
uses: actions/checkout@v1

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v25

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-bigint-patch",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "JSON.parse with bigints support",
55
"main": "dist/index.js",
66
"scripts": {
@@ -23,5 +23,6 @@
2323
"devDependencies": {
2424
"jest": "25.2.7",
2525
"rollup": "2.3.2"
26-
}
26+
},
27+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
2728
}

src/index.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1-
import { stringify } from './lib/stringify';
2-
import { json_parse } from './lib/parse'
1+
import { stringify } from "./lib/stringify";
2+
import { json_parse } from "./lib/parse";
33

44
let globalObj;
55

6-
if (typeof globalThis !== 'undefined') {
7-
globalObj = globalThis;
8-
} else if (typeof self !== 'undefined') {
9-
globalObj = self;
10-
} else if (typeof window !== 'undefined') {
11-
globalObj = window;
12-
} else if (typeof global !== 'undefined') {
13-
globalObj = global;
6+
if (typeof globalThis !== "undefined") {
7+
globalObj = globalThis;
8+
} else if (typeof self !== "undefined") {
9+
globalObj = self;
10+
} else if (typeof window !== "undefined") {
11+
globalObj = window;
12+
} else if (typeof global !== "undefined") {
13+
globalObj = global;
1414
}
1515

16-
globalObj.BigInt.prototype.toJSON = function () {
16+
// Check for JSON.rawJSON support
17+
if (JSON.rawJSON) {
18+
// Using toJSON() method to handle BigInt serialization
19+
BigInt.prototype.toJSON = function () {
20+
return JSON.rawJSON(this.toString());
21+
};
22+
const nativeParser = globalObj.JSON.parse;
23+
// Using reviver function to handle BigInt deserialization
24+
globalObj.JSON.parse = function JSONparseWithBigInt(text, reviver) {
25+
return nativeParser(text, function (key, value, context) {
26+
if (typeof value === "number") {
27+
if (context.source != value.toString()) {
28+
try {
29+
return BigInt(context.source);
30+
} catch (e) {
31+
// Not a valid BigInt, return the original number
32+
}
33+
}
34+
}
35+
return reviver ? reviver(key, value) : value;
36+
});
37+
};
38+
// Fallback if JSON.rawJSON is not available
39+
} else {
40+
globalObj.BigInt.prototype.toJSON = function () {
1741
return this;
42+
};
43+
globalObj.JSON.stringify = stringify;
44+
globalObj.JSON.parse = json_parse();
1845
}
19-
20-
globalObj.JSON.parse = json_parse();
21-
globalObj.JSON.stringify = stringify;

test/bigint.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe("Testing bigint support", function(){
1+
describe("Testing bigint support", () => {
22

33
let nativeJSON = {
44
parse: JSON.parse,
@@ -8,23 +8,21 @@ describe("Testing bigint support", function(){
88

99
var input = '{"big":9223372036854775807,"small":123}';
1010

11-
it("Should show classic JSON.parse lacks bigint support", function(done){
11+
it("Should show classic JSON.parse lacks bigint support", () => {
1212
var obj = nativeJSON.parse(input);
1313
expect(obj.small.toString()).toEqual("123");
1414
expect(obj.big.toString()).not.toEqual("9223372036854775807");
1515

1616
var output = nativeJSON.stringify(obj);
1717
expect(output).not.toEqual(input);
18-
done();
1918
});
2019

21-
it("Should show JSONbig does support bigint parse/stringify roundtrip", function(done){
20+
it("Should show JSONbig does support bigint parse/stringify roundtrip", () => {
2221
var obj = JSON.parse(input);
2322
expect(obj.small.toString()).toEqual("123");
2423
expect(obj.big.toString()).toEqual("9223372036854775807");
2524

2625
var output = JSON.stringify(obj);
2726
expect(output).toEqual(input);
28-
done();
2927
});
3028
});

0 commit comments

Comments
 (0)