forked from emcastro/ts-json-serializer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Serializable.ts
50 lines (47 loc) · 1.79 KB
/
Serializable.ts
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
import { SerializableType } from './SerializableType';
import { Resolver } from './Resolver';
import { NoFactoryProvidedError, NoNameProvided } from './errors';
/**
* Interface for the options object that can be used to configure the type in the resolver.
*
* @export
* @interface SerializableOptions
*/
export interface SerializableOptions<T> {
/**
* The name of the type. When the class mangled or function.name does not work,
* you should add a name for the resolver.
*
* @type {string}
* @memberOf SerializableOptions
*/
name?: string;
/**
* Factory function of the type. Must be used when the class has no default, parameterless constructor.
* Does receive the json / plain object version of the object and should instantiate the effective type.
*
* @type {(json: any) => T}
* @memberOf SerializableOptions
*/
factory?: (json: any) => T;
};
/**
* Class decorator for serializable types. Those registered type can be translated into the transport-json
* structure. All types (with exception of the primitive and date types) must be registered to be transportable.
*
* @export
* @param {SerializableOptions} [options] Configuration object for the type, contains an optional name and factory.
* @returns {ClassDecorator}
*/
export function Serializable<T>(options?: SerializableOptions<T>): ClassDecorator {
return (type: Function) => {
const name = options && options.name ? options.name : (type as any).name;
if (type.length > 0 && (!options || !options.factory)) {
throw new NoFactoryProvidedError(name);
}
if (!name) {
throw new NoNameProvided();
}
Resolver.instance.addType(new SerializableType(name, type, options ? options.factory : undefined));
};
}