json-web3 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025
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,44 @@
1
+ # json-web3
2
+
3
+ BigInt-safe JSON serialization and deserialization for Web3 data.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add json-web3
9
+ # or
10
+ npm i json-web3
11
+ # or
12
+ yarn add json-web3
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import jsonWeb3 from 'json-web3'
19
+
20
+ const payload = {
21
+ balance: 1234567890123456789n,
22
+ decimals: 18n,
23
+ }
24
+
25
+ const text = jsonWeb3.stringify(payload)
26
+ // => {"balance":{"__@json.bigint__":"1234567890123456789"},"decimals":18n}
27
+
28
+ const restored = jsonWeb3.parse(text)
29
+ /* =>
30
+ {
31
+ "balance": 1234567890123456789n,
32
+ "decimals": 18n
33
+ }
34
+ */
35
+ ```
36
+
37
+ ## API (Fully compatible with native globalThis.JSON)
38
+
39
+ - `stringify(value, replacer?, space?)`
40
+ - `parse(text)`
41
+
42
+ ## Notes
43
+
44
+ `bigint` values are encoded as objects: `{"__@json.bigint__":"<value>"}`. More types can be added later.
package/dist/index.cjs ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => index_default,
24
+ parse: () => parse,
25
+ stringify: () => stringify
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+ var BIGINT_TAG = "__@json.bigint__";
29
+ var RAW_JSON = JSON;
30
+ var applyReplacer = (key, value, replacer) => {
31
+ if (typeof replacer === "function") {
32
+ return replacer(key, value);
33
+ }
34
+ if (Array.isArray(replacer)) {
35
+ if (key === "") return value;
36
+ if (key === BIGINT_TAG) return value;
37
+ return replacer.includes(key) ? value : void 0;
38
+ }
39
+ return value;
40
+ };
41
+ var toSerializable = (value) => typeof value === "bigint" ? { [BIGINT_TAG]: value.toString() } : value;
42
+ var stringify = (value, replacer = null, space) => RAW_JSON.stringify(
43
+ value,
44
+ (key, v) => {
45
+ const replaced = applyReplacer(key, v, replacer);
46
+ return toSerializable(replaced);
47
+ },
48
+ space
49
+ );
50
+ var parse = (text) => RAW_JSON.parse(
51
+ text,
52
+ (_, v) => v && typeof v === "object" && BIGINT_TAG in v ? BigInt(v[BIGINT_TAG]) : v
53
+ );
54
+ var index_default = {
55
+ stringify,
56
+ parse
57
+ };
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ parse,
61
+ stringify
62
+ });
63
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const BIGINT_TAG = '__@json.bigint__'\nconst RAW_JSON = JSON\ntype Replacer = ((this: any, key: string, value: any) => any) | Array<string | number> | null\n\nconst applyReplacer = (key: string, value: any, replacer: Replacer): any => {\n if (typeof replacer === 'function') {\n return replacer(key, value)\n }\n if (Array.isArray(replacer)) {\n if (key === '') return value\n if (key === BIGINT_TAG) return value\n return replacer.includes(key) ? value : undefined\n }\n return value\n}\n\nconst toSerializable = (value: any): any =>\n typeof value === 'bigint' ? { [BIGINT_TAG]: value.toString() } : value\n\nexport const stringify = (value: any, replacer: Replacer = null, space?: string | number): string =>\n RAW_JSON.stringify(\n value,\n (key, v) => {\n const replaced = applyReplacer(key, v, replacer)\n return toSerializable(replaced)\n },\n space,\n )\n\nexport const parse = (text: string): any =>\n RAW_JSON.parse(text, (_, v) =>\n v && typeof v === 'object' && BIGINT_TAG in v ? BigInt(v[BIGINT_TAG]) : v,\n )\n\nexport default {\n stringify,\n parse,\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAM,aAAa;AACnB,IAAM,WAAW;AAGjB,IAAM,gBAAgB,CAAC,KAAa,OAAY,aAA4B;AAC1E,MAAI,OAAO,aAAa,YAAY;AAClC,WAAO,SAAS,KAAK,KAAK;AAAA,EAC5B;AACA,MAAI,MAAM,QAAQ,QAAQ,GAAG;AAC3B,QAAI,QAAQ,GAAI,QAAO;AACvB,QAAI,QAAQ,WAAY,QAAO;AAC/B,WAAO,SAAS,SAAS,GAAG,IAAI,QAAQ;AAAA,EAC1C;AACA,SAAO;AACT;AAEA,IAAM,iBAAiB,CAAC,UACtB,OAAO,UAAU,WAAW,EAAE,CAAC,UAAU,GAAG,MAAM,SAAS,EAAE,IAAI;AAE5D,IAAM,YAAY,CAAC,OAAY,WAAqB,MAAM,UAC/D,SAAS;AAAA,EACP;AAAA,EACA,CAAC,KAAK,MAAM;AACV,UAAM,WAAW,cAAc,KAAK,GAAG,QAAQ;AAC/C,WAAO,eAAe,QAAQ;AAAA,EAChC;AAAA,EACA;AACF;AAEK,IAAM,QAAQ,CAAC,SACpB,SAAS;AAAA,EAAM;AAAA,EAAM,CAAC,GAAG,MACvB,KAAK,OAAO,MAAM,YAAY,cAAc,IAAI,OAAO,EAAE,UAAU,CAAC,IAAI;AAC1E;AAEF,IAAO,gBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":[]}
@@ -0,0 +1,9 @@
1
+ type Replacer = ((this: any, key: string, value: any) => any) | Array<string | number> | null;
2
+ declare const stringify: (value: any, replacer?: Replacer, space?: string | number) => string;
3
+ declare const parse: (text: string) => any;
4
+ declare const _default: {
5
+ stringify: (value: any, replacer?: Replacer, space?: string | number) => string;
6
+ parse: (text: string) => any;
7
+ };
8
+
9
+ export { _default as default, parse, stringify };
@@ -0,0 +1,9 @@
1
+ type Replacer = ((this: any, key: string, value: any) => any) | Array<string | number> | null;
2
+ declare const stringify: (value: any, replacer?: Replacer, space?: string | number) => string;
3
+ declare const parse: (text: string) => any;
4
+ declare const _default: {
5
+ stringify: (value: any, replacer?: Replacer, space?: string | number) => string;
6
+ parse: (text: string) => any;
7
+ };
8
+
9
+ export { _default as default, parse, stringify };
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ // src/index.ts
2
+ var BIGINT_TAG = "__@json.bigint__";
3
+ var RAW_JSON = JSON;
4
+ var applyReplacer = (key, value, replacer) => {
5
+ if (typeof replacer === "function") {
6
+ return replacer(key, value);
7
+ }
8
+ if (Array.isArray(replacer)) {
9
+ if (key === "") return value;
10
+ if (key === BIGINT_TAG) return value;
11
+ return replacer.includes(key) ? value : void 0;
12
+ }
13
+ return value;
14
+ };
15
+ var toSerializable = (value) => typeof value === "bigint" ? { [BIGINT_TAG]: value.toString() } : value;
16
+ var stringify = (value, replacer = null, space) => RAW_JSON.stringify(
17
+ value,
18
+ (key, v) => {
19
+ const replaced = applyReplacer(key, v, replacer);
20
+ return toSerializable(replaced);
21
+ },
22
+ space
23
+ );
24
+ var parse = (text) => RAW_JSON.parse(
25
+ text,
26
+ (_, v) => v && typeof v === "object" && BIGINT_TAG in v ? BigInt(v[BIGINT_TAG]) : v
27
+ );
28
+ var index_default = {
29
+ stringify,
30
+ parse
31
+ };
32
+ export {
33
+ index_default as default,
34
+ parse,
35
+ stringify
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const BIGINT_TAG = '__@json.bigint__'\nconst RAW_JSON = JSON\ntype Replacer = ((this: any, key: string, value: any) => any) | Array<string | number> | null\n\nconst applyReplacer = (key: string, value: any, replacer: Replacer): any => {\n if (typeof replacer === 'function') {\n return replacer(key, value)\n }\n if (Array.isArray(replacer)) {\n if (key === '') return value\n if (key === BIGINT_TAG) return value\n return replacer.includes(key) ? value : undefined\n }\n return value\n}\n\nconst toSerializable = (value: any): any =>\n typeof value === 'bigint' ? { [BIGINT_TAG]: value.toString() } : value\n\nexport const stringify = (value: any, replacer: Replacer = null, space?: string | number): string =>\n RAW_JSON.stringify(\n value,\n (key, v) => {\n const replaced = applyReplacer(key, v, replacer)\n return toSerializable(replaced)\n },\n space,\n )\n\nexport const parse = (text: string): any =>\n RAW_JSON.parse(text, (_, v) =>\n v && typeof v === 'object' && BIGINT_TAG in v ? BigInt(v[BIGINT_TAG]) : v,\n )\n\nexport default {\n stringify,\n parse,\n}\n"],"mappings":";AAAA,IAAM,aAAa;AACnB,IAAM,WAAW;AAGjB,IAAM,gBAAgB,CAAC,KAAa,OAAY,aAA4B;AAC1E,MAAI,OAAO,aAAa,YAAY;AAClC,WAAO,SAAS,KAAK,KAAK;AAAA,EAC5B;AACA,MAAI,MAAM,QAAQ,QAAQ,GAAG;AAC3B,QAAI,QAAQ,GAAI,QAAO;AACvB,QAAI,QAAQ,WAAY,QAAO;AAC/B,WAAO,SAAS,SAAS,GAAG,IAAI,QAAQ;AAAA,EAC1C;AACA,SAAO;AACT;AAEA,IAAM,iBAAiB,CAAC,UACtB,OAAO,UAAU,WAAW,EAAE,CAAC,UAAU,GAAG,MAAM,SAAS,EAAE,IAAI;AAE5D,IAAM,YAAY,CAAC,OAAY,WAAqB,MAAM,UAC/D,SAAS;AAAA,EACP;AAAA,EACA,CAAC,KAAK,MAAM;AACV,UAAM,WAAW,cAAc,KAAK,GAAG,QAAQ;AAC/C,WAAO,eAAe,QAAQ;AAAA,EAChC;AAAA,EACA;AACF;AAEK,IAAM,QAAQ,CAAC,SACpB,SAAS;AAAA,EAAM;AAAA,EAAM,CAAC,GAAG,MACvB,KAAK,OAAO,MAAM,YAAY,cAAc,IAAI,OAAO,EAAE,UAAU,CAAC,IAAI;AAC1E;AAEF,IAAO,gBAAQ;AAAA,EACb;AAAA,EACA;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "json-web3",
3
+ "version": "0.1.0",
4
+ "description": "BigInt-safe JSON serialization and deserialization for Web3 use cases.",
5
+ "keywords": [
6
+ "json",
7
+ "bigint",
8
+ "serialization",
9
+ "deserialization",
10
+ "web3"
11
+ ],
12
+ "license": "MIT",
13
+ "sideEffects": false,
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "files": [
26
+ "dist",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "devDependencies": {
31
+ "@types/node": "^25.0.9",
32
+ "prettier": "^3.8.0",
33
+ "tsup": "^8.5.1",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^4.0.17"
36
+ },
37
+ "engines": {
38
+ "node": ">=18"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup",
42
+ "lint": "prettier . --write",
43
+ "test": "vitest run",
44
+ "test:watch": "vitest"
45
+ }
46
+ }