json-with-bigint 2.2.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 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, just skip the dot, use camelCase and that's it (JSONParse(), JSONStringify())
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 a TypeScript project (.d.ts file included)
35
+ ✔️ Can be used in both JavaScript and TypeScript projects (.d.ts file included)
34
36
 
35
- ✔️ Size: 424 bytes (minified and gzipped)
37
+ ✔️ Size: 762 bytes (minified and gzipped)
36
38
 
37
39
  ✔️ No dependencies
38
40
 
@@ -1,3 +1,9 @@
1
+ type JsonObject = {
2
+ [x: string]: Json;
3
+ };
4
+
5
+ type JsonArray = Json[];
6
+
1
7
  export type Json =
2
8
  | null
3
9
  | undefined
@@ -9,12 +15,14 @@ export type Json =
9
15
  | {}
10
16
  | JsonArray;
11
17
 
12
- interface JsonObject {
13
- [x: string]: Json;
14
- }
15
-
16
- interface JsonArray extends Array<Json> {}
18
+ export function JSONStringify(
19
+ data: Exclude<Json, undefined>,
20
+ space?: string | number
21
+ ): string;
17
22
 
18
- export function JSONStringify(data: Json, space?: string | number): string;
23
+ export function JSONStringify(
24
+ data: undefined,
25
+ space?: string | number
26
+ ): undefined;
19
27
 
20
28
  export function JSONParse<T extends Json = Json>(serializedData: string): T;
@@ -3,6 +3,8 @@
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, space) => {
6
+ if (!data) return JSON.stringify(data);
7
+
6
8
  const bigInts = /([\[:])?"(-?\d+)n"([,\}\]])/g;
7
9
  const preliminaryJSON = JSON.stringify(
8
10
  data,
@@ -20,14 +22,28 @@ export const JSONStringify = (data, space) => {
20
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.
21
23
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
22
24
 
23
- Big numbers are found and marked using RegEx with these conditions:
24
- - Before the match there is no . and there is ": OR ":[ OR ":[anyNumberOf(anyCharacters) with no \ before them
25
- - 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
26
- - After the match there is , OR } without " after it OR ] without " after it
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.
27
41
  */
28
42
  export const JSONParse = (json) => {
43
+ if (!json) return JSON.parse(json);
44
+
29
45
  const numbersBiggerThanMaxInt =
30
- /(?<=[^\\]":[\[]?|[^\\]":\[.*[^\.\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;
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;
31
47
  const serializedData = json.replace(numbersBiggerThanMaxInt, `"$1n"`);
32
48
 
33
49
  return JSON.parse(serializedData, (_, value) => {
@@ -1 +1 @@
1
- export const JSONStringify=(t,n)=>JSON.stringify(t,((t,n)=>"bigint"==typeof n?n.toString()+"n":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))};
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "2.2.3",
3
+ "version": "2.4.0",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
5
  "type": "module",
6
6
  "exports": "./json-with-bigint.js",