json-with-bigint 2.0.0 → 2.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
@@ -57,7 +57,7 @@ const userData = {
57
57
 
58
58
  localStorage.setItem('userData', JSONStringify(userData));
59
59
 
60
- const restoredUserData = JSONParse(localStorage.getItem('userData'));
60
+ const restoredUserData = JSONParse(localStorage.getItem('userData') || '');
61
61
  ```
62
62
 
63
63
  ### CDN
@@ -78,7 +78,7 @@ and use it
78
78
 
79
79
  localStorage.setItem('userData', JSONStringify(userData));
80
80
 
81
- const restoredUserData = JSONParse(localStorage.getItem('userData'));
81
+ const restoredUserData = JSONParse(localStorage.getItem('userData') || '');
82
82
  </script>
83
83
  ```
84
84
 
@@ -95,7 +95,7 @@ Download json-with-bigint.min.js from this repository to your project's folder a
95
95
 
96
96
  localStorage.setItem('userData', JSONStringify(userData));
97
97
 
98
- const restoredUserData = JSONParse(localStorage.getItem('userData'));
98
+ const restoredUserData = JSONParse(localStorage.getItem('userData') || '');
99
99
  </script>
100
100
  ```
101
101
 
@@ -1,3 +1,20 @@
1
- export function JSONStringify(data: any): string | undefined;
1
+ export type Json =
2
+ | null
3
+ | undefined
4
+ | string
5
+ | number
6
+ | bigint
7
+ | boolean
8
+ | JsonObject
9
+ | {}
10
+ | JsonArray;
2
11
 
3
- export function JSONParse(serializedData: string): any;
12
+ interface JsonObject {
13
+ [x: string]: Json;
14
+ }
15
+
16
+ interface JsonArray extends Array<Json> {}
17
+
18
+ export function JSONStringify(data: Json): string;
19
+
20
+ export function JSONParse<T extends Json = Json>(serializedData: string): T;
@@ -1,6 +1,6 @@
1
1
  /*
2
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
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
6
  const bigInts = /([\[:])?"(\d+)n"([,\}\]])/g;
@@ -14,18 +14,19 @@ export const JSONStringify = (data) => {
14
14
 
15
15
  /*
16
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)
17
+ 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)
18
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
19
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
20
+
21
+ Big numbers are found and marked using RegEx with these conditions:
22
+ - Before the match there's : OR :[ OR :[anyNumberOf(anyCharacters)
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's , OR } OR ]
20
25
  */
21
26
  export const JSONParse = (json) => {
22
- /*
23
- Big numbers are found and marked using Regex with this condition:
24
- 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
25
- */
26
27
  const numbersBiggerThanMaxInt =
27
- /([\[:])?(\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;
28
- const serializedData = json.replace(numbersBiggerThanMaxInt, '$1"$2n"$3');
28
+ /(?<=:|:\[|:\[.*)(\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"');
29
30
 
30
31
  return JSON.parse(serializedData, (_, value) => {
31
32
  const isCustomFormatBigInt =
@@ -1 +1 @@
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))};
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,'"$1n"');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,8 +1,9 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
- "main": "json-with-bigint.js",
5
+ "type": "module",
6
+ "exports": "./json-with-bigint.js",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "https://github.com/Ivan-Korolenko/json-with-bigint"