json-with-bigint 2.3.3 → 2.4.0
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 +6 -4
- package/json-with-bigint.js +17 -5
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,17 +22,19 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
|
22
22
|
|
|
23
23
|
✔️ No need to change your JSON or the way you want to work with your data
|
|
24
24
|
|
|
25
|
-
✔️ Parses and stringifies all other values other than big numbers same way as native JSON methods in JS do
|
|
25
|
+
✔️ Parses and stringifies all other values other than big numbers the same way as native JSON methods in JS do
|
|
26
26
|
|
|
27
27
|
✔️ Correctly parses float numbers and negative numbers
|
|
28
28
|
|
|
29
|
+
✔️ Correctly parses pretty printed JSON (formatted with newline and whitespace characters)
|
|
30
|
+
|
|
29
31
|
✔️ Does not contaminate your global space (unlike monkey-patching solution)
|
|
30
32
|
|
|
31
|
-
✔️ You don't have to memorize this library's API, you already know it
|
|
33
|
+
✔️ You don't have to memorize this library's API, you already know it. Just skip the dot, use camelCase and that's it (JSONParse(), JSONStringify())
|
|
32
34
|
|
|
33
|
-
✔️ Can be used in
|
|
35
|
+
✔️ Can be used in both JavaScript and TypeScript projects (.d.ts file included)
|
|
34
36
|
|
|
35
|
-
✔️ Size:
|
|
37
|
+
✔️ Size: 762 bytes (minified and gzipped)
|
|
36
38
|
|
|
37
39
|
✔️ No dependencies
|
|
38
40
|
|
package/json-with-bigint.js
CHANGED
|
@@ -22,16 +22,28 @@ export const JSONStringify = (data, space) => {
|
|
|
22
22
|
If JSON has values greater than Number.MAX_SAFE_INTEGER, we convert those values to our custom format, then parse them to BigInt values.
|
|
23
23
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
24
24
|
|
|
25
|
-
Big numbers are found and marked using
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
Big numbers are found and marked using a Regular Expression with these conditions:
|
|
26
|
+
|
|
27
|
+
1. Before the match there is no . and one of the following is present:
|
|
28
|
+
1.1 ":
|
|
29
|
+
1.2 ":[
|
|
30
|
+
1.3 ":[anyNumberOf(anyCharacters)
|
|
31
|
+
1.4 , with no ":"anyNumberOf(anyCharacters) before it
|
|
32
|
+
All " required in the rule are not escaped. All whitespace and newline characters outside of string values are ignored.
|
|
33
|
+
|
|
34
|
+
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.
|
|
35
|
+
|
|
36
|
+
3. After the match one of the following is present:
|
|
37
|
+
3.1 ,
|
|
38
|
+
3.2 } without " after it
|
|
39
|
+
3.3 ] without " after it
|
|
40
|
+
All whitespace and newline characters outside of string values are ignored.
|
|
29
41
|
*/
|
|
30
42
|
export const JSONParse = (json) => {
|
|
31
43
|
if (!json) return JSON.parse(json);
|
|
32
44
|
|
|
33
45
|
const numbersBiggerThanMaxInt =
|
|
34
|
-
/(?<=[^\\]"
|
|
46
|
+
/(?<=[^\\]":\n*\s*[\[]?|[^\\]":\n*\s*\[.*[^\.\d*]\n*\s*|(?<![^\\]"\n*\s*:\n*\s*[^\\]".*),\n*\s*)(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\n*\s*\}[^"]?|\n*\s*\][^"])/g;
|
|
35
47
|
const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`);
|
|
36
48
|
|
|
37
49
|
return JSON.parse(serializedData, (_, value) => {
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const JSONStringify=(t
|
|
1
|
+
export const JSONStringify=(n,t)=>{if(!n)return JSON.stringify(n);return JSON.stringify(n,((n,t)=>"bigint"==typeof t?t.toString()+"n":t),t).replace(/([\[:])?"(-?\d+)n"([,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return JSON.parse(n);const t=n.replace(/(?<=[^\\]":\n*\s*[\[]?|[^\\]":\n*\s*\[.*[^\.\d*]\n*\s*|(?<![^\\]"\n*\s*:\n*\s*[^\\]".*),\n*\s*)(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\n*\s*\}[^"]?|\n*\s*\][^"])/g,'"$1n"');return JSON.parse(t,((n,t)=>"string"==typeof t&&Boolean(t.match(/^-?\d+n$/))?BigInt(t.substring(0,t.length-1)):t))};
|
package/package.json
CHANGED