json-with-bigint 3.1.1 → 3.2.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 CHANGED
@@ -40,7 +40,7 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
40
40
 
41
41
  ✔️ Can be used as both ESM and CommonJS module
42
42
 
43
- ✔️ Size: 624 bytes (minified and gzipped)
43
+ ✔️ Size: 684 bytes (minified and gzipped)
44
44
 
45
45
  ✔️ No dependencies
46
46
 
@@ -1,15 +1,27 @@
1
1
  const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
2
+ const originalStringify = JSON.stringify;
3
+ const originalParse = JSON.parse;
2
4
 
3
5
  /*
4
6
  Function to serialize data to a JSON string.
5
7
  Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
6
8
  */
7
9
  const JSONStringify = (data, space) => {
8
- if (!data) return JSON.stringify(data);
10
+ if ("rawJSON" in JSON) {
11
+ return originalStringify(
12
+ data,
13
+ (_, value) => {
14
+ return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
15
+ },
16
+ space
17
+ );
18
+ }
19
+
20
+ if (!data) return originalStringify(data);
9
21
 
10
22
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
11
23
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
12
- const convertedToCustomJSON = JSON.stringify(
24
+ const convertedToCustomJSON = originalStringify(
13
25
  data,
14
26
  (_, value) => {
15
27
  const isNoise =
@@ -33,7 +45,7 @@ const JSONStringify = (data, space) => {
33
45
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
34
46
  */
35
47
  const JSONParse = (json) => {
36
- if (!json) return JSON.parse(json);
48
+ if (!json) return originalParse(json);
37
49
 
38
50
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
39
51
  const MAX_DIGITS = MAX_INT.length;
@@ -65,7 +77,7 @@ const JSONParse = (json) => {
65
77
  );
66
78
 
67
79
  // Convert marked big numbers to BigInt
68
- return JSON.parse(serializedData, (_, value) => {
80
+ return originalParse(serializedData, (_, value) => {
69
81
  const isCustomFormatBigInt =
70
82
  typeof value === "string" && Boolean(value.match(customFormat));
71
83
 
@@ -0,0 +1,28 @@
1
+ type JsonObject = {
2
+ [x: string]: Json;
3
+ };
4
+
5
+ type JsonArray = Json[];
6
+
7
+ export type Json =
8
+ | null
9
+ | undefined
10
+ | string
11
+ | number
12
+ | bigint
13
+ | boolean
14
+ | JsonObject
15
+ | {}
16
+ | JsonArray;
17
+
18
+ export function JSONStringify(
19
+ data: Exclude<Json, undefined>,
20
+ space?: string | number
21
+ ): string;
22
+
23
+ export function JSONStringify(
24
+ data: undefined,
25
+ space?: string | number
26
+ ): undefined;
27
+
28
+ export function JSONParse<T extends Json = Json>(serializedData: string): T;
@@ -1,15 +1,27 @@
1
1
  const noiseValue = /^-?\d+n+$/; // Noise - strings that match the custom format before being converted to it
2
+ const originalStringify = JSON.stringify;
3
+ const originalParse = JSON.parse;
2
4
 
3
5
  /*
4
6
  Function to serialize data to a JSON string.
5
7
  Converts BigInt values to a custom format (strings with digits and "n" at the end) and then converts them to proper big integers in a JSON string.
6
8
  */
7
9
  export const JSONStringify = (data, space) => {
8
- if (!data) return JSON.stringify(data);
10
+ if ("rawJSON" in JSON) {
11
+ return originalStringify(
12
+ data,
13
+ (_, value) => {
14
+ return typeof value === "bigint" ? JSON.rawJSON(value.toString()) : value
15
+ },
16
+ space
17
+ );
18
+ }
19
+
20
+ if (!data) return originalStringify(data);
9
21
 
10
22
  const bigInts = /([\[:])?"(-?\d+)n"($|[,\}\]])/g;
11
23
  const noise = /([\[:])?("-?\d+n+)n("$|"[,\}\]])/g;
12
- const convertedToCustomJSON = JSON.stringify(
24
+ const convertedToCustomJSON = originalStringify(
13
25
  data,
14
26
  (_, value) => {
15
27
  const isNoise =
@@ -33,7 +45,7 @@ export const JSONStringify = (data, space) => {
33
45
  Other types of values are not affected and parsed as native JSON.parse() would parse them.
34
46
  */
35
47
  export const JSONParse = (json) => {
36
- if (!json) return JSON.parse(json);
48
+ if (!json) return originalParse(json);
37
49
 
38
50
  const MAX_INT = Number.MAX_SAFE_INTEGER.toString();
39
51
  const MAX_DIGITS = MAX_INT.length;
@@ -65,7 +77,7 @@ export const JSONParse = (json) => {
65
77
  );
66
78
 
67
79
  // Convert marked big numbers to BigInt
68
- return JSON.parse(serializedData, (_, value) => {
80
+ return originalParse(serializedData, (_, value) => {
69
81
  const isCustomFormatBigInt =
70
82
  typeof value === "string" && Boolean(value.match(customFormat));
71
83
 
@@ -1 +1 @@
1
- const noiseValue=/^-?\d+n+$/;export const JSONStringify=(n,t)=>{if(!n)return JSON.stringify(n);return JSON.stringify(n,((n,t)=>"string"==typeof t&&Boolean(t.match(noiseValue))||"bigint"==typeof t?t.toString()+"n":t),t).replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return JSON.parse(n);const t=Number.MAX_SAFE_INTEGER.toString(),e=t.length,r=/^"-?\d+n+"$/,o=/^-?\d+n$/,i=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,o,i,s)=>{const g='"'===n[0];if(g&&Boolean(n.match(r)))return n.substring(0,n.length-1)+'n"';const a=i||s,l=o&&(o.length<e||o.length===e&&o<=t);return g||a||l?n:'"'+n+'n"'}));return JSON.parse(i,((n,t)=>{if("string"==typeof t&&Boolean(t.match(o)))return BigInt(t.substring(0,t.length-1));return"string"==typeof t&&Boolean(t.match(noiseValue))?t.substring(0,t.length-1):t}))};
1
+ const noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse;export const JSONStringify=(n,r)=>{if("rawJSON"in JSON)return originalStringify(n,((n,r)=>"bigint"==typeof r?JSON.rawJSON(r.toString()):r),r);if(!n)return originalStringify(n);return originalStringify(n,((n,r)=>"string"==typeof r&&Boolean(r.match(noiseValue))||"bigint"==typeof r?r.toString()+"n":r),r).replace(/([\[:])?"(-?\d+)n"($|[,\}\]])/g,"$1$2$3").replace(/([\[:])?("-?\d+n+)n("$|"[,\}\]])/g,"$1$2$3")};export const JSONParse=n=>{if(!n)return originalParse(n);const r=Number.MAX_SAFE_INTEGER.toString(),t=r.length,i=/^"-?\d+n+"$/,e=/^-?\d+n$/,g=n.replace(/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,((n,e,g,o)=>{const a='"'===n[0];if(a&&Boolean(n.match(i)))return n.substring(0,n.length-1)+'n"';const s=g||o,l=e&&(e.length<t||e.length===t&&e<=r);return a||s||l?n:'"'+n+'n"'}));return originalParse(g,((n,r)=>{if("string"==typeof r&&Boolean(r.match(e)))return BigInt(r.substring(0,r.length-1));return"string"==typeof r&&Boolean(r.match(noiseValue))?r.substring(0,r.length-1):r}))};
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "json-with-bigint",
3
- "version": "3.1.1",
3
+ "version": "3.2.2",
4
4
  "description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "import": "./json-with-bigint.js",
8
8
  "require": "./json-with-bigint.cjs"
9
9
  },
10
+ "scripts": {
11
+ "performance": "node __tests__/performance.cjs",
12
+ "unit": "node __tests__/unit.cjs"
13
+ },
10
14
  "repository": {
11
15
  "type": "git",
12
16
  "url": "https://github.com/Ivan-Korolenko/json-with-bigint"
File without changes
File without changes