diff options
Diffstat (limited to 'engine/utils/coding.ts')
-rw-r--r-- | engine/utils/coding.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts new file mode 100644 index 0000000..4c1b17f --- /dev/null +++ b/engine/utils/coding.ts @@ -0,0 +1,27 @@ +const replacer = (_key: any, value: any) => { + if (value instanceof Map) { + return { + dataType: "Map", + value: Array.from(value.entries()), + }; + } else { + return value; + } +}; + +const reviver = (_key: any, value: any) => { + if (typeof value === "object" && value !== null) { + if (value.dataType === "Map") { + return new Map(value.value); + } + } + return value; +}; + +export const stringify = (obj: any) => { + return JSON.stringify(obj, replacer); +}; + +export const parse = <T>(str: string) => { + return JSON.parse(str, reviver) as unknown as T; +}; |