json-with-bigint 1.1.0 → 2.0.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 CHANGED
@@ -16,6 +16,7 @@ JS library that allows you to easily serialize and deserialize data with BigInt
16
16
 
17
17
  ```
18
18
  const data = { bigNumber: 9007199254740992n };
19
+ JSONStringify(data) // '{"bigNumber":9007199254740992}'
19
20
  JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
20
21
  ```
21
22
 
@@ -29,7 +30,7 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
29
30
 
30
31
  ✔️ Can be used in a TypeScript project (.d.ts file included)
31
32
 
32
- ✔️ Size: 301 bytes (minified and gzipped)
33
+ ✔️ Size: 424 bytes (minified and gzipped)
33
34
 
34
35
  ✔️ No dependencies
35
36
 
@@ -1,18 +1,22 @@
1
1
  /*
2
- Function to serialize data to JSON string.
3
- Converts all BigInt values to strings with "n" character at the end.
2
+ Function to serialize data to JSON string
3
+ Converts BigInt values to custom format (strings with "n" at the end) and then converts them to proper big integers in JSON string
4
4
  */
5
- export const JSONStringify = (data) =>
6
- JSON.stringify(data, (_, value) =>
5
+ export const JSONStringify = (data) => {
6
+ const bigInts = /([\[:])?"(\d+)n"([,\}\]])/g;
7
+ const preliminaryJSON = JSON.stringify(data, (_, value) =>
7
8
  typeof value === "bigint" ? value.toString() + "n" : value
8
9
  );
10
+ const finalJSON = preliminaryJSON.replace(bigInts, "$1$2$3");
11
+
12
+ return finalJSON;
13
+ };
9
14
 
10
15
  /*
11
- Function to parse JSON.
12
- If JSON has values in our custom format BigInt (strings with "n" character at the end), we just parse them to BigInt values.
13
- If JSON has big number values, but not in our custom format, we copy it, and in a copy we convert those values to our custom format,
14
- then parse them to BigInt values.
15
- Other values (not big numbers and not our custom format BigInt values) are not affected and parsed as native JSON.parse() would parse them.
16
+ Function to parse JSON
17
+ If JSON has values presented in a lib's custom format (strings with "n" character at the end), we just parse them to BigInt values (for backward compatibility with previous versions of the lib)
18
+ If JSON has values greater than Number.MAX_SAFE_INTEGER, we convert those values to our custom format, then parse them to BigInt values.
19
+ Other types of values are not affected and parsed as native JSON.parse() would parse them.
16
20
  */
17
21
  export const JSONParse = (json) => {
18
22
  /*
@@ -1 +1 @@
1
- export const JSONStringify=t=>JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":n));export const JSONParse=t=>{const n=t.replace(/([\[:])?(\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,'$1"$2n"$3');return JSON.parse(n,((t,n)=>"string"==typeof n&&n.match(/^\d+n$/)?BigInt(n.substring(0,n.length-1)):n))};
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{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,'$1"$2n"$3');return JSON.parse(n,((t,n)=>"string"==typeof n&&n.match(/^\d+n$/)?BigInt(n.substring(0,n.length-1)):n))};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
5
  "main": "json-with-bigint.js",
6
6
  "repository": {