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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
import { PropSchema, serialize, deserialize, custom, setDefaultModelSchema, getDefaultModelSchema } from 'serializr';
import Context from 'serializr/lib/core/Context';
// import { Field } from '../../fields/Doc';
let serializing = 0;
export function afterDocDeserialize(cb: (err: unknown, val: unknown) => void, err: unknown, newValue: unknown) {
serializing++;
cb(err, newValue);
serializing--;
}
const serializationTypes: { [name: string]: { ctor: { new (): unknown }; afterDeserialize?: (obj: unknown) => void | Promise<unknown> } } = {};
const reverseMap: { [ctor: string]: string } = {};
export namespace SerializationHelper {
export function IsSerializing() {
return serializing > 0;
}
export function Serialize(obj: unknown /* Field */): unknown {
if (obj === undefined || obj === null) {
return null;
}
if (typeof obj !== 'object') {
return obj;
}
serializing++;
if (!(obj.constructor.name in reverseMap)) {
serializing--;
throw Error(`Error: type '${obj.constructor.name}' not registered. Make sure you register it using a @Deserializable decorator`);
}
const json = serialize(obj);
json.__type = reverseMap[obj.constructor.name];
serializing--;
return json;
}
export async function Deserialize(obj: unknown): Promise<unknown> {
if (obj === undefined || obj === null) {
return undefined;
}
if (typeof obj !== 'object') {
return obj;
}
const objtype = '__type' in obj ? (obj.__type as string) : undefined;
if (!objtype) {
console.warn(`No property ${objtype} found in JSON.`);
return undefined;
}
if (!(objtype in serializationTypes)) {
throw Error(`type '${objtype}' not registered. Make sure you register it using a @Deserializable decorator`);
}
const type = serializationTypes[objtype];
const value = await new Promise(res => {
deserialize(type.ctor, obj, (err, result) => res(result));
});
type.afterDeserialize?.(value);
return value;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function Deserializable(classNameForSerializer: string, afterDeserialize?: (obj: unknown) => void | Promise<unknown>, constructorArgs?: [string]): (constructor: { new (...args: any[]): any }) => void {
function addToMap(className: string, Ctor: { new (...args: unknown[]): unknown }) {
const schema = getDefaultModelSchema(Ctor);
if (schema && (schema.targetClass !== Ctor || constructorArgs)) {
setDefaultModelSchema(Ctor, { ...schema, factory: (context: Context) => new Ctor(...(constructorArgs ?? []).map(arg => context.json[arg])) });
}
if (!(className in serializationTypes)) {
serializationTypes[className] = { ctor: Ctor, afterDeserialize };
reverseMap[Ctor.name] = className;
} else {
throw new Error(`Name ${className} has already been registered as deserializable`);
}
}
return (ctor: { new (...args: unknown[]): unknown }) => addToMap(classNameForSerializer, ctor);
}
export function autoObject(): PropSchema {
return custom(
s => SerializationHelper.Serialize(s),
(json: object, context: Context, oldValue: unknown, cb: (err: unknown, result: unknown) => void) => SerializationHelper.Deserialize(json).then(res => cb(null, res))
);
}
|