-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
deserialize.ts
170 lines (136 loc) · 4.94 KB
/
deserialize.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {isArray, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from "@tsed/core";
import {getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from "@tsed/schema";
import "../components";
import {JsonMapperContext} from "../domain/JsonMapperContext";
import {getJsonMapperTypes} from "../domain/JsonMapperTypesContainer";
import {JsonMapperMethods} from "../interfaces/JsonMapperMethods";
export interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {
/**
* Types used to map complex types (Symbol, Array, Set, Map)
*/
types?: Map<Type<any>, JsonMapperMethods>;
/**
* useAlias mapping
*/
useAlias?: boolean;
/**
* Accept additionalProperties or ignore it
*/
additionalProperties?: boolean;
/**
* Use the store which have all metadata to deserialize correctly the model. This
* property is useful when you deal with metadata parameters.
*/
store?: JsonEntityStore;
[key: string]: any;
}
function isDeserializable(obj: any, options: JsonDeserializerOptions) {
if ((!!options.collectionType && isNil(obj)) || obj === undefined) {
return false;
}
return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));
}
function alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {
return schema.$hooks.alter("onDeserialize", value, [options]);
}
function transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {
const {types, type = Object, collectionType} = options;
const context = new JsonMapperContext({
type,
collectionType,
options,
next: (data, {collectionType, ...options}) => deserialize(data, options)
});
return types?.get(options.collectionType)?.deserialize<T>(src, context);
}
function transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {
const {types, type = Object} = options;
const context = new JsonMapperContext({
type,
options,
next: (data, {type, ...options}) => deserialize(data, options)
});
return types?.get(type)?.deserialize<T>(src, context);
}
/**
* Transform given plain object to class.
* @param src
* @param options
*/
export function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {
if (isEmpty(src)) {
return src;
}
const {type, store = JsonEntityStore.from(type), ...next} = options;
const propertiesMap = getPropertiesStores(store);
let keys = objectKeys(src);
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
const out: any = new type(src);
propertiesMap.forEach((propStore) => {
const key = propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName;
keys = keys.filter((k) => k !== key);
let value = alterValue(propStore.schema, src[key], {...options, self: src});
next.type = propStore.computedType;
if (propStore.schema.hasGenerics) {
next.nestedGenerics = propStore.schema.nestedGenerics;
} else if (propStore.schema.isGeneric && options.nestedGenerics) {
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
const genericLabels = propStore.parent.schema.genericLabels || [];
next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
next.nestedGenerics = nestedGenerics;
}
value = deserialize(value, {
...next,
collectionType: propStore.collectionType
});
if (value !== undefined) {
out[propStore.propertyName] = value;
}
});
if (additionalProperties) {
keys.forEach((key) => {
out[key] = src[key];
});
}
return out;
}
function buildOptions(options: JsonDeserializerOptions<any, any>): any {
if (options.store instanceof JsonEntityStore) {
if (options.store.parameter && options.store.parameter.nestedGenerics.length) {
options.nestedGenerics = options.store.parameter.nestedGenerics;
}
options.type = options.store.computedType;
options.collectionType = options.store.collectionType;
delete options.store;
}
return {
...options,
types: options.types ? options.types : getJsonMapperTypes()
};
}
/**
* Transform given source to class base on the given `options.type`.
*
* @param src
* @param options
*/
export function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {
options = buildOptions(options);
if (!isDeserializable(src, options)) {
return src;
}
if (!options.collectionType && isArray(src)) {
options.collectionType = Array;
}
if (options.collectionType) {
if (!options.types?.has(options.collectionType)) {
throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);
}
return transformCollection(src, options);
}
if (options.types?.has(options.type)) {
return transformType(src, options);
}
// class converter
return plainObjectToClass(src, options);
}