diff --git a/client/MapJson.ts b/client/MapJson.ts new file mode 100644 index 0000000..be64c19 --- /dev/null +++ b/client/MapJson.ts @@ -0,0 +1,20 @@ +// https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map + +export default class MapJson { + static replacer(key: unknown, value: unknown) { + if (value instanceof Map) { + return { + dataType: 'Map', + value: Array.from(value.entries()), // or with spread: value: [...value] + } + } else { + return value + } + } + static reviver(key: unknown, value: any) { + if (value?.dataType === 'Map') { + return new Map(value.value) + } + return value + } +} \ No newline at end of file