aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/SerializationHelper.ts
blob: 656101c95c0fc38a5353599eaa1dffaac6fddc76 (plain)
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
import { PropSchema, serialize, deserialize, custom } from "serializr";
import { Field } from "../../fields/NewDoc";

export class SerializationHelper {

    public static Serialize(obj: Field): any {
        if (!obj) {
            return null;
        }

        if (typeof obj !== 'object') {
            return obj;
        }

        if (!(obj.constructor.name in reverseMap)) {
            throw 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];
        return json;
    }

    public static Deserialize(obj: any): any {
        if (!obj) {
            return null;
        }

        if (typeof obj !== 'object') {
            return obj;
        }

        if (!obj.__type) {
            throw Error("No property 'type' found in JSON.");
        }

        if (!(obj.__type in serializationTypes)) {
            throw Error(`type '${obj.__type}' not registered. Make sure you register it using a @Deserializable decorator`);
        }

        return deserialize(serializationTypes[obj.__type], obj);
    }
}

let serializationTypes: { [name: string]: any } = {};
let reverseMap: { [ctor: string]: string } = {};

export function Deserializable(name: string): Function;
export function Deserializable(constructor: Function): void;
export function Deserializable(constructor: Function | string): Function | void {
    function addToMap(name: string, ctor: Function) {
        if (!(name in serializationTypes)) {
            serializationTypes[name] = constructor;
            reverseMap[ctor.name] = name;
        } else {
            throw new Error(`Name ${name} has already been registered as deserializable`);
        }
    }
    if (typeof constructor === "string") {
        return (ctor: Function) => {
            addToMap(constructor, ctor);
        };
    }
    addToMap(constructor.name, constructor);
}

export function autoObject(): PropSchema {
    return custom(
        (s) => SerializationHelper.Serialize(s),
        (s) => SerializationHelper.Deserialize(s)
    );
}