json-with-bigint 2.4.1 → 3.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 +12 -6
- package/__tests__/performance.js +29 -0
- package/__tests__/performance.json +1319112 -0
- package/__tests__/unit.js +137 -0
- package/json-with-bigint.cjs +84 -0
- package/json-with-bigint.js +55 -31
- package/json-with-bigint.min.js +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ 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 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
|
|
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 specify which properties in JSON include BigInt values, or 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
|
|
|
@@ -20,9 +20,13 @@ JSONStringify(data) // '{"bigNumber":9007199254740992}'
|
|
|
20
20
|
JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
✔️ No need to specify which properties in JSON include BigInt values. Library will find them itself
|
|
24
|
+
|
|
23
25
|
✔️ No need to change your JSON or the way you want to work with your data
|
|
24
26
|
|
|
25
|
-
✔️
|
|
27
|
+
✔️ 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())
|
|
28
|
+
|
|
29
|
+
✔️ Parses and stringifies all other values other than big numbers the same way as native JSON methods in JS do. You can just replace every `JSON.parse` and `JSON.stringify` call in your project with functions from this library and it'll work.
|
|
26
30
|
|
|
27
31
|
✔️ Correctly parses float numbers and negative numbers
|
|
28
32
|
|
|
@@ -30,14 +34,16 @@ JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true
|
|
|
30
34
|
|
|
31
35
|
✔️ Does not contaminate your global space (unlike monkey-patching solution)
|
|
32
36
|
|
|
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())
|
|
34
|
-
|
|
35
37
|
✔️ Can be used in both JavaScript and TypeScript projects (.d.ts file included)
|
|
36
38
|
|
|
37
|
-
✔️
|
|
39
|
+
✔️ Can be used as both ESM and CommonJS module
|
|
40
|
+
|
|
41
|
+
✔️ Size: 624 bytes (minified and gzipped)
|
|
38
42
|
|
|
39
43
|
✔️ No dependencies
|
|
40
44
|
|
|
45
|
+
✔️ Covered by tests
|
|
46
|
+
|
|
41
47
|
## Getting Started
|
|
42
48
|
|
|
43
49
|
This library has no default export. [Why it's a good thing](https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/)
|
|
@@ -111,7 +117,7 @@ Download json-with-bigint.min.js from this repository to your project's folder a
|
|
|
111
117
|
|
|
112
118
|
Examples:
|
|
113
119
|
|
|
114
|
-
- `JSONParse('{"someBigNumber":
|
|
120
|
+
- `JSONParse('{"someBigNumber":9007199254740992}')`
|
|
115
121
|
- `JSONStringify({
|
|
116
122
|
someBigNumber: 9007199254740992n
|
|
117
123
|
})`
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// ------ Performance tests ------
|
|
2
|
+
const { performance } = require("perf_hooks");
|
|
3
|
+
const { JSONParse, JSONStringify } = require("../json-with-bigint.cjs");
|
|
4
|
+
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
|
|
7
|
+
const measureExecTime = (fn) => {
|
|
8
|
+
const startTime = performance.now();
|
|
9
|
+
|
|
10
|
+
fn();
|
|
11
|
+
|
|
12
|
+
const endTime = performance.now();
|
|
13
|
+
|
|
14
|
+
console.log("Time: ", endTime - startTime);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const performanceTest1 = fs.readFileSync("__tests__/performance.json", "utf8");
|
|
18
|
+
|
|
19
|
+
measureExecTime(() => {
|
|
20
|
+
console.log("___________");
|
|
21
|
+
console.log("Performance test 1. One-way");
|
|
22
|
+
JSONParse(performanceTest1);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
measureExecTime(() => {
|
|
26
|
+
console.log("___________");
|
|
27
|
+
console.log("Performance test 1. Round-trip");
|
|
28
|
+
JSONStringify(JSONParse(performanceTest1));
|
|
29
|
+
});
|