json-with-bigint 2.1.0 → 2.1.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 +2 -0
- package/json-with-bigint.js +7 -7
- package/json-with-bigint.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,8 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
|
24
24
|
|
|
25
25
|
✔️ Parses and stringifies all other values other than big numbers same way as native JSON methods in JS do
|
|
26
26
|
|
|
27
|
+
✔️ Correctly parses float numbers and negative numbers
|
|
28
|
+
|
|
27
29
|
✔️ Does not contaminate your global space (unlike monkey-patching solution)
|
|
28
30
|
|
|
29
31
|
✔️ 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())
|
package/json-with-bigint.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
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
|
|
4
4
|
*/
|
|
5
5
|
export const JSONStringify = (data) => {
|
|
6
|
-
const bigInts = /([\[:])?"(
|
|
6
|
+
const bigInts = /([\[:])?"(-?\d+)n"([,\}\]])/g;
|
|
7
7
|
const preliminaryJSON = JSON.stringify(data, (_, value) =>
|
|
8
8
|
typeof value === "bigint" ? value.toString() + "n" : value
|
|
9
9
|
);
|
|
@@ -19,18 +19,18 @@ export const JSONStringify = (data) => {
|
|
|
19
19
|
Other types of values are not affected and parsed as native JSON.parse() would parse them.
|
|
20
20
|
|
|
21
21
|
Big numbers are found and marked using RegEx with these conditions:
|
|
22
|
-
- Before the match there
|
|
23
|
-
- 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)
|
|
24
|
-
- After the match there
|
|
22
|
+
- Before the match there is no . and there is ": OR ":[ OR ":[anyNumberOf(anyCharacters) with no \ before them
|
|
23
|
+
- 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
|
|
24
|
+
- After the match there is , OR } without " after it OR ] without " after it
|
|
25
25
|
*/
|
|
26
26
|
export const JSONParse = (json) => {
|
|
27
27
|
const numbersBiggerThanMaxInt =
|
|
28
|
-
/(
|
|
29
|
-
const serializedData = json.replace(numbersBiggerThanMaxInt,
|
|
28
|
+
/(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\d*])(-?\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])))(?=,|\}[^"]|\][^"])/g;
|
|
29
|
+
const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`);
|
|
30
30
|
|
|
31
31
|
return JSON.parse(serializedData, (_, value) => {
|
|
32
32
|
const isCustomFormatBigInt =
|
|
33
|
-
typeof value === "string" && value.match(
|
|
33
|
+
typeof value === "string" && Boolean(value.match(/^-?\d+n$/));
|
|
34
34
|
|
|
35
35
|
if (isCustomFormatBigInt)
|
|
36
36
|
return BigInt(value.substring(0, value.length - 1));
|
package/json-with-bigint.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const JSONStringify=t=>JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":n)).replace(/([\[:])?"(
|
|
1
|
+
export const JSONStringify=t=>JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":n)).replace(/([\[:])?"(-?\d+)n"([,\}\]])/g,"$1$2$3");export const JSONParse=t=>{const n=t.replace(/(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\d*])(-?\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])))(?=,|\}[^"]|\][^"])/g,'"$1n"');return JSON.parse(n,((t,n)=>"string"==typeof n&&Boolean(n.match(/^-?\d+n$/))?BigInt(n.substring(0,n.length-1)):n))};
|
package/package.json
CHANGED