json-with-bigint 1.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/LICENSE +21 -0
- package/README.md +25 -0
- package/json-with-bigint.d.ts +3 -0
- package/json-with-bigint.js +11 -0
- package/json-with-bigint.min.js +1 -0
- package/package.json +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Ivan Korolenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# JSON with BigInt
|
|
2
|
+
|
|
3
|
+
JS library that allows you to easily serialize and deserialize data with BigInt values
|
|
4
|
+
|
|
5
|
+
## Why would I need JSON-with-BigInt?
|
|
6
|
+
|
|
7
|
+
3 reasons:
|
|
8
|
+
|
|
9
|
+
1. You need to convert some data to/from JSON and it includes BigInt values
|
|
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)
|
|
12
|
+
|
|
13
|
+
## Good things about JSON-with-BigInt
|
|
14
|
+
|
|
15
|
+
✔️ Supports consistent round-trip operations with JSON.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
JSONParse(JSONStringify(9007199254740991n)) === 9007199254740991n // true
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
✔️ Can be used in a TypeScript project (.d.ts file included)
|
|
22
|
+
|
|
23
|
+
✔️ Size: 164 bytes (minified and gzipped)
|
|
24
|
+
|
|
25
|
+
✔️ No dependencies
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const JSONStringify = (data) =>
|
|
2
|
+
JSON.stringify(data, (_, value) =>
|
|
3
|
+
typeof value === "bigint" ? value.toString() + "n" : value
|
|
4
|
+
);
|
|
5
|
+
|
|
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
|
+
);
|
|
@@ -0,0 +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);
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "json-with-bigint",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "JS library that allows you to easily serialize and deserialize data with BigInt values",
|
|
5
|
+
"main": "json-with-bigint.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": ""
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "Ivan Korolenko <iam@ivankorolenko.com>",
|
|
12
|
+
"license": "MIT"
|
|
13
|
+
}
|