-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: json reviver and replacer utils (#396)
- Loading branch information
1 parent
223d9da
commit e5679f7
Showing
5 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { AnonymousIdentity } from "@dfinity/agent"; | ||
import { Principal } from "@dfinity/principal"; | ||
import { arrayOfNumberToUint8Array } from "@dfinity/utils"; | ||
import { jsonReplacer, jsonReviver } from "./json.utils"; | ||
|
||
describe("json-utils", () => { | ||
describe("stringify", () => { | ||
it("should stringify bigint with a custom representation", () => { | ||
expect(JSON.stringify(123n, jsonReplacer)).toEqual( | ||
'{"__bigint__":"123"}', | ||
); | ||
expect(JSON.stringify({ value: 123n }, jsonReplacer)).toEqual( | ||
'{"value":{"__bigint__":"123"}}', | ||
); | ||
}); | ||
|
||
it("should stringify Principal with a custom representation", () => { | ||
const principal = new AnonymousIdentity().getPrincipal(); | ||
|
||
expect(JSON.stringify(principal, jsonReplacer)).toEqual( | ||
'{"__principal__":"2vxsx-fae"}', | ||
); | ||
expect(JSON.stringify({ principal }, jsonReplacer)).toEqual( | ||
'{"principal":{"__principal__":"2vxsx-fae"}}', | ||
); | ||
|
||
const rootCanisterId = Principal.fromText("tmxop-wyaaa-aaaaa-aaapa-cai"); | ||
|
||
expect(JSON.stringify(rootCanisterId, jsonReplacer)).toEqual( | ||
'{"__principal__":"tmxop-wyaaa-aaaaa-aaapa-cai"}', | ||
); | ||
expect( | ||
JSON.stringify({ principal: rootCanisterId }, jsonReplacer), | ||
).toEqual( | ||
'{"principal":{"__principal__":"tmxop-wyaaa-aaaaa-aaapa-cai"}}', | ||
); | ||
}); | ||
|
||
it("should stringify Uint8Array with a custom representation", () => { | ||
const arr = arrayOfNumberToUint8Array([1, 2, 3]); | ||
|
||
expect(JSON.stringify(arr, jsonReplacer)).toEqual( | ||
'{"__uint8array__":[1,2,3]}', | ||
); | ||
expect(JSON.stringify({ arr }, jsonReplacer)).toEqual( | ||
'{"arr":{"__uint8array__":[1,2,3]}}', | ||
); | ||
}); | ||
}); | ||
|
||
describe("parse", () => { | ||
it("should parse bigint from a custom representation", () => { | ||
expect(JSON.parse('{"__bigint__":"123"}', jsonReviver)).toEqual(123n); | ||
expect(JSON.parse('{"value":{"__bigint__":"123"}}', jsonReviver)).toEqual( | ||
{ value: 123n }, | ||
); | ||
}); | ||
|
||
it("should parse principal from a custom representation", () => { | ||
const principal = new AnonymousIdentity().getPrincipal(); | ||
|
||
expect(JSON.parse('{"__principal__":"2vxsx-fae"}', jsonReviver)).toEqual( | ||
principal, | ||
); | ||
expect( | ||
JSON.parse('{"principal":{"__principal__":"2vxsx-fae"}}', jsonReviver), | ||
).toEqual({ principal }); | ||
|
||
const rootCanisterId = Principal.fromText("tmxop-wyaaa-aaaaa-aaapa-cai"); | ||
|
||
expect( | ||
JSON.parse( | ||
'{"__principal__":"tmxop-wyaaa-aaaaa-aaapa-cai"}', | ||
jsonReviver, | ||
), | ||
).toEqual(rootCanisterId); | ||
expect( | ||
JSON.parse( | ||
'{"principal":{"__principal__":"tmxop-wyaaa-aaaaa-aaapa-cai"}}', | ||
jsonReviver, | ||
), | ||
).toEqual({ principal: rootCanisterId }); | ||
}); | ||
|
||
it("should parse principal to object", () => { | ||
const obj = JSON.parse( | ||
'{"__principal__":"tmxop-wyaaa-aaaaa-aaapa-cai"}', | ||
jsonReviver, | ||
); | ||
expect(obj instanceof Principal).toBeTruthy(); | ||
expect((obj as Principal).toText()).toEqual( | ||
"tmxop-wyaaa-aaaaa-aaapa-cai", | ||
); | ||
}); | ||
|
||
it("should parse Uint8Array from a custom representation", () => { | ||
const arr = arrayOfNumberToUint8Array([1, 2, 3]); | ||
|
||
expect(JSON.parse('{"__uint8array__":[1,2,3]}', jsonReviver)).toEqual( | ||
arr, | ||
); | ||
expect( | ||
JSON.parse('{"arr":{"__uint8array__":[1,2,3]}}', jsonReviver), | ||
).toEqual({ arr }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Principal } from "@dfinity/principal"; | ||
import { nonNullish } from "./nullish.utils"; | ||
|
||
const JSON_KEY_BIGINT = "__bigint__"; | ||
const JSON_KEY_PRINCIPAL = "__principal__"; | ||
const JSON_KEY_UINT8ARRAY = "__uint8array__"; | ||
|
||
/** | ||
* A parser that interprets revived BigInt, Principal, and Uint8Array when constructing JavaScript values or objects. | ||
*/ | ||
export const jsonReplacer = (_key: string, value: unknown): unknown => { | ||
if (typeof value === "bigint") { | ||
return { [JSON_KEY_BIGINT]: `${value}` }; | ||
} | ||
|
||
if (nonNullish(value) && value instanceof Principal) { | ||
return { [JSON_KEY_PRINCIPAL]: value.toText() }; | ||
} | ||
|
||
if (nonNullish(value) && value instanceof Uint8Array) { | ||
return { [JSON_KEY_UINT8ARRAY]: Array.from(value) }; | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
/** | ||
* A function that alters the behavior of the stringification process for BigInt, Principal and Uint8Array. | ||
*/ | ||
export const jsonReviver = (_key: string, value: unknown): unknown => { | ||
const mapValue = <T>(key: string): T => (value as Record<string, T>)[key]; | ||
|
||
if ( | ||
nonNullish(value) && | ||
typeof value === "object" && | ||
JSON_KEY_BIGINT in value | ||
) { | ||
return BigInt(mapValue(JSON_KEY_BIGINT)); | ||
} | ||
|
||
if ( | ||
nonNullish(value) && | ||
typeof value === "object" && | ||
JSON_KEY_PRINCIPAL in value | ||
) { | ||
return Principal.fromText(mapValue(JSON_KEY_PRINCIPAL)); | ||
} | ||
|
||
if ( | ||
nonNullish(value) && | ||
typeof value === "object" && | ||
JSON_KEY_UINT8ARRAY in value | ||
) { | ||
return Uint8Array.from(mapValue(JSON_KEY_UINT8ARRAY)); | ||
} | ||
|
||
return value; | ||
}; |