You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Code adopted from https://github.com/Ivan-Korolenko/json-with-bigint
3
+
// Based on upstream commit 79f8c9eec0017eff0b89b371c045962e5c2da709 (v3.4.4, April 2025)
4
+
5
+
constnoiseValue=/^-?\d+n+$/;// Noise - strings that match the custom format before being converted to it
6
+
constoriginalStringify=JSON.stringify;
7
+
constoriginalParse=JSON.parse;
3
8
4
9
/*
5
-
Function to serialize data to JSON string
6
-
Converts BigInt values to custom format (strings with digits and "n" at the end) and then converts them to proper big integers in JSON string
10
+
Function to serialize value to a JSON string.
11
+
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.
7
12
*/
8
13
9
14
/**
10
15
* Serialize a value to JSON
11
16
* @param value A JavaScript value, usually an object or array, to be converted.
12
-
* @param replacer A function that transforms the results.
17
+
* @param replacer A function that transforms the results, or an array of strings and numbers that acts as an approved list for selecting object properties.
13
18
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
constprocessedJSON=convertedToCustomJSON.replace(bigInts,'$1$2$3');// Delete one "n" off the end of every BigInt value
81
+
constdenoisedJSON=processedJSON.replace(noise,'$1$2$3');// Remove one "n" off the end of every noisy string
41
82
42
-
returnfinalJSON;
83
+
returndenoisedJSON;
43
84
};
44
85
45
86
/*
46
-
Function to parse JSON
47
-
If JSON has values presented in a lib's custom format (strings with digits and "n" character at the end), we just parse them to BigInt values (for backward compatibility with previous versions of the lib)
48
-
If JSON has values greater than Number.MAX_SAFE_INTEGER, we convert those values to our custom format, then parse them to BigInt values.
87
+
Function to parse JSON.
88
+
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.
49
89
Other types of values are not affected and parsed as native JSON.parse() would parse them.
50
-
51
-
Big numbers are found and marked using a Regular Expression with these conditions:
52
-
53
-
1. Before the match there is no . and one of the following is present:
54
-
1.1 ":
55
-
1.2 ":[
56
-
1.3 ":[anyNumberOf(anyCharacters)
57
-
1.4 , with no ":"anyNumberOf(anyCharacters) before it
58
-
All " required in the rule are not escaped. All whitespace and newline characters outside of string values are ignored.
59
-
60
-
2. The match itself has more than 16 digits OR (16 digits and any digit of the number is greater than that of the Number.MAX_SAFE_INTEGER). And it may have a - sign at the start.
61
-
62
-
3. After the match one of the following is present:
63
-
3.1 , without anyNumberOf(anyCharacters) "} or anyNumberOf(anyCharacters) "] after it
64
-
3.2 } without " after it
65
-
3.3 ] without " after it
66
-
All whitespace and newline characters outside of string values are ignored.
(digits.length<MAX_DIGITS||(digits.length===MAX_DIGITS&&digits<=MAX_INT));// With a fixed number of digits, we can correctly use lexicographical comparison to do a numeric comparison
0 commit comments