@tsed/json-mapper 7.35.0 → 7.36.0-rc.1
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/jest.config.js +3 -3
- package/lib/browser/json-mapper.umd.min.js +1 -1
- package/lib/browser/json-mapper.umd.min.js.map +1 -1
- package/lib/cjs/components/DateMapper.js +1 -1
- package/lib/cjs/components/DateMapper.js.map +1 -1
- package/lib/cjs/components/PrimitiveMapper.js +30 -26
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -1
- package/lib/cjs/components/SymbolMapper.js.map +1 -1
- package/lib/cjs/domain/JsonDeserializer.js +275 -0
- package/lib/cjs/domain/JsonDeserializer.js.map +1 -0
- package/lib/cjs/domain/JsonDeserializerOptions.js +3 -0
- package/lib/cjs/domain/JsonDeserializerOptions.js.map +1 -0
- package/lib/cjs/domain/JsonMapperCompiler.js +167 -0
- package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/cjs/domain/JsonSerializer.js +219 -0
- package/lib/cjs/domain/JsonSerializer.js.map +1 -0
- package/lib/cjs/domain/JsonSerializerOptions.js +3 -0
- package/lib/cjs/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/cjs/domain/Writer.js +141 -0
- package/lib/cjs/domain/Writer.js.map +1 -0
- package/lib/cjs/hooks/alterOnDeserialize.js +8 -0
- package/lib/cjs/hooks/alterOnDeserialize.js.map +1 -0
- package/lib/cjs/hooks/alterOnSerialize.js +8 -0
- package/lib/cjs/hooks/alterOnSerialize.js.map +1 -0
- package/lib/cjs/index.js +9 -4
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/utils/deserialize.js +5 -202
- package/lib/cjs/utils/deserialize.js.map +1 -1
- package/lib/cjs/utils/getObjectProperties.js +9 -0
- package/lib/cjs/utils/getObjectProperties.js.map +1 -0
- package/lib/cjs/utils/serialize.js +5 -143
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/components/DateMapper.js +1 -1
- package/lib/esm/components/DateMapper.js.map +1 -1
- package/lib/esm/components/PrimitiveMapper.js +30 -26
- package/lib/esm/components/PrimitiveMapper.js.map +1 -1
- package/lib/esm/components/SymbolMapper.js.map +1 -1
- package/lib/esm/domain/JsonDeserializer.js +271 -0
- package/lib/esm/domain/JsonDeserializer.js.map +1 -0
- package/lib/esm/domain/JsonDeserializerOptions.js +2 -0
- package/lib/esm/domain/JsonDeserializerOptions.js.map +1 -0
- package/lib/esm/domain/JsonMapperCompiler.js +163 -0
- package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/esm/domain/JsonSerializer.js +215 -0
- package/lib/esm/domain/JsonSerializer.js.map +1 -0
- package/lib/esm/domain/JsonSerializerOptions.js +2 -0
- package/lib/esm/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/esm/domain/Writer.js +137 -0
- package/lib/esm/domain/Writer.js.map +1 -0
- package/lib/esm/hooks/alterOnDeserialize.js +4 -0
- package/lib/esm/hooks/alterOnDeserialize.js.map +1 -0
- package/lib/esm/hooks/alterOnSerialize.js +4 -0
- package/lib/esm/hooks/alterOnSerialize.js.map +1 -0
- package/lib/esm/index.js +9 -4
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils/deserialize.js +4 -200
- package/lib/esm/utils/deserialize.js.map +1 -1
- package/lib/esm/utils/getObjectProperties.js +5 -0
- package/lib/esm/utils/getObjectProperties.js.map +1 -0
- package/lib/esm/utils/serialize.js +4 -141
- package/lib/esm/utils/serialize.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/components/PrimitiveMapper.d.ts +4 -0
- package/lib/types/domain/JsonDeserializer.d.ts +33 -0
- package/lib/types/domain/JsonDeserializerOptions.d.ts +31 -0
- package/lib/types/domain/JsonMapperCompiler.d.ts +58 -0
- package/lib/types/domain/JsonSerializer.d.ts +21 -0
- package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
- package/lib/types/domain/Writer.d.ts +43 -0
- package/lib/types/hooks/alterOnDeserialize.d.ts +2 -0
- package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
- package/lib/types/index.d.ts +9 -4
- package/lib/types/utils/deserialize.d.ts +2 -47
- package/lib/types/utils/getObjectProperties.d.ts +1 -0
- package/lib/types/utils/serialize.d.ts +2 -18
- package/package.json +11 -8
- package/lib/cjs/components/ArrayMapper.js +0 -23
- package/lib/cjs/components/ArrayMapper.js.map +0 -1
- package/lib/cjs/components/MapMapper.js +0 -30
- package/lib/cjs/components/MapMapper.js.map +0 -1
- package/lib/cjs/components/SetMapper.js +0 -30
- package/lib/cjs/components/SetMapper.js.map +0 -1
- package/lib/cjs/domain/JsonMapperContext.js +0 -27
- package/lib/cjs/domain/JsonMapperContext.js.map +0 -1
- package/lib/esm/components/ArrayMapper.js +0 -20
- package/lib/esm/components/ArrayMapper.js.map +0 -1
- package/lib/esm/components/MapMapper.js +0 -27
- package/lib/esm/components/MapMapper.js.map +0 -1
- package/lib/esm/components/SetMapper.js +0 -27
- package/lib/esm/components/SetMapper.js.map +0 -1
- package/lib/esm/domain/JsonMapperContext.js +0 -23
- package/lib/esm/domain/JsonMapperContext.js.map +0 -1
- package/lib/types/components/ArrayMapper.d.ts +0 -10
- package/lib/types/components/MapMapper.d.ts +0 -12
- package/lib/types/components/SetMapper.d.ts +0 -10
- package/lib/types/domain/JsonMapperContext.d.ts +0 -20
|
@@ -1,210 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deserialize =
|
|
4
|
-
const core_1 = require("@tsed/core");
|
|
5
|
-
const schema_1 = require("@tsed/schema");
|
|
6
|
-
require("../components/ArrayMapper");
|
|
3
|
+
exports.deserialize = void 0;
|
|
7
4
|
require("../components/DateMapper");
|
|
8
|
-
require("../components/MapMapper");
|
|
9
5
|
require("../components/PrimitiveMapper");
|
|
10
|
-
require("../components/SetMapper");
|
|
11
6
|
require("../components/SymbolMapper");
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const alterBeforeDeserialize_1 = require("../hooks/alterBeforeDeserialize");
|
|
17
|
-
function isDeserializable(obj, options) {
|
|
18
|
-
if ((!!options.collectionType && (0, core_1.isNil)(obj)) || obj === undefined) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
return !((0, core_1.isEmpty)(options.type) || (options.type === Object && !options.collectionType));
|
|
22
|
-
}
|
|
23
|
-
function alterValue(schema, value, options) {
|
|
24
|
-
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
25
|
-
}
|
|
26
|
-
function transformCollection(src, options) {
|
|
27
|
-
const { types, type = Object, collectionType } = options;
|
|
28
|
-
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
29
|
-
type,
|
|
30
|
-
collectionType,
|
|
31
|
-
options,
|
|
32
|
-
next: (data, { collectionType, ...options }) => deserialize(data, options)
|
|
33
|
-
});
|
|
34
|
-
return types?.get(options.collectionType)?.deserialize(src, context);
|
|
35
|
-
}
|
|
36
|
-
function transformType(src, options) {
|
|
37
|
-
const { types, type = Object } = options;
|
|
38
|
-
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
39
|
-
type,
|
|
40
|
-
options,
|
|
41
|
-
next: (data, { type, ...options }) => deserialize(data, options)
|
|
42
|
-
});
|
|
43
|
-
return types?.get(type)?.deserialize(src, context);
|
|
44
|
-
}
|
|
45
|
-
function getAdditionalProperties(nbProps, store, options) {
|
|
46
|
-
const additionalProperties = store.schema.get("additionalProperties");
|
|
47
|
-
if ((0, core_1.isBoolean)(additionalProperties) || (0, core_1.isClass)(additionalProperties)) {
|
|
48
|
-
return additionalProperties;
|
|
49
|
-
}
|
|
50
|
-
return nbProps === 0 || !!options.additionalProperties;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Transform given plain object to class.
|
|
54
|
-
* @param src
|
|
55
|
-
* @param options
|
|
56
|
-
*/
|
|
57
|
-
function plainObjectToClass(src, options) {
|
|
58
|
-
if ((0, core_1.isEmpty)(src)) {
|
|
59
|
-
return src;
|
|
60
|
-
}
|
|
61
|
-
const { type, store = schema_1.JsonEntityStore.from(type) } = options;
|
|
62
|
-
const propertiesMap = (0, schema_1.getProperties)(store, { ...options, withIgnoredProps: true });
|
|
63
|
-
let keys = new Set((0, core_1.objectKeys)(src));
|
|
64
|
-
const additionalProperties = getAdditionalProperties(propertiesMap.size, store, options);
|
|
65
|
-
src = (0, alterBeforeDeserialize_1.alterBeforeDeserialize)(src, store.schema, options);
|
|
66
|
-
const out = new type(options.disableUnsecureConstructor ? {} : src);
|
|
67
|
-
propertiesMap.forEach((propStore) => {
|
|
68
|
-
const key = options.useAlias
|
|
69
|
-
? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName
|
|
70
|
-
: propStore.propertyName;
|
|
71
|
-
keys.delete(key);
|
|
72
|
-
if ((0, schema_1.alterIgnore)(propStore.schema, options)) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let value = alterValue(propStore.schema, src[key], { ...options, self: src });
|
|
76
|
-
value =
|
|
77
|
-
value === src[key]
|
|
78
|
-
? deserialize(value, {
|
|
79
|
-
...options,
|
|
80
|
-
store: propStore,
|
|
81
|
-
format: propStore.itemSchema.get("format"),
|
|
82
|
-
self: src,
|
|
83
|
-
collectionType: propStore.collectionType
|
|
84
|
-
})
|
|
85
|
-
: value;
|
|
86
|
-
if (!propStore.isGetterOnly()) {
|
|
87
|
-
if (value !== undefined) {
|
|
88
|
-
out[propStore.propertyName] = value;
|
|
89
|
-
}
|
|
90
|
-
else if (options.partial) {
|
|
91
|
-
delete out[propStore.propertyName];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
if (additionalProperties) {
|
|
96
|
-
if ((0, core_1.isBoolean)(additionalProperties)) {
|
|
97
|
-
keys.forEach((key) => {
|
|
98
|
-
out[key] = src[key];
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
const type = additionalProperties.getComputedType();
|
|
103
|
-
keys.forEach((key) => {
|
|
104
|
-
out[key] = deserialize(src[key], {
|
|
105
|
-
...options,
|
|
106
|
-
type
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return (0, alterAfterDeserialize_1.alterAfterDeserialize)(out, store.schema, options);
|
|
112
|
-
}
|
|
113
|
-
exports.plainObjectToClass = plainObjectToClass;
|
|
114
|
-
function mapPropStoreOptions(store, options) {
|
|
115
|
-
const itemOpts = {
|
|
116
|
-
...options,
|
|
117
|
-
store: undefined,
|
|
118
|
-
type: store.computedType
|
|
119
|
-
};
|
|
120
|
-
if (store.itemSchema.isDiscriminator) {
|
|
121
|
-
itemOpts.type = store.itemSchema.discriminator();
|
|
122
|
-
}
|
|
123
|
-
if (store.schema.hasGenerics) {
|
|
124
|
-
itemOpts.nestedGenerics = store.schema.nestedGenerics;
|
|
125
|
-
}
|
|
126
|
-
else if (store.schema.isGeneric && options.nestedGenerics) {
|
|
127
|
-
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
128
|
-
const genericLabels = store.parent.schema.genericLabels || [];
|
|
129
|
-
itemOpts.type = genericTypes[genericLabels.indexOf(store.schema.genericType)] || Object;
|
|
130
|
-
if (itemOpts.type instanceof schema_1.JsonSchema) {
|
|
131
|
-
itemOpts.type = itemOpts.type.getTarget();
|
|
132
|
-
}
|
|
133
|
-
itemOpts.nestedGenerics = nestedGenerics;
|
|
134
|
-
}
|
|
135
|
-
return itemOpts;
|
|
136
|
-
}
|
|
137
|
-
function mapParamStoreOptions(store, options) {
|
|
138
|
-
return {
|
|
139
|
-
...options,
|
|
140
|
-
store: undefined,
|
|
141
|
-
type: store.getBestType(),
|
|
142
|
-
collectionType: store.collectionType,
|
|
143
|
-
groups: store.parameter.groups,
|
|
144
|
-
genericTypes: store.nestedGenerics[0],
|
|
145
|
-
nestedGenerics: store.nestedGenerics
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
function buildOptions(options) {
|
|
149
|
-
if (options.store instanceof schema_1.JsonPropertyStore) {
|
|
150
|
-
return buildOptions(mapPropStoreOptions(options.store, options));
|
|
151
|
-
}
|
|
152
|
-
if (options.store instanceof schema_1.JsonParameterStore) {
|
|
153
|
-
return buildOptions(mapParamStoreOptions(options.store, options));
|
|
154
|
-
}
|
|
155
|
-
if (options.store instanceof schema_1.JsonEntityStore) {
|
|
156
|
-
options.type = options.store.computedType;
|
|
157
|
-
options.collectionType = options.store.collectionType;
|
|
158
|
-
options.store = undefined;
|
|
159
|
-
}
|
|
160
|
-
else if ((0, core_1.isClass)(options.type)) {
|
|
161
|
-
const store = schema_1.JsonEntityStore.from(options.type);
|
|
162
|
-
if (store.schema.isDiscriminator) {
|
|
163
|
-
options.type = schema_1.JsonEntityStore.from(options.type).schema.discriminator();
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return {
|
|
167
|
-
groups: false,
|
|
168
|
-
useAlias: true,
|
|
169
|
-
...options,
|
|
170
|
-
additionalProperties: (0, core_1.getValue)(options, "additionalProperties", JsonMapperSettings_1.JsonMapperSettings.additionalProperties),
|
|
171
|
-
disableUnsecureConstructor: (0, core_1.getValue)(options, "disableUnsecureConstructor", JsonMapperSettings_1.JsonMapperSettings.disableUnsecureConstructor),
|
|
172
|
-
partial: options.groups ? options.groups.includes("partial") : false,
|
|
173
|
-
type: options.type ? options.type : undefined,
|
|
174
|
-
types: options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)()
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Transform given source to class base on the given `options.type`.
|
|
179
|
-
*
|
|
180
|
-
* @param src
|
|
181
|
-
* @param options
|
|
182
|
-
*/
|
|
183
|
-
function deserialize(src, options = {}) {
|
|
184
|
-
options = buildOptions(options);
|
|
185
|
-
if (!isDeserializable(src, options)) {
|
|
186
|
-
return src;
|
|
187
|
-
}
|
|
188
|
-
if (!options.collectionType && (0, core_1.isArray)(src)) {
|
|
189
|
-
options.collectionType = Array;
|
|
190
|
-
}
|
|
191
|
-
if (options.collectionType) {
|
|
192
|
-
if (!options.types?.has(options.collectionType)) {
|
|
193
|
-
throw new Error(`${(0, core_1.nameOf)(options.collectionType)} is not supported by JsonMapper.`);
|
|
194
|
-
}
|
|
195
|
-
return transformCollection(src, options);
|
|
196
|
-
}
|
|
197
|
-
if (options.types?.has(options.type)) {
|
|
198
|
-
return transformType(src, options);
|
|
199
|
-
}
|
|
200
|
-
if (options.type instanceof schema_1.Discriminator) {
|
|
201
|
-
const discriminator = options.type;
|
|
202
|
-
const discriminatorValue = src[discriminator.propertyName];
|
|
203
|
-
const type = discriminator.getType(discriminatorValue);
|
|
204
|
-
return plainObjectToClass(src, { ...options, type });
|
|
205
|
-
}
|
|
206
|
-
// class converter
|
|
207
|
-
return plainObjectToClass(src, options);
|
|
7
|
+
const JsonDeserializer_1 = require("../domain/JsonDeserializer");
|
|
8
|
+
const deserializer = new JsonDeserializer_1.JsonDeserializer();
|
|
9
|
+
function deserialize(input, options) {
|
|
10
|
+
return deserializer.map(input, options);
|
|
208
11
|
}
|
|
209
12
|
exports.deserialize = deserialize;
|
|
210
13
|
//# sourceMappingURL=deserialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,oCAAkC;AAClC,yCAAuC;AACvC,sCAAoC;AAEpC,iEAA4D;AAG5D,MAAM,YAAY,GAAG,IAAI,mCAAgB,EAAE,CAAC;AAE5C,SAAgB,WAAW,CAAc,KAAU,EAAE,OAAiC;IACpF,OAAO,YAAY,CAAC,GAAG,CAAQ,KAAK,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC;AAFD,kCAEC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getObjectProperties = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
function getObjectProperties(obj) {
|
|
6
|
+
return Object.entries(obj).filter(([, value]) => !(0, core_1.isFunction)(value));
|
|
7
|
+
}
|
|
8
|
+
exports.getObjectProperties = getObjectProperties;
|
|
9
|
+
//# sourceMappingURL=getObjectProperties.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getObjectProperties.js","sourceRoot":"","sources":["../../../src/utils/getObjectProperties.ts"],"names":[],"mappings":";;;AAAA,qCAAsC;AAEtC,SAAgB,mBAAmB,CAAC,GAAQ;IAC1C,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,IAAA,iBAAU,EAAC,KAAK,CAAC,CAAC,CAAC;AACvE,CAAC;AAFD,kDAEC"}
|
|
@@ -1,151 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.serialize =
|
|
4
|
-
const core_1 = require("@tsed/core");
|
|
5
|
-
const schema_1 = require("@tsed/schema");
|
|
6
|
-
require("../components/ArrayMapper");
|
|
3
|
+
exports.serialize = void 0;
|
|
7
4
|
require("../components/DateMapper");
|
|
8
|
-
require("../components/MapMapper");
|
|
9
5
|
require("../components/PrimitiveMapper");
|
|
10
|
-
require("../components/SetMapper");
|
|
11
6
|
require("../components/SymbolMapper");
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
function
|
|
15
|
-
return
|
|
16
|
-
}
|
|
17
|
-
function getSchemaProperties(storedJson, obj) {
|
|
18
|
-
const stores = Array.from((0, schema_1.getPropertiesStores)(storedJson).entries());
|
|
19
|
-
if (!stores.length) {
|
|
20
|
-
// fallback to auto discovering field from obj
|
|
21
|
-
(0, core_1.objectKeys)(obj).forEach((key) => {
|
|
22
|
-
const propStore = schema_1.JsonEntityStore.from(storedJson.target, key);
|
|
23
|
-
stores.push([key, propStore]);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
return stores;
|
|
27
|
-
}
|
|
28
|
-
function getObjectProperties(obj) {
|
|
29
|
-
return Object.entries(obj).filter(([, value]) => !(0, core_1.isFunction)(value));
|
|
30
|
-
}
|
|
31
|
-
function getType(propStore, value) {
|
|
32
|
-
if ((0, core_1.isNil)(value) || propStore.type === Object || (0, core_1.isCollection)(propStore.type)) {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
return propStore.type;
|
|
36
|
-
}
|
|
37
|
-
function classToPlainObject(obj, options) {
|
|
38
|
-
const { useAlias = true, type, ...props } = options;
|
|
39
|
-
const entity = schema_1.JsonEntityStore.from(type || obj);
|
|
40
|
-
const additionalProperties = !!entity.schema.get("additionalProperties");
|
|
41
|
-
const schemaProperties = getSchemaProperties(entity, obj);
|
|
42
|
-
const properties = new Set();
|
|
43
|
-
const out = schemaProperties.reduce((newObj, [key, propStore]) => {
|
|
44
|
-
properties.add(key);
|
|
45
|
-
const schema = propStore.schema;
|
|
46
|
-
if ((0, schema_1.alterIgnore)(schema, { useAlias, ...props, self: obj })) {
|
|
47
|
-
return newObj;
|
|
48
|
-
}
|
|
49
|
-
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
50
|
-
value = serialize(value, {
|
|
51
|
-
useAlias,
|
|
52
|
-
self: obj,
|
|
53
|
-
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
54
|
-
collectionType: propStore.collectionType,
|
|
55
|
-
format: propStore.itemSchema.get("format"),
|
|
56
|
-
...props
|
|
57
|
-
});
|
|
58
|
-
if (value === undefined) {
|
|
59
|
-
return newObj;
|
|
60
|
-
}
|
|
61
|
-
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
62
|
-
return {
|
|
63
|
-
...newObj,
|
|
64
|
-
[key]: value
|
|
65
|
-
};
|
|
66
|
-
}, {});
|
|
67
|
-
if (entity.discriminatorAncestor) {
|
|
68
|
-
const discriminator = entity.discriminatorAncestor.schema.discriminator();
|
|
69
|
-
if (!out[discriminator.propertyName]) {
|
|
70
|
-
out[discriminator.propertyName] = discriminator.getDefaultValue(entity.target);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (additionalProperties) {
|
|
74
|
-
(0, core_1.objectKeys)(obj).forEach((key) => {
|
|
75
|
-
if (!properties.has(key)) {
|
|
76
|
-
out[key] = obj[key];
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return out;
|
|
81
|
-
}
|
|
82
|
-
exports.classToPlainObject = classToPlainObject;
|
|
83
|
-
function toObject(obj, options) {
|
|
84
|
-
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
85
|
-
...newObj,
|
|
86
|
-
[key]: serialize(value, options)
|
|
87
|
-
}), {});
|
|
88
|
-
}
|
|
89
|
-
function getBestType(type, obj) {
|
|
90
|
-
const dataType = (0, core_1.classOf)(obj);
|
|
91
|
-
if (dataType && !(0, core_1.isClassObject)(dataType)) {
|
|
92
|
-
return dataType;
|
|
93
|
-
}
|
|
94
|
-
return type || Object;
|
|
95
|
-
}
|
|
96
|
-
function hasJsonMethod(obj) {
|
|
97
|
-
return typeof obj.toJSON === "function";
|
|
98
|
-
}
|
|
99
|
-
function isMongooseObject(obj) {
|
|
100
|
-
return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;
|
|
101
|
-
}
|
|
102
|
-
function isMomentObject(obj) {
|
|
103
|
-
return obj && obj?._isAMomentObject;
|
|
104
|
-
}
|
|
105
|
-
function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
106
|
-
if ((0, core_1.isEmpty)(obj)) {
|
|
107
|
-
return obj;
|
|
108
|
-
}
|
|
109
|
-
let currentType = type;
|
|
110
|
-
const types = options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
|
|
111
|
-
options.groups = groups;
|
|
112
|
-
// FIX custom serialization function from @tsed/mongoose and bson
|
|
113
|
-
if (isMongooseObject(obj)) {
|
|
114
|
-
return obj.toJSON(options);
|
|
115
|
-
}
|
|
116
|
-
if (hasJsonMethod(obj) && !isMomentObject(obj) && !(0, core_1.isDate)(obj)) {
|
|
117
|
-
return serialize(obj.toJSON(), { ...options, type: (0, core_1.classOf)(obj) });
|
|
118
|
-
}
|
|
119
|
-
if (!(0, core_1.isCollection)(obj)) {
|
|
120
|
-
options.type = currentType = getBestType(currentType, obj);
|
|
121
|
-
}
|
|
122
|
-
else if (!options.collectionType) {
|
|
123
|
-
currentType = (0, core_1.classOf)(obj);
|
|
124
|
-
options.type = type;
|
|
125
|
-
options.collectionType = currentType;
|
|
126
|
-
}
|
|
127
|
-
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
128
|
-
type: currentType,
|
|
129
|
-
options,
|
|
130
|
-
next: (data) => serialize(data, {
|
|
131
|
-
...options,
|
|
132
|
-
collectionType: undefined,
|
|
133
|
-
type: options.type
|
|
134
|
-
})
|
|
135
|
-
});
|
|
136
|
-
const mapper = types.get(currentType) || types.get((0, core_1.nameOf)(currentType));
|
|
137
|
-
if (mapper) {
|
|
138
|
-
const jsonMapper = mapper;
|
|
139
|
-
return jsonMapper.serialize(obj, context);
|
|
140
|
-
}
|
|
141
|
-
else if (obj?._isAMomentObject) {
|
|
142
|
-
return obj.toJSON();
|
|
143
|
-
}
|
|
144
|
-
if ((0, core_1.isArray)(obj)) {
|
|
145
|
-
// Serialize Array class like
|
|
146
|
-
return types.get(Array)?.serialize(obj, context);
|
|
147
|
-
}
|
|
148
|
-
return !(0, core_1.isClassObject)(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
7
|
+
const JsonSerializer_1 = require("../domain/JsonSerializer");
|
|
8
|
+
const serializer = new JsonSerializer_1.JsonSerializer();
|
|
9
|
+
function serialize(input, options) {
|
|
10
|
+
return serializer.map(input, options);
|
|
149
11
|
}
|
|
150
12
|
exports.serialize = serialize;
|
|
151
13
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":";;;AAAA,oCAAkC;AAClC,yCAAuC;AACvC,sCAAoC;AAEpC,6DAAwD;AAGxD,MAAM,UAAU,GAAG,IAAI,+BAAc,EAAE,CAAC;AAExC,SAAgB,SAAS,CAAC,KAAU,EAAE,OAA+B;IACnE,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC;AAFD,8BAEC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateMapper.js","sourceRoot":"","sources":["../../../src/components/DateMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AACrC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"DateMapper.js","sourceRoot":"","sources":["../../../src/components/DateMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,YAAY,CAAC;AACrC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AAEI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAGrB,WAAW,CAAC,IAAS;QACnB,6EAA6E;QAC7E,6EAA6E;QAC7E,oBAAoB;QACpB,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,EAAE;YAC1D,OAAO,IAAI,CAAC;SACb;QAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,SAAS,CAAC,MAAY;QACpB,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IAC1D,CAAC;CACF,CAAA;AAjBY,UAAU;IADtB,UAAU,CAAC,IAAI,CAAC;GACJ,UAAU,CAiBtB;SAjBY,UAAU"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
|
+
import { nameOf } from "@tsed/core";
|
|
2
3
|
import { JsonMapper } from "../decorators/jsonMapper.js";
|
|
3
4
|
function isNullish(data) {
|
|
4
5
|
return [null, "null"].includes(data);
|
|
@@ -16,36 +17,39 @@ export class CastError extends Error {
|
|
|
16
17
|
*/
|
|
17
18
|
let PrimitiveMapper = class PrimitiveMapper {
|
|
18
19
|
deserialize(data, ctx) {
|
|
19
|
-
|
|
20
|
-
case String:
|
|
21
|
-
return data === null ? null : "" + data;
|
|
22
|
-
case BigInt:
|
|
23
|
-
if (isNullish(data))
|
|
24
|
-
return null;
|
|
25
|
-
return BigInt(data);
|
|
26
|
-
case Number:
|
|
27
|
-
if (isNullish(data))
|
|
28
|
-
return null;
|
|
29
|
-
const n = +data;
|
|
30
|
-
if (isNaN(n)) {
|
|
31
|
-
throw new CastError("Expression value is not a number.");
|
|
32
|
-
}
|
|
33
|
-
return n;
|
|
34
|
-
case Boolean:
|
|
35
|
-
if (["true", "1", true].includes(data))
|
|
36
|
-
return true;
|
|
37
|
-
if (["false", "0", false].includes(data))
|
|
38
|
-
return false;
|
|
39
|
-
if (isNullish(data))
|
|
40
|
-
return null;
|
|
41
|
-
if (data === undefined)
|
|
42
|
-
return undefined;
|
|
43
|
-
return !!data;
|
|
44
|
-
}
|
|
20
|
+
return this[nameOf(ctx.type)] ? this[nameOf(ctx.type)](data, ctx) : undefined;
|
|
45
21
|
}
|
|
46
22
|
serialize(object) {
|
|
47
23
|
return object;
|
|
48
24
|
}
|
|
25
|
+
String(data) {
|
|
26
|
+
return data === null ? null : "" + data;
|
|
27
|
+
}
|
|
28
|
+
Boolean(data) {
|
|
29
|
+
if (["true", "1", true].includes(data))
|
|
30
|
+
return true;
|
|
31
|
+
if (["false", "0", false].includes(data))
|
|
32
|
+
return false;
|
|
33
|
+
if (isNullish(data))
|
|
34
|
+
return null;
|
|
35
|
+
if (data === undefined)
|
|
36
|
+
return undefined;
|
|
37
|
+
return !!data;
|
|
38
|
+
}
|
|
39
|
+
Number(data) {
|
|
40
|
+
if (isNullish(data))
|
|
41
|
+
return null;
|
|
42
|
+
const n = +data;
|
|
43
|
+
if (isNaN(n)) {
|
|
44
|
+
throw new CastError("Expression value is not a number.");
|
|
45
|
+
}
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
48
|
+
BigInt(data) {
|
|
49
|
+
if (isNullish(data))
|
|
50
|
+
return null;
|
|
51
|
+
return BigInt(data);
|
|
52
|
+
}
|
|
49
53
|
};
|
|
50
54
|
PrimitiveMapper = __decorate([
|
|
51
55
|
JsonMapper(String, Number, Boolean, BigInt)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrimitiveMapper.js","sourceRoot":"","sources":["../../../src/components/PrimitiveMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD,SAAS,SAAS,CAAC,IAAS;IAC1B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,IAAI,GAAG,YAAY,CAAC;IAEpB,YAAY,OAAe;QACzB,KAAK,CAAC,eAAe,OAAO,EAAE,CAAC,CAAC;IAClC,CAAC;CACF;AAED;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"PrimitiveMapper.js","sourceRoot":"","sources":["../../../src/components/PrimitiveMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,YAAY,CAAC;AAClC,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD,SAAS,SAAS,CAAC,IAAS;IAC1B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,IAAI,GAAG,YAAY,CAAC;IAEpB,YAAY,OAAe;QACzB,KAAK,CAAC,eAAe,OAAO,EAAE,CAAC,CAAC;IAClC,CAAC;CACF;AAED;;;;GAIG;AAEI,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,WAAW,CAAI,IAAS,EAAE,GAAkB;QAC1C,OAAQ,IAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAE,IAAY,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAClG,CAAC;IAED,SAAS,CAAC,MAA0C;QAClD,OAAO,MAAM,CAAC;IAChB,CAAC;IAES,MAAM,CAAC,IAAS;QACxB,OAAO,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC;IAC1C,CAAC;IAES,OAAO,CAAC,IAAS;QACzB,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QACpD,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;YAAE,OAAO,KAAK,CAAC;QACvD,IAAI,SAAS,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QACjC,IAAI,IAAI,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QAEzC,OAAO,CAAC,CAAC,IAAI,CAAC;IAChB,CAAC;IAES,MAAM,CAAC,IAAS;QACxB,IAAI,SAAS,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QAEjC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;QAEhB,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;YACZ,MAAM,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAC;SAC1D;QAED,OAAO,CAAC,CAAC;IACX,CAAC;IAES,MAAM,CAAC,IAAS;QACxB,IAAI,SAAS,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QAEjC,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;CACF,CAAA;AAvCY,eAAe;IAD3B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;GAC/B,eAAe,CAuC3B;SAvCY,eAAe"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SymbolMapper.js","sourceRoot":"","sources":["../../../src/components/SymbolMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"SymbolMapper.js","sourceRoot":"","sources":["../../../src/components/SymbolMapper.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AAGpD;;;;;GAKG;AAEI,IAAM,YAAY,GAAlB,MAAM,YAAY;IACvB,WAAW,CAAC,IAAY;QACtB,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,SAAS,CAAC,MAAc;QACtB,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACF,CAAA;AARY,YAAY;IADxB,UAAU,CAAC,MAAM,CAAC;GACN,YAAY,CAQxB;SARY,YAAY"}
|