@tsed/json-mapper 6.88.0 → 6.91.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{components → cjs/components}/ArrayMapper.d.ts +0 -0
- package/lib/cjs/components/ArrayMapper.js +23 -0
- package/lib/cjs/components/ArrayMapper.js.map +1 -0
- package/lib/{components → cjs/components}/DateMapper.d.ts +0 -0
- package/lib/cjs/components/DateMapper.js +30 -0
- package/lib/cjs/components/DateMapper.js.map +1 -0
- package/lib/{components → cjs/components}/MapMapper.d.ts +0 -0
- package/lib/cjs/components/MapMapper.js +30 -0
- package/lib/cjs/components/MapMapper.js.map +1 -0
- package/lib/{components → cjs/components}/PrimitiveMapper.d.ts +0 -0
- package/lib/cjs/components/PrimitiveMapper.js +45 -0
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SetMapper.d.ts +0 -0
- package/lib/cjs/components/SetMapper.js +30 -0
- package/lib/cjs/components/SetMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SymbolMapper.d.ts +0 -0
- package/lib/cjs/components/SymbolMapper.js +24 -0
- package/lib/cjs/components/SymbolMapper.js.map +1 -0
- package/lib/{components → cjs/components}/index.d.ts +0 -0
- package/lib/cjs/components/index.js +10 -0
- package/lib/cjs/components/index.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/afterDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/afterDeserialize.js +39 -0
- package/lib/cjs/decorators/afterDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/beforeDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/beforeDeserialize.js +39 -0
- package/lib/cjs/decorators/beforeDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/jsonMapper.d.ts +0 -0
- package/lib/cjs/decorators/jsonMapper.js +18 -0
- package/lib/cjs/decorators/jsonMapper.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/onDeserialize.js +29 -0
- package/lib/cjs/decorators/onDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onSerialize.d.ts +0 -0
- package/lib/cjs/decorators/onSerialize.js +29 -0
- package/lib/cjs/decorators/onSerialize.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperContext.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperContext.js +23 -0
- package/lib/cjs/domain/JsonMapperContext.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperTypesContainer.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js +23 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterAfterDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js +9 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterBeforeDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js +9 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{interfaces → cjs/interfaces}/JsonMapperMethods.d.ts +0 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js +3 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/{utils → cjs/utils}/deserialize.d.ts +0 -0
- package/lib/cjs/utils/deserialize.js +140 -0
- package/lib/cjs/utils/deserialize.js.map +1 -0
- package/lib/{utils → cjs/utils}/serialize.d.ts +0 -0
- package/lib/cjs/utils/serialize.js +112 -0
- package/lib/cjs/utils/serialize.js.map +1 -0
- package/lib/esm/components/ArrayMapper.js +20 -0
- package/lib/esm/components/ArrayMapper.js.map +1 -0
- package/lib/esm/components/DateMapper.js +27 -0
- package/lib/esm/components/DateMapper.js.map +1 -0
- package/lib/esm/components/MapMapper.js +27 -0
- package/lib/esm/components/MapMapper.js.map +1 -0
- package/lib/esm/components/PrimitiveMapper.js +42 -0
- package/lib/esm/components/PrimitiveMapper.js.map +1 -0
- package/lib/esm/components/SetMapper.js +27 -0
- package/lib/esm/components/SetMapper.js.map +1 -0
- package/lib/esm/components/SymbolMapper.js +21 -0
- package/lib/esm/components/SymbolMapper.js.map +1 -0
- package/lib/esm/components/index.js +7 -0
- package/lib/esm/components/index.js.map +1 -0
- package/lib/esm/decorators/afterDeserialize.js +35 -0
- package/lib/esm/decorators/afterDeserialize.js.map +1 -0
- package/lib/esm/decorators/beforeDeserialize.js +35 -0
- package/lib/esm/decorators/beforeDeserialize.js.map +1 -0
- package/lib/esm/decorators/jsonMapper.js +14 -0
- package/lib/esm/decorators/jsonMapper.js.map +1 -0
- package/lib/esm/decorators/onDeserialize.js +25 -0
- package/lib/esm/decorators/onDeserialize.js.map +1 -0
- package/lib/esm/decorators/onSerialize.js +25 -0
- package/lib/esm/decorators/onSerialize.js.map +1 -0
- package/lib/esm/domain/JsonMapperContext.js +19 -0
- package/lib/esm/domain/JsonMapperContext.js.map +1 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js +18 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/esm/hooks/alterAfterDeserialize.js +5 -0
- package/lib/esm/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js +5 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/esm/index.js +12 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/JsonMapperMethods.js +2 -0
- package/lib/esm/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/esm/utils/deserialize.js +135 -0
- package/lib/esm/utils/deserialize.js.map +1 -0
- package/lib/esm/utils/serialize.js +107 -0
- package/lib/esm/utils/serialize.js.map +1 -0
- package/lib/types/components/ArrayMapper.d.ts +10 -0
- package/lib/types/components/DateMapper.d.ts +11 -0
- package/lib/types/components/MapMapper.d.ts +12 -0
- package/lib/types/components/PrimitiveMapper.d.ts +10 -0
- package/lib/types/components/SetMapper.d.ts +10 -0
- package/lib/types/components/SymbolMapper.d.ts +11 -0
- package/lib/types/components/index.d.ts +6 -0
- package/lib/types/decorators/afterDeserialize.d.ts +33 -0
- package/lib/types/decorators/beforeDeserialize.d.ts +33 -0
- package/lib/types/decorators/jsonMapper.d.ts +7 -0
- package/lib/types/decorators/onDeserialize.d.ts +23 -0
- package/lib/types/decorators/onSerialize.d.ts +23 -0
- package/lib/types/domain/JsonMapperContext.d.ts +20 -0
- package/lib/types/domain/JsonMapperTypesContainer.d.ts +10 -0
- package/lib/types/hooks/alterAfterDeserialize.d.ts +4 -0
- package/lib/types/hooks/alterBeforeDeserialize.d.ts +4 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/interfaces/JsonMapperMethods.d.ts +25 -0
- package/lib/types/utils/deserialize.d.ts +41 -0
- package/lib/types/utils/serialize.d.ts +16 -0
- package/package.json +18 -14
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index.js +0 -719
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -708
- package/lib/index.modern.js.map +0 -1
package/lib/index.modern.js
DELETED
|
@@ -1,708 +0,0 @@
|
|
|
1
|
-
import { __decorate } from 'tslib';
|
|
2
|
-
import { isBoolean, objectKeys, isCollection, isEmpty, isArray, nameOf, isNil, classOf, isClassObject, isFunction } from '@tsed/core';
|
|
3
|
-
import { BadRequest } from '@tsed/exceptions';
|
|
4
|
-
import { JsonEntityFn, JsonEntityStore, getProperties, alterIgnore, JsonSchema, getPropertiesStores } from '@tsed/schema';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @ignore
|
|
8
|
-
*/
|
|
9
|
-
// tslint:disable-next-line:variable-name
|
|
10
|
-
const JsonMapperTypesContainer = new Map();
|
|
11
|
-
/**
|
|
12
|
-
* @ignore
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
function registerJsonTypeMapper(type, token) {
|
|
16
|
-
JsonMapperTypesContainer.set(type, new token());
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* @ignore
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
function getJsonMapperTypes() {
|
|
23
|
-
return JsonMapperTypesContainer;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Register a new JsonMapper Type
|
|
28
|
-
* @param types
|
|
29
|
-
* @constructor
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
function JsonMapper(...types) {
|
|
33
|
-
return target => {
|
|
34
|
-
types.forEach(type => {
|
|
35
|
-
registerJsonTypeMapper(type, target);
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Mapper for `Array` type.
|
|
42
|
-
* @jsonmapper
|
|
43
|
-
* @component
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
let ArrayMapper = class ArrayMapper {
|
|
47
|
-
deserialize(data, options) {
|
|
48
|
-
return [].concat(data).map(item => options.next(item));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
serialize(data, options) {
|
|
52
|
-
return [].concat(data).map(item => options.next(item));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
ArrayMapper = __decorate([JsonMapper(Array)], ArrayMapper);
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Mapper for `Date` type.
|
|
60
|
-
* @jsonmapper
|
|
61
|
-
* @component
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
let DateMapper = class DateMapper {
|
|
65
|
-
deserialize(data) {
|
|
66
|
-
// don't convert unexpected data. In normal case, Ajv reject unexpected data.
|
|
67
|
-
// But by default, we have to skip data deserialization and let user to apply
|
|
68
|
-
// the right mapping
|
|
69
|
-
if (isBoolean(data) || data === null || data === undefined) {
|
|
70
|
-
return data;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return new Date(data);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
serialize(object) {
|
|
77
|
-
return new Date(object).toISOString();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
};
|
|
81
|
-
DateMapper = __decorate([JsonMapper(Date)], DateMapper);
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Mapper for `Map` type.
|
|
85
|
-
* @jsonmapper
|
|
86
|
-
* @component
|
|
87
|
-
*/
|
|
88
|
-
|
|
89
|
-
let MapMapper = class MapMapper {
|
|
90
|
-
deserialize(data, ctx) {
|
|
91
|
-
const obj = new Map();
|
|
92
|
-
objectKeys(data).forEach(key => {
|
|
93
|
-
obj.set(key, ctx.next(data[key]));
|
|
94
|
-
});
|
|
95
|
-
return obj;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
serialize(data, ctx) {
|
|
99
|
-
const obj = {};
|
|
100
|
-
data.forEach((value, key) => obj[key] = ctx.next(value));
|
|
101
|
-
return obj;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
-
MapMapper = __decorate([JsonMapper(Map)], MapMapper);
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Mapper for the `String`, `Number` and `Boolean` types.
|
|
109
|
-
* @jsonmapper
|
|
110
|
-
* @component
|
|
111
|
-
*/
|
|
112
|
-
|
|
113
|
-
let PrimitiveMapper = class PrimitiveMapper {
|
|
114
|
-
deserialize(data, ctx) {
|
|
115
|
-
switch (ctx.type) {
|
|
116
|
-
case String:
|
|
117
|
-
return data === null ? null : "" + data;
|
|
118
|
-
|
|
119
|
-
case Number:
|
|
120
|
-
if ([null, "null"].includes(data)) return null;
|
|
121
|
-
const n = +data;
|
|
122
|
-
|
|
123
|
-
if (isNaN(n)) {
|
|
124
|
-
throw new BadRequest("Cast error. Expression value is not a number.");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return n;
|
|
128
|
-
|
|
129
|
-
case Boolean:
|
|
130
|
-
if (["true", "1", true].includes(data)) return true;
|
|
131
|
-
if (["false", "0", false].includes(data)) return false;
|
|
132
|
-
if ([null, "null"].includes(data)) return null;
|
|
133
|
-
if (data === undefined) return undefined;
|
|
134
|
-
return !!data;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
serialize(object) {
|
|
139
|
-
return object;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
};
|
|
143
|
-
PrimitiveMapper = __decorate([JsonMapper(String, Number, Boolean)], PrimitiveMapper);
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Mapper for the `Set` Type.
|
|
147
|
-
* @jsonmapper
|
|
148
|
-
* @component
|
|
149
|
-
*/
|
|
150
|
-
|
|
151
|
-
let SetMapper = class SetMapper {
|
|
152
|
-
deserialize(data, ctx) {
|
|
153
|
-
const obj = new Set();
|
|
154
|
-
objectKeys(data).forEach(key => {
|
|
155
|
-
obj.add(ctx.next(data[key]));
|
|
156
|
-
});
|
|
157
|
-
return obj;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
serialize(data, ctx) {
|
|
161
|
-
const array = [];
|
|
162
|
-
data.forEach(value => array.push(ctx.next(value)));
|
|
163
|
-
return array;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
};
|
|
167
|
-
SetMapper = __decorate([JsonMapper(Set)], SetMapper);
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Mapper for the `Symbol` type.
|
|
171
|
-
*
|
|
172
|
-
* @jsonmapper
|
|
173
|
-
* @component
|
|
174
|
-
*/
|
|
175
|
-
|
|
176
|
-
let SymbolMapper = class SymbolMapper {
|
|
177
|
-
deserialize(data) {
|
|
178
|
-
return Symbol.for(data);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
serialize(object) {
|
|
182
|
-
return object.toString().replace("Symbol(", "").replace(")", "");
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
};
|
|
186
|
-
SymbolMapper = __decorate([JsonMapper(Symbol)], SymbolMapper);
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Call the function before property deserialization.
|
|
190
|
-
*
|
|
191
|
-
* ## Example
|
|
192
|
-
*
|
|
193
|
-
* ```typescript
|
|
194
|
-
* class Model {
|
|
195
|
-
* @OnDeserialize(v => v + 1)
|
|
196
|
-
* property: string;
|
|
197
|
-
* }
|
|
198
|
-
* ```
|
|
199
|
-
*
|
|
200
|
-
* @returns {Function}
|
|
201
|
-
* @decorator
|
|
202
|
-
* @schema
|
|
203
|
-
* @property
|
|
204
|
-
* @param cb
|
|
205
|
-
*/
|
|
206
|
-
|
|
207
|
-
function OnDeserialize(cb) {
|
|
208
|
-
return JsonEntityFn(storedJson => {
|
|
209
|
-
storedJson.schema.$hooks.on("onDeserialize", cb);
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Call the function before property serialization.
|
|
215
|
-
*
|
|
216
|
-
* ## Example
|
|
217
|
-
*
|
|
218
|
-
* ```typescript
|
|
219
|
-
* class Model {
|
|
220
|
-
* @OnSerialize(v => v + 1)
|
|
221
|
-
* property: string;
|
|
222
|
-
* }
|
|
223
|
-
* ```
|
|
224
|
-
*
|
|
225
|
-
* @returns {Function}
|
|
226
|
-
* @decorator
|
|
227
|
-
* @schema
|
|
228
|
-
* @property
|
|
229
|
-
* @param cb
|
|
230
|
-
*/
|
|
231
|
-
|
|
232
|
-
function OnSerialize(cb) {
|
|
233
|
-
return JsonEntityFn(storedJson => {
|
|
234
|
-
storedJson.schema.$hooks.on("onSerialize", cb);
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Call the function after JsonMapper.
|
|
240
|
-
*
|
|
241
|
-
* ## Example
|
|
242
|
-
*
|
|
243
|
-
* ```typescript
|
|
244
|
-
* @AfterDeserialize((data: Model) => { // After deserialize get the instance class
|
|
245
|
-
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
246
|
-
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
247
|
-
* } else {
|
|
248
|
-
* data.prop2 = 'VALUE2';
|
|
249
|
-
* return data;
|
|
250
|
-
* }
|
|
251
|
-
* })
|
|
252
|
-
* class Model {
|
|
253
|
-
* @Property()
|
|
254
|
-
* prop1: string;
|
|
255
|
-
* @Property()
|
|
256
|
-
* prop2: string;
|
|
257
|
-
* }
|
|
258
|
-
* ```
|
|
259
|
-
*
|
|
260
|
-
* @returns {Function}
|
|
261
|
-
* @decorator
|
|
262
|
-
* @schema
|
|
263
|
-
* @property
|
|
264
|
-
* @param cb
|
|
265
|
-
*/
|
|
266
|
-
|
|
267
|
-
function AfterDeserialize(cb) {
|
|
268
|
-
return JsonEntityFn(storedJson => {
|
|
269
|
-
storedJson.schema.$hooks.on("afterDeserialize", cb);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Call the function before JsonMapper.
|
|
275
|
-
*
|
|
276
|
-
* ## Example
|
|
277
|
-
*
|
|
278
|
-
* ```typescript
|
|
279
|
-
* @BeforeDeserialize((data: Record<string, unknown>) => { // Before deserialize get the data
|
|
280
|
-
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
281
|
-
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
282
|
-
* } else {
|
|
283
|
-
* data.prop2 = 'VALUE2';
|
|
284
|
-
* return data;
|
|
285
|
-
* }
|
|
286
|
-
* })
|
|
287
|
-
* class Model {
|
|
288
|
-
* @Property()
|
|
289
|
-
* prop1: string;
|
|
290
|
-
* @Property()
|
|
291
|
-
* prop2: string;
|
|
292
|
-
* }
|
|
293
|
-
* ```
|
|
294
|
-
*
|
|
295
|
-
* @returns {Function}
|
|
296
|
-
* @decorator
|
|
297
|
-
* @schema
|
|
298
|
-
* @property
|
|
299
|
-
* @param cb
|
|
300
|
-
*/
|
|
301
|
-
|
|
302
|
-
function BeforeDeserialize(cb) {
|
|
303
|
-
return JsonEntityFn(storedJson => {
|
|
304
|
-
storedJson.schema.$hooks.on("beforeDeserialize", cb);
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
function _extends() {
|
|
309
|
-
_extends = Object.assign || function (target) {
|
|
310
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
311
|
-
var source = arguments[i];
|
|
312
|
-
|
|
313
|
-
for (var key in source) {
|
|
314
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
315
|
-
target[key] = source[key];
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return target;
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
return _extends.apply(this, arguments);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
327
|
-
if (source == null) return {};
|
|
328
|
-
var target = {};
|
|
329
|
-
var sourceKeys = Object.keys(source);
|
|
330
|
-
var key, i;
|
|
331
|
-
|
|
332
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
333
|
-
key = sourceKeys[i];
|
|
334
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
335
|
-
target[key] = source[key];
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return target;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* @ignore
|
|
343
|
-
*/
|
|
344
|
-
|
|
345
|
-
class JsonMapperContext {
|
|
346
|
-
constructor({
|
|
347
|
-
type,
|
|
348
|
-
collectionType,
|
|
349
|
-
next,
|
|
350
|
-
options
|
|
351
|
-
}) {
|
|
352
|
-
this.type = type;
|
|
353
|
-
this.collectionType = isCollection(collectionType) ? collectionType : undefined;
|
|
354
|
-
this._next = next;
|
|
355
|
-
this.options = options;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
next(data) {
|
|
359
|
-
return this._next(data, _extends({}, this.options, {
|
|
360
|
-
type: this.type
|
|
361
|
-
}));
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function alterAfterDeserialize(data, schema, options) {
|
|
367
|
-
var _schema$$hooks;
|
|
368
|
-
|
|
369
|
-
return schema == null ? void 0 : (_schema$$hooks = schema.$hooks) == null ? void 0 : _schema$$hooks.alter("afterDeserialize", data, [options]);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
function alterBeforeDeserialize(data, schema, options) {
|
|
373
|
-
var _schema$$hooks;
|
|
374
|
-
|
|
375
|
-
return schema == null ? void 0 : (_schema$$hooks = schema.$hooks) == null ? void 0 : _schema$$hooks.alter("beforeDeserialize", data, [options]);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const _excluded$1 = ["collectionType"],
|
|
379
|
-
_excluded2$1 = ["type"],
|
|
380
|
-
_excluded3 = ["type", "store"];
|
|
381
|
-
|
|
382
|
-
function isDeserializable(obj, options) {
|
|
383
|
-
if (!!options.collectionType && isNil(obj) || obj === undefined) {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
return !(isEmpty(options.type) || options.type === Object && !options.collectionType);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function alterValue$1(schema, value, options) {
|
|
391
|
-
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
function transformCollection(src, options) {
|
|
395
|
-
var _types$get;
|
|
396
|
-
|
|
397
|
-
const {
|
|
398
|
-
types,
|
|
399
|
-
type = Object,
|
|
400
|
-
collectionType
|
|
401
|
-
} = options;
|
|
402
|
-
const context = new JsonMapperContext({
|
|
403
|
-
type,
|
|
404
|
-
collectionType,
|
|
405
|
-
options,
|
|
406
|
-
next: (data, _ref) => {
|
|
407
|
-
let options = _objectWithoutPropertiesLoose(_ref, _excluded$1);
|
|
408
|
-
|
|
409
|
-
return deserialize(data, options);
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
return types == null ? void 0 : (_types$get = types.get(options.collectionType)) == null ? void 0 : _types$get.deserialize(src, context);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
function transformType(src, options) {
|
|
416
|
-
var _types$get2;
|
|
417
|
-
|
|
418
|
-
const {
|
|
419
|
-
types,
|
|
420
|
-
type = Object
|
|
421
|
-
} = options;
|
|
422
|
-
const context = new JsonMapperContext({
|
|
423
|
-
type,
|
|
424
|
-
options,
|
|
425
|
-
next: (data, _ref2) => {
|
|
426
|
-
let options = _objectWithoutPropertiesLoose(_ref2, _excluded2$1);
|
|
427
|
-
|
|
428
|
-
return deserialize(data, options);
|
|
429
|
-
}
|
|
430
|
-
});
|
|
431
|
-
return types == null ? void 0 : (_types$get2 = types.get(type)) == null ? void 0 : _types$get2.deserialize(src, context);
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Transform given plain object to class.
|
|
435
|
-
* @param src
|
|
436
|
-
* @param options
|
|
437
|
-
*/
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
function plainObjectToClass(src, options) {
|
|
441
|
-
if (isEmpty(src)) {
|
|
442
|
-
return src;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
const {
|
|
446
|
-
type,
|
|
447
|
-
store = JsonEntityStore.from(type)
|
|
448
|
-
} = options,
|
|
449
|
-
next = _objectWithoutPropertiesLoose(options, _excluded3);
|
|
450
|
-
|
|
451
|
-
const propertiesMap = getProperties(store, _extends({}, options, {
|
|
452
|
-
withIgnoredProps: true
|
|
453
|
-
}));
|
|
454
|
-
let keys = objectKeys(src);
|
|
455
|
-
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
|
|
456
|
-
src = alterBeforeDeserialize(src, store.schema, options);
|
|
457
|
-
const out = new type(src);
|
|
458
|
-
propertiesMap.forEach(propStore => {
|
|
459
|
-
const key = options.useAlias ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName : propStore.propertyName;
|
|
460
|
-
keys = keys.filter(k => k !== key);
|
|
461
|
-
next.type = propStore.computedType;
|
|
462
|
-
|
|
463
|
-
if (alterIgnore(propStore.itemSchema, options)) {
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
let value = alterValue$1(propStore.schema, src[key], _extends({}, options, {
|
|
468
|
-
self: src
|
|
469
|
-
}));
|
|
470
|
-
|
|
471
|
-
if (propStore.schema.hasGenerics) {
|
|
472
|
-
next.nestedGenerics = propStore.schema.nestedGenerics;
|
|
473
|
-
} else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
474
|
-
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
475
|
-
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
476
|
-
next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
477
|
-
|
|
478
|
-
if (next.type instanceof JsonSchema) {
|
|
479
|
-
next.type = next.type.getTarget();
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
next.nestedGenerics = nestedGenerics;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
value = deserialize(value, _extends({}, next, {
|
|
486
|
-
type: value === src[key] ? next.type : undefined,
|
|
487
|
-
collectionType: propStore.collectionType
|
|
488
|
-
}));
|
|
489
|
-
|
|
490
|
-
if (value !== undefined) {
|
|
491
|
-
out[propStore.propertyName] = value;
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
if (additionalProperties) {
|
|
496
|
-
keys.forEach(key => {
|
|
497
|
-
out[key] = src[key];
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
return alterAfterDeserialize(out, store.schema, options);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
function buildOptions(options) {
|
|
505
|
-
if (options.store instanceof JsonEntityStore) {
|
|
506
|
-
if (options.store.parameter && options.store.parameter.nestedGenerics.length) {
|
|
507
|
-
options.nestedGenerics = options.store.parameter.nestedGenerics;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
options.type = options.store.computedType;
|
|
511
|
-
options.collectionType = options.store.collectionType;
|
|
512
|
-
delete options.store;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
return _extends({
|
|
516
|
-
groups: false,
|
|
517
|
-
useAlias: true
|
|
518
|
-
}, options, {
|
|
519
|
-
type: options.type ? options.type : undefined,
|
|
520
|
-
types: options.types ? options.types : getJsonMapperTypes()
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* Transform given source to class base on the given `options.type`.
|
|
525
|
-
*
|
|
526
|
-
* @param src
|
|
527
|
-
* @param options
|
|
528
|
-
*/
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
function deserialize(src, options = {}) {
|
|
532
|
-
var _options$types2;
|
|
533
|
-
|
|
534
|
-
options = buildOptions(options);
|
|
535
|
-
|
|
536
|
-
if (!isDeserializable(src, options)) {
|
|
537
|
-
return src;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
if (!options.collectionType && isArray(src)) {
|
|
541
|
-
options.collectionType = Array;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
if (options.collectionType) {
|
|
545
|
-
var _options$types;
|
|
546
|
-
|
|
547
|
-
if (!((_options$types = options.types) != null && _options$types.has(options.collectionType))) {
|
|
548
|
-
throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
return transformCollection(src, options);
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
if ((_options$types2 = options.types) != null && _options$types2.has(options.type)) {
|
|
555
|
-
return transformType(src, options);
|
|
556
|
-
} // class converter
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
return plainObjectToClass(src, options);
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
const _excluded = ["useAlias", "type"],
|
|
563
|
-
_excluded2 = ["type", "collectionType", "groups"];
|
|
564
|
-
|
|
565
|
-
function alterValue(schema, value, options) {
|
|
566
|
-
return schema.$hooks.alter("onSerialize", value, [options]);
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
function getSchemaProperties(storedJson, obj) {
|
|
570
|
-
const stores = Array.from(getPropertiesStores(storedJson).entries());
|
|
571
|
-
|
|
572
|
-
if (!stores.length) {
|
|
573
|
-
// fallback to auto discovering field from obj
|
|
574
|
-
objectKeys(obj).forEach(key => {
|
|
575
|
-
const propStore = JsonEntityStore.from(storedJson.target, key);
|
|
576
|
-
stores.push([key, propStore]);
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return stores;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
function getObjectProperties(obj) {
|
|
584
|
-
return Object.entries(obj).filter(([, value]) => !isFunction(value));
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
function getType(propStore, value) {
|
|
588
|
-
if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {
|
|
589
|
-
return undefined;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return propStore.type;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
function classToPlainObject(obj, options) {
|
|
596
|
-
const {
|
|
597
|
-
useAlias = true,
|
|
598
|
-
type
|
|
599
|
-
} = options,
|
|
600
|
-
props = _objectWithoutPropertiesLoose(options, _excluded);
|
|
601
|
-
|
|
602
|
-
const entity = JsonEntityStore.from(type || obj);
|
|
603
|
-
return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {
|
|
604
|
-
const schema = propStore.schema;
|
|
605
|
-
|
|
606
|
-
if (alterIgnore(schema, _extends({
|
|
607
|
-
useAlias
|
|
608
|
-
}, props, {
|
|
609
|
-
self: obj
|
|
610
|
-
}))) {
|
|
611
|
-
return newObj;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
let value = alterValue(schema, obj[key], _extends({
|
|
615
|
-
useAlias
|
|
616
|
-
}, props, {
|
|
617
|
-
self: obj
|
|
618
|
-
}));
|
|
619
|
-
value = serialize(value, _extends({
|
|
620
|
-
useAlias,
|
|
621
|
-
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
622
|
-
collectionType: propStore.collectionType
|
|
623
|
-
}, props));
|
|
624
|
-
|
|
625
|
-
if (value === undefined) {
|
|
626
|
-
return newObj;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
630
|
-
return _extends({}, newObj, {
|
|
631
|
-
[key]: value
|
|
632
|
-
});
|
|
633
|
-
}, {});
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function toObject(obj, options) {
|
|
637
|
-
return getObjectProperties(obj).reduce((newObj, [key, value]) => _extends({}, newObj, {
|
|
638
|
-
[key]: serialize(value, options)
|
|
639
|
-
}), {});
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
function getBestType(type, obj) {
|
|
643
|
-
const dataType = classOf(obj);
|
|
644
|
-
|
|
645
|
-
if (dataType && !isClassObject(dataType)) {
|
|
646
|
-
return dataType;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
return type || Object;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
function serialize(obj, _ref = {}) {
|
|
653
|
-
let {
|
|
654
|
-
type,
|
|
655
|
-
groups = false
|
|
656
|
-
} = _ref,
|
|
657
|
-
options = _objectWithoutPropertiesLoose(_ref, _excluded2);
|
|
658
|
-
|
|
659
|
-
if (isEmpty(obj)) {
|
|
660
|
-
return obj;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
const types = options.types ? options.types : getJsonMapperTypes();
|
|
664
|
-
options.groups = groups; // FIX custom serialization function from @tsed/mongoose and bson
|
|
665
|
-
|
|
666
|
-
if (typeof obj.toJSON === "function" && obj.$isMongooseModelPrototype || obj._bsontype) {
|
|
667
|
-
return obj.toJSON(options);
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
if (typeof obj.toJSON === "function") {
|
|
671
|
-
return serialize(obj.toJSON(), _extends({}, options, {
|
|
672
|
-
type: classOf(obj)
|
|
673
|
-
}));
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (!isCollection(obj)) {
|
|
677
|
-
options.type = type = getBestType(type, obj);
|
|
678
|
-
} else if (!options.collectionType) {
|
|
679
|
-
type = classOf(obj);
|
|
680
|
-
options.collectionType = type;
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
const context = new JsonMapperContext({
|
|
684
|
-
type,
|
|
685
|
-
options,
|
|
686
|
-
next: data => serialize(data, _extends({}, options, {
|
|
687
|
-
collectionType: undefined,
|
|
688
|
-
type: options.type
|
|
689
|
-
}))
|
|
690
|
-
});
|
|
691
|
-
|
|
692
|
-
if (types.has(type)) {
|
|
693
|
-
const jsonMapper = types.get(type);
|
|
694
|
-
return jsonMapper.serialize(obj, context);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
if (isArray(obj)) {
|
|
698
|
-
var _types$get;
|
|
699
|
-
|
|
700
|
-
// Serialize Array class like
|
|
701
|
-
return (_types$get = types.get(Array)) == null ? void 0 : _types$get.serialize(obj, context);
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
return !isClassObject(type) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
export { AfterDeserialize, ArrayMapper, BeforeDeserialize, DateMapper, JsonMapper, JsonMapperContext, MapMapper, OnDeserialize, OnSerialize, PrimitiveMapper, SetMapper, SymbolMapper, classToPlainObject, deserialize, getJsonMapperTypes, plainObjectToClass, registerJsonTypeMapper, serialize };
|
|
708
|
-
//# sourceMappingURL=index.modern.js.map
|