json-with-bigint 1.0.1 → 1.1.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
@@ -8,23 +8,28 @@ JS library that allows you to easily serialize and deserialize data with BigInt
8
8
 
9
9
  1. You need to convert some data to/from JSON and it includes BigInt values
10
10
  2. Native JSON.stringify() and JSON.parse() methods in JS can't work with BigInt
11
- 3. Other libraries and pieces of code that you'll find can't solve this problem while supporting consistent round-trip operations (meaning, you will not get the same BigInt values if you serialize and then deserialize them)
11
+ 3. Other libraries and pieces of code that you'll find either can't solve this problem while supporting consistent round-trip operations (meaning, you will not get the same BigInt values if you serialize and then deserialize them) or requires you to change your JSON or the way you want to work with your data
12
12
 
13
13
  ## Good things about JSON-with-BigInt
14
14
 
15
15
  ✔️ Supports consistent round-trip operations with JSON
16
16
 
17
17
  ```
18
- JSONParse(JSONStringify(9007199254740991n)) === 9007199254740991n // true
18
+ const data = { bigNumber: 9007199254740992n };
19
+ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
19
20
  ```
20
21
 
22
+ ✔️ No need to change your JSON or the way you want to work with your data
23
+
24
+ ✔️ Parses and stringifies all other values other than big numbers same way as native JSON methods in JS do
25
+
21
26
  ✔️ Does not contaminate your global space (unlike monkey-patching solution)
22
27
 
23
- ✔️ You don't have to memorize this library's API, you already know it, just skip the dot and that's it
28
+ ✔️ 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())
24
29
 
25
30
  ✔️ Can be used in a TypeScript project (.d.ts file included)
26
31
 
27
- ✔️ Size: 164 bytes (minified and gzipped)
32
+ ✔️ Size: 301 bytes (minified and gzipped)
28
33
 
29
34
  ✔️ No dependencies
30
35
 
@@ -46,7 +51,7 @@ and use it
46
51
  import { JSONParse, JSONStringify } from 'json-with-bigint';
47
52
 
48
53
  const userData = {
49
- someBigNumber: 9007199254740991n
54
+ someBigNumber: 9007199254740992n
50
55
  };
51
56
 
52
57
  localStorage.setItem('userData', JSONStringify(userData));
@@ -67,7 +72,7 @@ and use it
67
72
  ```
68
73
  <script>
69
74
  const userData = {
70
- someBigNumber: 9007199254740991n
75
+ someBigNumber: 9007199254740992n
71
76
  };
72
77
 
73
78
  localStorage.setItem('userData', JSONStringify(userData));
@@ -84,7 +89,7 @@ Download json-with-bigint.min.js from this repository to your project's folder a
84
89
  <script src="./json-with-bigint.min.js"></script>
85
90
  <script>
86
91
  const userData = {
87
- someBigNumber: 9007199254740991n
92
+ someBigNumber: 9007199254740992n
88
93
  };
89
94
 
90
95
  localStorage.setItem('userData', JSONStringify(userData));
@@ -101,7 +106,7 @@ Download json-with-bigint.min.js from this repository to your project's folder a
101
106
 
102
107
  Examples:
103
108
 
104
- - `JSONParse('{"someBigNumber":9007199254740991n}')`
109
+ - `JSONParse('{"someBigNumber":9007199254740992n}')`
105
110
  - `JSONStringify({
106
- someBigNumber: 9007199254740991n
111
+ someBigNumber: 9007199254740992n
107
112
  })`
@@ -1,11 +1,35 @@
1
+ /*
2
+ Function to serialize data to JSON string.
3
+ Converts all BigInt values to strings with "n" character at the end.
4
+ */
1
5
  export const JSONStringify = (data) =>
2
6
  JSON.stringify(data, (_, value) =>
3
7
  typeof value === "bigint" ? value.toString() + "n" : value
4
8
  );
5
9
 
6
- export const JSONParse = (serializedData) =>
7
- JSON.parse(serializedData, (_, value) =>
8
- typeof value === "string" && value.match(/^\d+n$/)
9
- ? BigInt(value.substring(0, value.length - 1))
10
- : value
11
- );
10
+ /*
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
+ */
17
+ export const JSONParse = (json) => {
18
+ /*
19
+ Big numbers are found and marked using Regex with this condition:
20
+ Number's length is bigger than 16 || Number's length is 16 and any numerical digit of the number is greater than that of the Number.MAX_SAFE_INTEGER
21
+ */
22
+ const numbersBiggerThanMaxInt =
23
+ /([\[:])?(\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;
24
+ const serializedData = json.replace(numbersBiggerThanMaxInt, '$1"$2n"$3');
25
+
26
+ return JSON.parse(serializedData, (_, value) => {
27
+ const isCustomFormatBigInt =
28
+ typeof value === "string" && value.match(/^\d+n$/);
29
+
30
+ if (isCustomFormatBigInt)
31
+ return BigInt(value.substring(0, value.length - 1));
32
+
33
+ return value;
34
+ });
35
+ };
@@ -1 +1 @@
1
- export const JSONStringify=t=>JSON.stringify(t,(t,n)=>"bigint"==typeof n?n.toString()+"n":n);export const JSONParse=t=>JSON.parse(t,(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));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.0.1",
3
+ "version": "1.1.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": {
@@ -12,12 +12,7 @@
12
12
  "BigInt",
13
13
  "js",
14
14
  "ts",
15
- "library",
16
- "JSON.parse",
17
- "JSON.stringify",
18
- "serialization",
19
- "deserialization",
20
- "round-trip"
15
+ "library"
21
16
  ],
22
17
  "author": "Ivan Korolenko <iam@ivankorolenko.com>",
23
18
  "license": "MIT"