chore: 添加 Map 序列化相关辅助类

This commit is contained in:
CrescentLeaf
2025-10-04 14:07:21 +08:00
parent e4cf9d6a68
commit 39b4a6d8a6

20
client/MapJson.ts Normal file
View File

@@ -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
}
}