Spaces:
Sleeping
Sleeping
File size: 647 Bytes
d605f27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
const recoverJSON = (json: string | object, classDict) => {
if (typeof json === "object")
json = JSON.stringify(json);
return JSON.parse(json, (_, value) => {
if (value && (typeof value === "object") && value.__prototype) {
const Class = classDict[value.__prototype];
if (Class) {
const {__prototype, ...fields} = value;
return new Class(fields);
}
}
return value;
});
};
class SimpleClass {
constructor (data?: object) {
if (data)
Object.assign(this, data);
}
toJSON () {
return {
__prototype: (this.constructor as any).className,
...this,
};
}
};
export {
recoverJSON,
SimpleClass,
};
|