chore: 添加 Map 序列化相关辅助类
This commit is contained in:
20
client/MapJson.ts
Normal file
20
client/MapJson.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user