Skip to content

Commit

Permalink
handle undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Jan 24, 2025
1 parent 46dede6 commit af7f6c4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/stable/stable_structures/stable_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export const stableJson = StableJson();
* @returns The converted value safe for JSON string serialization
*/
export function jsonReplacer(_key: string, value: any): any {
if (typeof value === 'undefined') {
return {
__undefined__: '__undefined__'
};
}

if (typeof value === 'bigint') {
return {
__bigint__: value.toString()
Expand Down Expand Up @@ -153,6 +159,10 @@ export function jsonReplacer(_key: string, value: any): any {
*/
export function jsonReviver(_key: string, value: any): any {
if (typeof value === 'object' && value !== null) {
if (typeof value.__undefined__ === 'string') {
return undefined;
}

if (typeof value.__bigint__ === 'string') {
return BigInt(value.__bigint__);
}
Expand Down

0 comments on commit af7f6c4

Please sign in to comment.