json-with-bigint 3.1.1 → 3.2.2
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 +16 -4
- package/json-with-bigint.d.cts +28 -0
- package/json-with-bigint.js +16 -4
- package/json-with-bigint.min.js +1 -1
- package/package.json +5 -1
- /package/__tests__/{performance.js → performance.cjs} +0 -0
- /package/__tests__/{unit.js → unit.cjs} +0 -0
package/README.md
CHANGED
package/json-with-bigint.cjs
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
|
|
2
|
+
const originalStringify = JSON.stringify;
|
|
3
|
+
const originalParse = JSON.parse;
|
|
2
4
|
|
|
3
5
|
/*
|
|
4
6
|
Function to serialize data to a JSON string.
|
|
5
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.
|
|
6
8
|
*/
|
|
7
9
|
const JSONStringify = (data, space) => {
|
|
8
|
-
if (
|
|
10
|
+
if ("rawJSON" in JSON) {
|
|
11
|
+
return originalStringify(
|
|
12
|
+
data,
|
|
13
|
+
(_, value) => {
|
|
14
|
+
return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
|
|
15
|
+
},
|
|
16
|
+
space
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!data) return originalStringify(data);
|
|
9
21
|
|
|
10
22
|
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
|
11
23
|
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
12
|
-
const convertedToCustomJSON =
|
|
24
|
+
const convertedToCustomJSON = originalStringify(
|
|
13
25
|
data,
|
|
14
26
|
(_, value) => {
|
|
15
27
|
const isNoise =
|
|
@@ -33,7 +45,7 @@ const JSONStringify = (data, space) => {
|
|
|
33
45
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
34
46
|
*/
|
|
35
47
|
const JSONParse = (json) => {
|
|
36
|
-
if (!json) return
|
|
48
|
+
if (!json) return originalParse(json);
|
|
37
49
|
|
|
38
50
|
const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
|
|
39
51
|
const MAX_DIGITS = MAX_INT.length;
|
|
@@ -65,7 +77,7 @@ const JSONParse = (json) => {
|
|
|
65
77
|
);
|
|
66
78
|
|
|
67
79
|
// Convert marked big numbers to BigInt
|
|
68
|
-
return
|
|
80
|
+
return originalParse(serializedData, (_, value) => {
|
|
69
81
|
const isCustomFormatBigInt =
|
|
70
82
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
71
83
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type JsonObject = {
|
|
2
|
+
[x: string]: Json;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
type JsonArray = Json[];
|
|
6
|
+
|
|
7
|
+
export type Json =
|
|
8
|
+
| null
|
|
9
|
+
| undefined
|
|
10
|
+
| string
|
|
11
|
+
| number
|
|
12
|
+
| bigint
|
|
13
|
+
| boolean
|
|
14
|
+
| JsonObject
|
|
15
|
+
| {}
|
|
16
|
+
| JsonArray;
|
|
17
|
+
|
|
18
|
+
export function JSONStringify(
|
|
19
|
+
data: Exclude<Json, undefined>,
|
|
20
|
+
space?: string | number
|
|
21
|
+
): string;
|
|
22
|
+
|
|
23
|
+
export function JSONStringify(
|
|
24
|
+
data: undefined,
|
|
25
|
+
space?: string | number
|
|
26
|
+
): undefined;
|
|
27
|
+
|
|
28
|
+
export function JSONParse<T extends Json = Json>(serializedData: string): T;
|
package/json-with-bigint.js
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
|
|
2
|
+
const originalStringify = JSON.stringify;
|
|
3
|
+
const originalParse = JSON.parse;
|
|
2
4
|
|
|
3
5
|
/*
|
|
4
6
|
Function to serialize data to a JSON string.
|
|
5
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.
|
|
6
8
|
*/
|
|
7
9
|
export const JSONStringify = (data, space) => {
|
|
8
|
-
if (
|
|
10
|
+
if ("rawJSON" in JSON) {
|
|
11
|
+
return originalStringify(
|
|
12
|
+
data,
|
|
13
|
+
(_, value) => {
|
|
14
|
+
return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
|
|
15
|
+
},
|
|
16
|
+
space
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!data) return originalStringify(data);
|
|
9
21
|
|
|
10
22
|
const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
|
|
11
23
|
const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
|
|
12
|
-
const convertedToCustomJSON =
|
|
24
|
+
const convertedToCustomJSON = originalStringify(
|
|
13
25
|
data,
|
|
14
26
|
(_, value) => {
|
|
15
27
|
const isNoise =
|
|
@@ -33,7 +45,7 @@ export const JSONStringify = (data, space) => {
|
|
|
33
45
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
34
46
|
*/
|
|
35
47
|
export const JSONParse = (json) => {
|
|
36
|
-
if (!json) return
|
|
48
|
+
if (!json) return originalParse(json);
|
|
37
49
|
|
|
38
50
|
const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
|
|
39
51
|
const MAX_DIGITS = MAX_INT.length;
|
|
@@ -65,7 +77,7 @@ export const JSONParse = (json) => {
|
|
|
65
77
|
);
|
|
66
78
|
|
|
67
79
|
// Convert marked big numbers to BigInt
|
|
68
|
-
return
|
|
80
|
+
return originalParse(serializedData, (_, value) => {
|
|
69
81
|
const isCustomFormatBigInt =
|
|
70
82
|
typeof value === "string" && Boolean(value.match(customFormat));
|
|
71
83
|
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const noiseValue=/^-?\d+n
|
|
1
|
+
const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r)=>{if("rawJSON"in JSON)return originalStringify(n,((n,r)=>"bigint"==typeof r?JSON.rawJSON(r.toString()):r),r);if(!n)return originalStringify(n);return originalStringify(n,((n,r)=>"string"==typeof r&&Boolean(r.match(noiseValue))||"bigint"==typeof r?r.toString()+"n":r),r).replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return originalParse(n);const r=Number.MAX_SAFE_INTEGER.toString(),t=r.length,i=/^"-?\d+n+"$/,e=/^-?\d+n$/,g=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,e,g,o)=>{const a='"'===n[0];if(a&&Boolean(n.match(i)))return n.substring(0,n.length-1)+'n"';const s=g||o,l=e&&(e.length<t||e.length===t&&e<=r);return a||s||l?n:'"'+n+'n"'}));return originalParse(g,((n,r)=>{if("string"==typeof r&&Boolean(r.match(e)))return BigInt(r.substring(0,r.length-1));return"string"==typeof r&&Boolean(r.match(noiseValue))?r.substring(0,r.length-1):r}))};
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-with-bigint",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.2",
|
|
4
4
|
"description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"import": "./json-with-bigint.js",
|
|
8
8
|
"require": "./json-with-bigint.cjs"
|
|
9
9
|
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"performance": "node __tests__/performance.cjs",
|
|
12
|
+
"unit": "node __tests__/unit.cjs"
|
|
13
|
+
},
|
|
10
14
|
"repository": {
|
|
11
15
|
"type": "git",
|
|
12
16
|
"url": "https://github.com/Ivan-Korolenko/json-with-bigint"
|
|
File without changes
|
|
File without changes
|