json-with-bigint 3.2.2 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/json-with-bigint.cjs +31 -17
- package/json-with-bigint.js +31 -17
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/json-with-bigint.cjs
CHANGED
|
@@ -2,34 +2,46 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
|
|
|
2
2
|
const originalStringify = JSON.stringify;
|
|
3
3
|
const originalParse = JSON.parse;
|
|
4
4
|
|
|
5
|
-
/*
|
|
6
|
-
Function to serialize
|
|
5
|
+
/*
|
|
6
|
+
Function to serialize value to a JSON string.
|
|
7
7
|
Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
|
|
8
8
|
*/
|
|
9
|
-
const JSONStringify = (
|
|
9
|
+
const JSONStringify = (value, replacer, space) => {
|
|
10
10
|
if ("rawJSON" in JSON) {
|
|
11
11
|
return originalStringify(
|
|
12
|
-
|
|
13
|
-
(
|
|
14
|
-
|
|
12
|
+
value,
|
|
13
|
+
(key, value) => {
|
|
14
|
+
if (typeof value === "bigint") return JSON.rawJSON(value.toString());
|
|
15
|
+
|
|
16
|
+
if (typeof replacer === "function") return replacer(key, value);
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(replacer) && replacer.includes(key)) return value;
|
|
19
|
+
|
|
20
|
+
return value;
|
|
15
21
|
},
|
|
16
22
|
space
|
|
17
23
|
);
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
if (!
|
|
26
|
+
if (!value) return originalStringify(value, replacer, space);
|
|
21
27
|
|
|
22
|
-
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
|
28
|
+
const bigInts = /([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
23
29
|
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
24
30
|
const convertedToCustomJSON = originalStringify(
|
|
25
|
-
|
|
26
|
-
(
|
|
31
|
+
value,
|
|
32
|
+
(key, value) => {
|
|
27
33
|
const isNoise =
|
|
28
34
|
typeof value === "string" && Boolean(value.match(noiseValue));
|
|
29
35
|
|
|
30
36
|
if (isNoise) return value.toString() + "n"; // Mark noise values with additional "n" to offset the deletion of one "n" during the processing
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
if (typeof value === "bigint") return value.toString() + "n";
|
|
39
|
+
|
|
40
|
+
if (typeof replacer === "function") return replacer(key, value);
|
|
41
|
+
|
|
42
|
+
if (Array.isArray(replacer) && replacer.includes(key)) return value;
|
|
43
|
+
|
|
44
|
+
return value;
|
|
33
45
|
},
|
|
34
46
|
space
|
|
35
47
|
);
|
|
@@ -39,13 +51,13 @@ const JSONStringify = (data, space) => {
|
|
|
39
51
|
return denoisedJSON;
|
|
40
52
|
};
|
|
41
53
|
|
|
42
|
-
/*
|
|
54
|
+
/*
|
|
43
55
|
Function to parse JSON.
|
|
44
56
|
If JSON has number values greater than Number.MAX_SAFE_INTEGER, we convert those values to a custom format, then parse them to BigInt values.
|
|
45
57
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
46
58
|
*/
|
|
47
|
-
const JSONParse = (
|
|
48
|
-
if (!
|
|
59
|
+
const JSONParse = (text, reviver) => {
|
|
60
|
+
if (!text) return originalParse(text, reviver);
|
|
49
61
|
|
|
50
62
|
const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
|
|
51
63
|
const MAX_DIGITS = MAX_INT.length;
|
|
@@ -55,7 +67,7 @@ const JSONParse = (json) => {
|
|
|
55
67
|
const customFormat = /^-?\d+n$/;
|
|
56
68
|
|
|
57
69
|
// Find and mark big numbers with "n"
|
|
58
|
-
const serializedData =
|
|
70
|
+
const serializedData = text.replace(
|
|
59
71
|
stringsOrLargeNumbers,
|
|
60
72
|
(text, digits, fractional, exponential) => {
|
|
61
73
|
const isString = text[0] === '"';
|
|
@@ -77,7 +89,7 @@ const JSONParse = (json) => {
|
|
|
77
89
|
);
|
|
78
90
|
|
|
79
91
|
// Convert marked big numbers to BigInt
|
|
80
|
-
return originalParse(serializedData, (
|
|
92
|
+
return originalParse(serializedData, (key, value, context) => {
|
|
81
93
|
const isCustomFormatBigInt =
|
|
82
94
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
83
95
|
|
|
@@ -89,7 +101,9 @@ const JSONParse = (json) => {
|
|
|
89
101
|
|
|
90
102
|
if (isNoiseValue) return value.substring(0, value.length - 1); // Remove one "n" off the end of the noisy string
|
|
91
103
|
|
|
92
|
-
return value;
|
|
104
|
+
if (typeof reviver !== "function") return value;
|
|
105
|
+
|
|
106
|
+
return reviver(key, value, context);
|
|
93
107
|
});
|
|
94
108
|
};
|
|
95
109
|
|
package/json-with-bigint.js
CHANGED
|
@@ -2,34 +2,46 @@ const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format
|
|
|
2
2
|
const originalStringify = JSON.stringify;
|
|
3
3
|
const originalParse = JSON.parse;
|
|
4
4
|
|
|
5
|
-
/*
|
|
6
|
-
Function to serialize
|
|
5
|
+
/*
|
|
6
|
+
Function to serialize value to a JSON string.
|
|
7
7
|
Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
|
|
8
8
|
*/
|
|
9
|
-
export const JSONStringify = (
|
|
9
|
+
export const JSONStringify = (value, replacer, space) => {
|
|
10
10
|
if ("rawJSON" in JSON) {
|
|
11
11
|
return originalStringify(
|
|
12
|
-
|
|
13
|
-
(
|
|
14
|
-
|
|
12
|
+
value,
|
|
13
|
+
(key, value) => {
|
|
14
|
+
if (typeof value === "bigint") return JSON.rawJSON(value.toString());
|
|
15
|
+
|
|
16
|
+
if (typeof replacer === "function") return replacer(key, value);
|
|
17
|
+
|
|
18
|
+
if (Array.isArray(replacer) && replacer.includes(key)) return value;
|
|
19
|
+
|
|
20
|
+
return value;
|
|
15
21
|
},
|
|
16
22
|
space
|
|
17
23
|
);
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
if (!
|
|
26
|
+
if (!value) return originalStringify(value, replacer, space);
|
|
21
27
|
|
|
22
|
-
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
|
28
|
+
const bigInts = /([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g;
|
|
23
29
|
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
24
30
|
const convertedToCustomJSON = originalStringify(
|
|
25
|
-
|
|
26
|
-
(
|
|
31
|
+
value,
|
|
32
|
+
(key, value) => {
|
|
27
33
|
const isNoise =
|
|
28
34
|
typeof value === "string" && Boolean(value.match(noiseValue));
|
|
29
35
|
|
|
30
36
|
if (isNoise) return value.toString() + "n"; // Mark noise values with additional "n" to offset the deletion of one "n" during the processing
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
if (typeof value === "bigint") return value.toString() + "n";
|
|
39
|
+
|
|
40
|
+
if (typeof replacer === "function") return replacer(key, value);
|
|
41
|
+
|
|
42
|
+
if (Array.isArray(replacer) && replacer.includes(key)) return value;
|
|
43
|
+
|
|
44
|
+
return value;
|
|
33
45
|
},
|
|
34
46
|
space
|
|
35
47
|
);
|
|
@@ -39,13 +51,13 @@ export const JSONStringify = (data, space) => {
|
|
|
39
51
|
return denoisedJSON;
|
|
40
52
|
};
|
|
41
53
|
|
|
42
|
-
/*
|
|
54
|
+
/*
|
|
43
55
|
Function to parse JSON.
|
|
44
56
|
If JSON has number values greater than Number.MAX_SAFE_INTEGER, we convert those values to a custom format, then parse them to BigInt values.
|
|
45
57
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
46
58
|
*/
|
|
47
|
-
export const JSONParse = (
|
|
48
|
-
if (!
|
|
59
|
+
export const JSONParse = (text, reviver) => {
|
|
60
|
+
if (!text) return originalParse(text, reviver);
|
|
49
61
|
|
|
50
62
|
const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
|
|
51
63
|
const MAX_DIGITS = MAX_INT.length;
|
|
@@ -55,7 +67,7 @@ export const JSONParse = (json) => {
|
|
|
55
67
|
const customFormat = /^-?\d+n$/;
|
|
56
68
|
|
|
57
69
|
// Find and mark big numbers with "n"
|
|
58
|
-
const serializedData =
|
|
70
|
+
const serializedData = text.replace(
|
|
59
71
|
stringsOrLargeNumbers,
|
|
60
72
|
(text, digits, fractional, exponential) => {
|
|
61
73
|
const isString = text[0] === '"';
|
|
@@ -77,7 +89,7 @@ export const JSONParse = (json) => {
|
|
|
77
89
|
);
|
|
78
90
|
|
|
79
91
|
// Convert marked big numbers to BigInt
|
|
80
|
-
return originalParse(serializedData, (
|
|
92
|
+
return originalParse(serializedData, (key, value, context) => {
|
|
81
93
|
const isCustomFormatBigInt =
|
|
82
94
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
83
95
|
|
|
@@ -89,6 +101,8 @@ export const JSONParse = (json) => {
|
|
|
89
101
|
|
|
90
102
|
if (isNoiseValue) return value.substring(0, value.length - 1); // Remove one "n" off the end of the noisy string
|
|
91
103
|
|
|
92
|
-
return value;
|
|
104
|
+
if (typeof reviver !== "function") return value;
|
|
105
|
+
|
|
106
|
+
return reviver(key, value, context);
|
|
93
107
|
});
|
|
94
108
|
};
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r)=>{if("rawJSON"in JSON)return originalStringify(n,((n,
|
|
1
|
+
const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r,t)=>{if("rawJSON"in JSON)return originalStringify(n,((n,t)=>"bigint"==typeof t?JSON.rawJSON(t.toString()):"function"==typeof r?r(n,t):(Array.isArray(r)&&r.includes(n),t)),t);if(!n)return originalStringify(n,r,t);const i=originalStringify(n,((n,t)=>"string"==typeof t&&Boolean(t.match(noiseValue))||"bigint"==typeof t?t.toString()+"n":"function"==typeof r?r(n,t):(Array.isArray(r)&&r.includes(n),t)),t);return i.replace(/([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=(n,r)=>{if(!n)return originalParse(n,r);const t=Number.MAX_SAFE_INTEGER.toString(),i=t.length,e=/^"-?\d+n+"$/,o=/^-?\d+n$/,g=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,r,o,g)=>{const a='"'===n[0];if(a&&Boolean(n.match(e)))return n.substring(0,n.length-1)+'n"';const s=o||g,l=r&&(r.length<i||r.length===i&&r<=t);return a||s||l?n:'"'+n+'n"'}));return originalParse(g,((n,t,i)=>{if("string"==typeof t&&Boolean(t.match(o)))return BigInt(t.substring(0,t.length-1));return"string"==typeof t&&Boolean(t.match(noiseValue))?t.substring(0,t.length-1):"function"!=typeof r?t:r(n,t,i)}))};
|