@tsed/json-mapper 8.11.1 → 8.12.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/lib/esm/components/DateMapper.js +0 -1
- package/lib/esm/components/PrimitiveMapper.js +0 -1
- package/lib/esm/components/SymbolMapper.js +0 -1
- package/lib/esm/decorators/afterDeserialize.js +0 -1
- package/lib/esm/decorators/beforeDeserialize.js +0 -1
- package/lib/esm/decorators/jsonMapper.js +0 -1
- package/lib/esm/decorators/onDeserialize.js +0 -1
- package/lib/esm/decorators/onSerialize.js +0 -1
- package/lib/esm/domain/JsonDeserializer.js +0 -1
- package/lib/esm/domain/JsonDeserializerOptions.js +0 -1
- package/lib/esm/domain/JsonMapperCompiler.js +0 -1
- package/lib/esm/domain/JsonMapperGlobalOptions.js +0 -1
- package/lib/esm/domain/JsonMapperSettings.js +0 -1
- package/lib/esm/domain/JsonMapperTypesContainer.js +0 -1
- package/lib/esm/domain/JsonSerializer.js +0 -1
- package/lib/esm/domain/JsonSerializerOptions.js +0 -1
- package/lib/esm/domain/Writer.js +0 -1
- package/lib/esm/hooks/alterAfterDeserialize.js +0 -1
- package/lib/esm/hooks/alterBeforeDeserialize.js +0 -1
- package/lib/esm/hooks/alterOnDeserialize.js +0 -1
- package/lib/esm/hooks/alterOnSerialize.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/interfaces/JsonMapperMethods.js +0 -1
- package/lib/esm/utils/deserialize.js +0 -1
- package/lib/esm/utils/getObjectProperties.js +0 -1
- package/lib/esm/utils/serialize.js +0 -1
- package/package.json +12 -11
- package/lib/browser/components/DateMapper.js +0 -22
- package/lib/browser/components/DateMapper.js.map +0 -1
- package/lib/browser/components/PrimitiveMapper.js +0 -52
- package/lib/browser/components/PrimitiveMapper.js.map +0 -1
- package/lib/browser/components/SymbolMapper.js +0 -21
- package/lib/browser/components/SymbolMapper.js.map +0 -1
- package/lib/browser/decorators/afterDeserialize.js +0 -10
- package/lib/browser/decorators/afterDeserialize.js.map +0 -1
- package/lib/browser/decorators/beforeDeserialize.js +0 -10
- package/lib/browser/decorators/beforeDeserialize.js.map +0 -1
- package/lib/browser/decorators/jsonMapper.js +0 -12
- package/lib/browser/decorators/jsonMapper.js.map +0 -1
- package/lib/browser/decorators/onDeserialize.js +0 -10
- package/lib/browser/decorators/onDeserialize.js.map +0 -1
- package/lib/browser/decorators/onSerialize.js +0 -10
- package/lib/browser/decorators/onSerialize.js.map +0 -1
- package/lib/browser/domain/JsonDeserializer.js +0 -179
- package/lib/browser/domain/JsonDeserializer.js.map +0 -1
- package/lib/browser/domain/JsonDeserializerOptions.js +0 -2
- package/lib/browser/domain/JsonDeserializerOptions.js.map +0 -1
- package/lib/browser/domain/JsonMapperCompiler.js +0 -110
- package/lib/browser/domain/JsonMapperCompiler.js.map +0 -1
- package/lib/browser/domain/JsonMapperGlobalOptions.js +0 -2
- package/lib/browser/domain/JsonMapperGlobalOptions.js.map +0 -1
- package/lib/browser/domain/JsonMapperSettings.js +0 -9
- package/lib/browser/domain/JsonMapperSettings.js.map +0 -1
- package/lib/browser/domain/JsonMapperTypesContainer.js +0 -12
- package/lib/browser/domain/JsonMapperTypesContainer.js.map +0 -1
- package/lib/browser/domain/JsonSerializer.js +0 -164
- package/lib/browser/domain/JsonSerializer.js.map +0 -1
- package/lib/browser/domain/JsonSerializerOptions.js +0 -2
- package/lib/browser/domain/JsonSerializerOptions.js.map +0 -1
- package/lib/browser/domain/Writer.js +0 -124
- package/lib/browser/domain/Writer.js.map +0 -1
- package/lib/browser/hooks/alterAfterDeserialize.js +0 -8
- package/lib/browser/hooks/alterAfterDeserialize.js.map +0 -1
- package/lib/browser/hooks/alterBeforeDeserialize.js +0 -8
- package/lib/browser/hooks/alterBeforeDeserialize.js.map +0 -1
- package/lib/browser/hooks/alterOnDeserialize.js +0 -7
- package/lib/browser/hooks/alterOnDeserialize.js.map +0 -1
- package/lib/browser/hooks/alterOnSerialize.js +0 -7
- package/lib/browser/hooks/alterOnSerialize.js.map +0 -1
- package/lib/browser/index.js +0 -47
- package/lib/browser/index.js.map +0 -1
- package/lib/browser/utils/deserialize.js +0 -12
- package/lib/browser/utils/deserialize.js.map +0 -1
- package/lib/browser/utils/getObjectProperties.js +0 -8
- package/lib/browser/utils/getObjectProperties.js.map +0 -1
- package/lib/browser/utils/serialize.js +0 -12
- package/lib/browser/utils/serialize.js.map +0 -1
- package/lib/esm/components/DateMapper.js.map +0 -1
- package/lib/esm/components/PrimitiveMapper.js.map +0 -1
- package/lib/esm/components/SymbolMapper.js.map +0 -1
- package/lib/esm/decorators/afterDeserialize.js.map +0 -1
- package/lib/esm/decorators/beforeDeserialize.js.map +0 -1
- package/lib/esm/decorators/jsonMapper.js.map +0 -1
- package/lib/esm/decorators/onDeserialize.js.map +0 -1
- package/lib/esm/decorators/onSerialize.js.map +0 -1
- package/lib/esm/domain/JsonDeserializer.js.map +0 -1
- package/lib/esm/domain/JsonDeserializerOptions.js.map +0 -1
- package/lib/esm/domain/JsonMapperCompiler.js.map +0 -1
- package/lib/esm/domain/JsonMapperGlobalOptions.js.map +0 -1
- package/lib/esm/domain/JsonMapperSettings.js.map +0 -1
- package/lib/esm/domain/JsonMapperTypesContainer.js.map +0 -1
- package/lib/esm/domain/JsonSerializer.js.map +0 -1
- package/lib/esm/domain/JsonSerializerOptions.js.map +0 -1
- package/lib/esm/domain/Writer.js.map +0 -1
- package/lib/esm/hooks/alterAfterDeserialize.js.map +0 -1
- package/lib/esm/hooks/alterBeforeDeserialize.js.map +0 -1
- package/lib/esm/hooks/alterOnDeserialize.js.map +0 -1
- package/lib/esm/hooks/alterOnSerialize.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/JsonMapperMethods.js.map +0 -1
- package/lib/esm/utils/deserialize.js.map +0 -1
- package/lib/esm/utils/getObjectProperties.js.map +0 -1
- package/lib/esm/utils/serialize.js.map +0 -1
- package/src/components/DateMapper.spec.ts +0 -43
- package/src/components/DateMapper.ts +0 -29
- package/src/components/PrimitiveMapper.spec.ts +0 -179
- package/src/components/PrimitiveMapper.ts +0 -64
- package/src/components/SymbolMapper.spec.ts +0 -23
- package/src/components/SymbolMapper.ts +0 -19
- package/src/decorators/afterDeserialize.ts +0 -39
- package/src/decorators/beforeDeserialize.ts +0 -39
- package/src/decorators/jsonMapper.ts +0 -16
- package/src/decorators/onDeserialize.ts +0 -29
- package/src/decorators/onSerialize.ts +0 -29
- package/src/domain/JsonDeserializer.spec.ts +0 -1437
- package/src/domain/JsonDeserializer.ts +0 -402
- package/src/domain/JsonDeserializerOptions.ts +0 -31
- package/src/domain/JsonMapperCompiler.ts +0 -247
- package/src/domain/JsonMapperGlobalOptions.ts +0 -14
- package/src/domain/JsonMapperSettings.ts +0 -7
- package/src/domain/JsonMapperTypesContainer.ts +0 -20
- package/src/domain/JsonSerializer.spec.ts +0 -1506
- package/src/domain/JsonSerializer.ts +0 -371
- package/src/domain/JsonSerializerOptions.ts +0 -17
- package/src/domain/Writer.spec.ts +0 -11
- package/src/domain/Writer.ts +0 -181
- package/src/domain/__snapshots__/Writer.spec.ts.snap +0 -10
- package/src/hooks/alterAfterDeserialize.ts +0 -5
- package/src/hooks/alterBeforeDeserialize.ts +0 -5
- package/src/hooks/alterOnDeserialize.ts +0 -5
- package/src/hooks/alterOnSerialize.ts +0 -5
- package/src/index.ts +0 -28
- package/src/interfaces/JsonMapperMethods.ts +0 -32
- package/src/utils/deserialize.spec.ts +0 -33
- package/src/utils/deserialize.ts +0 -12
- package/src/utils/getObjectProperties.ts +0 -5
- package/src/utils/serialize.spec.ts +0 -37
- package/src/utils/serialize.ts +0 -12
- package/vite.config.mts +0 -45
- package/vitest.config.mts +0 -21
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
classOf,
|
|
3
|
-
hasJsonMethod,
|
|
4
|
-
isArray,
|
|
5
|
-
isBoolean,
|
|
6
|
-
isClassObject,
|
|
7
|
-
isCollection,
|
|
8
|
-
isDate,
|
|
9
|
-
isMomentObject,
|
|
10
|
-
isMongooseObject,
|
|
11
|
-
isNil,
|
|
12
|
-
isObject,
|
|
13
|
-
isPrimitive,
|
|
14
|
-
nameOf,
|
|
15
|
-
Type
|
|
16
|
-
} from "@tsed/core";
|
|
17
|
-
import {getPropertiesStores, JsonClassStore, JsonEntityStore, JsonPropertyStore} from "@tsed/schema";
|
|
18
|
-
|
|
19
|
-
import {alterOnSerialize} from "../hooks/alterOnSerialize.js";
|
|
20
|
-
import {getObjectProperties} from "../utils/getObjectProperties.js";
|
|
21
|
-
import {JsonMapperCompiler} from "./JsonMapperCompiler.js";
|
|
22
|
-
import {JsonMapperSettings} from "./JsonMapperSettings.js";
|
|
23
|
-
import {getJsonMapperTypes} from "./JsonMapperTypesContainer.js";
|
|
24
|
-
import {JsonSerializerOptions} from "./JsonSerializerOptions.js";
|
|
25
|
-
import {Writer} from "./Writer.js";
|
|
26
|
-
|
|
27
|
-
const getCollectionType = (input: any) => {
|
|
28
|
-
return isArray(input) ? "Array" : input instanceof Set ? "Set" : input instanceof Map ? "Map" : undefined;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
function getBestType(type: Type<any>, obj: any) {
|
|
32
|
-
const dataType = classOf(obj);
|
|
33
|
-
|
|
34
|
-
if (dataType && !isClassObject(dataType) && !isCollection(obj)) {
|
|
35
|
-
return dataType;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return type || Object;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function varKey(k: string) {
|
|
42
|
-
return `__${k}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class JsonSerializer extends JsonMapperCompiler<JsonSerializerOptions> {
|
|
46
|
-
constructor() {
|
|
47
|
-
super();
|
|
48
|
-
|
|
49
|
-
this.addTypeMapper(Object, this.mapObject.bind(this));
|
|
50
|
-
this.addTypeMapper(Array, this.mapArray.bind(this));
|
|
51
|
-
this.addTypeMapper(Map, this.mapMap.bind(this));
|
|
52
|
-
this.addTypeMapper(Set, this.mapSet.bind(this));
|
|
53
|
-
this.addGlobal("mapJSON", this.mapJSON.bind(this));
|
|
54
|
-
this.addTypeMapper("ObjectId", (value: any) => String(value));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
map(input: any, options: JsonSerializerOptions = {}) {
|
|
58
|
-
if (isNil(input)) {
|
|
59
|
-
return input;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
options = this.mapOptions(options);
|
|
63
|
-
|
|
64
|
-
const model = getBestType(options.type, input);
|
|
65
|
-
|
|
66
|
-
const mapper = this.compile(model, options.groups);
|
|
67
|
-
|
|
68
|
-
return mapper.fn(input, this.createContext(options));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
protected alterValue(schemaId: string, value: any, options: JsonSerializerOptions): any {
|
|
72
|
-
return alterOnSerialize(this.schemes[schemaId], value, options as any);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
protected createMapper(model: Type<any>, id: string, groups: false | string[]): string {
|
|
76
|
-
const entity = JsonEntityStore.from(model);
|
|
77
|
-
const properties = new Set<string>();
|
|
78
|
-
const schemaProperties = [...getPropertiesStores(entity).values()];
|
|
79
|
-
|
|
80
|
-
const writer = new Writer().arrow("input", "options");
|
|
81
|
-
|
|
82
|
-
writer.if("isNil(input)").return("input");
|
|
83
|
-
|
|
84
|
-
// prepare options
|
|
85
|
-
writer.const("obj", "{}");
|
|
86
|
-
writer.set("options", "{...options, self: input}");
|
|
87
|
-
|
|
88
|
-
// detect some special case
|
|
89
|
-
writer.add(this.mapPrecondition(id));
|
|
90
|
-
|
|
91
|
-
if (!schemaProperties.length) {
|
|
92
|
-
return writer.return("isObject(input) ? {...input} : input").root().toString();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// properties
|
|
96
|
-
writer.add(
|
|
97
|
-
...schemaProperties.flatMap((propertyStore) => {
|
|
98
|
-
properties.add(propertyStore.propertyKey as string);
|
|
99
|
-
|
|
100
|
-
if (
|
|
101
|
-
(propertyStore.schema?.$ignore && isBoolean(propertyStore.schema?.$ignore)) ||
|
|
102
|
-
(propertyStore.schema?.$hooks?.has("groups") && this.alterGroups(propertyStore.schema, groups))
|
|
103
|
-
) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return this.mapProperty(propertyStore, id, groups);
|
|
108
|
-
})
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
// discriminator
|
|
112
|
-
writer.add(this.mapDiscriminatorKeyValue(entity));
|
|
113
|
-
|
|
114
|
-
// additional properties
|
|
115
|
-
writer.add(this.mapAdditionalProperties(entity, properties));
|
|
116
|
-
|
|
117
|
-
return writer.return("obj").root().toString();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private mapOptions({groups, useAlias = true, types, ...options}: JsonSerializerOptions<any, any>): JsonSerializerOptions {
|
|
121
|
-
const customMappers: Record<string, any> = {};
|
|
122
|
-
types = types || getJsonMapperTypes();
|
|
123
|
-
|
|
124
|
-
types.forEach((mapper, model) => {
|
|
125
|
-
if (![Array, Set, Map].includes(model as any)) {
|
|
126
|
-
this.addTypeMapper(model as any, (value: any, options: any) =>
|
|
127
|
-
mapper.serialize(value, {
|
|
128
|
-
...options,
|
|
129
|
-
type: model
|
|
130
|
-
})
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
const strictGroups = options.strictGroups ?? JsonMapperSettings.strictGroups;
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
...options,
|
|
139
|
-
groups: groups === undefined ? (strictGroups ? [] : false) : groups || false,
|
|
140
|
-
useAlias,
|
|
141
|
-
customMappers
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private mapProperty(propertyStore: JsonPropertyStore, id: string, groups: false | string[]) {
|
|
146
|
-
const key = String(propertyStore.propertyKey);
|
|
147
|
-
const aliasKey: string = String(propertyStore.parent.schema.getAliasOf(key) || key);
|
|
148
|
-
const schemaId = this.getSchemaId(id, key);
|
|
149
|
-
const format = propertyStore.itemSchema.get("format");
|
|
150
|
-
const formatOpts = format && `options: {format: '${format}'}`;
|
|
151
|
-
|
|
152
|
-
let writer = new Writer().add(`// Map ${key} ${id} ${groups || ""}`);
|
|
153
|
-
|
|
154
|
-
// ignore hook (deprecated)
|
|
155
|
-
if (propertyStore.schema?.$hooks?.has("ignore")) {
|
|
156
|
-
this.schemes[schemaId] = propertyStore.schema;
|
|
157
|
-
|
|
158
|
-
writer = writer.if(`!alterIgnore('${schemaId}', {...options, self: input})`);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// pre hook
|
|
162
|
-
const hasSerializer = propertyStore.schema?.$hooks?.has("onSerialize");
|
|
163
|
-
let getter = `input.${key}`;
|
|
164
|
-
|
|
165
|
-
if (hasSerializer) {
|
|
166
|
-
this.schemes[schemaId] = propertyStore.schema;
|
|
167
|
-
const opts = Writer.options(formatOpts);
|
|
168
|
-
|
|
169
|
-
getter = `alterValue('${schemaId}', input.${key}, ${opts})`;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
writer = writer.set(`let ${varKey(key)}`, getter).if(`${varKey(key)} !== undefined`);
|
|
173
|
-
|
|
174
|
-
const fill = this.getPropertyFiller(propertyStore, key, groups, formatOpts);
|
|
175
|
-
|
|
176
|
-
if (hasSerializer) {
|
|
177
|
-
fill(writer.if(`${varKey(key)} === input.${key}`));
|
|
178
|
-
} else {
|
|
179
|
-
fill(writer);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (aliasKey !== key) {
|
|
183
|
-
writer.set(`obj[options.useAlias ? '${aliasKey}' : '${key}']`, varKey(key));
|
|
184
|
-
} else {
|
|
185
|
-
writer.set(`obj.${key}`, varKey(key));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return writer.root();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
private getPropertyFiller(propertyStore: JsonPropertyStore, key: string, groups: false | string[], formatOpts: any) {
|
|
192
|
-
const isGeneric = propertyStore.itemSchema.isGeneric;
|
|
193
|
-
const hasDiscriminator = propertyStore.itemSchema.hasDiscriminator;
|
|
194
|
-
|
|
195
|
-
if (propertyStore.isCollection) {
|
|
196
|
-
const type = propertyStore.getBestType();
|
|
197
|
-
let nestedMapper: any;
|
|
198
|
-
|
|
199
|
-
if (hasDiscriminator) {
|
|
200
|
-
const targetName = propertyStore.parent.targetName;
|
|
201
|
-
|
|
202
|
-
nestedMapper = this.compile(`Discriminator:${targetName}:${key}`, groups, {
|
|
203
|
-
mapper: () => this.createDiscriminatorMapper(propertyStore, groups)
|
|
204
|
-
});
|
|
205
|
-
} else {
|
|
206
|
-
nestedMapper = isGeneric ? {id: ""} : this.compile(type, groups);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return (writer: Writer) =>
|
|
210
|
-
writer.callMapper(nameOf(propertyStore.collectionType), varKey(key), `id: '${nestedMapper.id}'`, formatOpts);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (isGeneric) {
|
|
214
|
-
return (writer: Writer) => writer.set(varKey(key), `compileAndMap(${varKey(key)}, options)`);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
let nestedMapper: any;
|
|
218
|
-
if (hasDiscriminator) {
|
|
219
|
-
const targetName = propertyStore.parent.targetName;
|
|
220
|
-
|
|
221
|
-
nestedMapper = this.compile(`Discriminator:${targetName}:${key}`, groups, {
|
|
222
|
-
mapper: () => this.createDiscriminatorMapper(propertyStore, groups)
|
|
223
|
-
});
|
|
224
|
-
} else {
|
|
225
|
-
nestedMapper = this.compile(propertyStore.getBestType(), groups);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return (writer: Writer) => writer.callMapper(nestedMapper.id, varKey(key), formatOpts);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
private createDiscriminatorMapper(propertyStore: JsonPropertyStore, groups: false | string[]) {
|
|
232
|
-
const discriminator = propertyStore.itemSchema.discriminator();
|
|
233
|
-
const writer = new Writer().arrow("input", "options");
|
|
234
|
-
|
|
235
|
-
const sw = writer.switch(`nameOf(classOf(input))`);
|
|
236
|
-
|
|
237
|
-
discriminator.values.forEach((value, kind) => {
|
|
238
|
-
const nestedMapper = this.compile(value, groups);
|
|
239
|
-
|
|
240
|
-
sw.case(`'${nameOf(value)}'`).returnCallMapper(nestedMapper.id, "input");
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
return writer.root().toString();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
private mapPrecondition(id: string) {
|
|
247
|
-
const writer = new Writer();
|
|
248
|
-
|
|
249
|
-
writer.if("input && isCollection(input)").return(Writer.mapperFrom("input", `{...options, id: '${id}'}`));
|
|
250
|
-
|
|
251
|
-
writer.if("hasJsonMethod(input)").return(`mapJSON(input, {...options, id: '${id}'})`);
|
|
252
|
-
|
|
253
|
-
return writer;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
private mapDiscriminatorKeyValue(entity: JsonClassStore) {
|
|
257
|
-
if (entity.discriminatorAncestor) {
|
|
258
|
-
const discriminator = entity.discriminatorAncestor.schema.discriminator();
|
|
259
|
-
const type = discriminator.getDefaultValue(entity.target);
|
|
260
|
-
|
|
261
|
-
if (type) {
|
|
262
|
-
const writer = new Writer();
|
|
263
|
-
writer.if(`!obj.${discriminator.propertyName}`).set(`obj.${discriminator.propertyName}`, `'${type}'`);
|
|
264
|
-
|
|
265
|
-
return writer;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
private mapAdditionalProperties(entity: JsonClassStore, properties: Set<string>) {
|
|
271
|
-
const additionalProperties = !!entity.schema.get("additionalProperties");
|
|
272
|
-
|
|
273
|
-
if (additionalProperties) {
|
|
274
|
-
const exclude = [...properties.values()].map((key) => `'${key}'`).join(", ");
|
|
275
|
-
const writer = new Writer();
|
|
276
|
-
writer.add("// add additional properties");
|
|
277
|
-
writer.each("objectKeys(input)", ["key"]).if(`![${exclude}].includes(key)`).set("obj[key]", "input[key]");
|
|
278
|
-
|
|
279
|
-
return writer;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
private mapObject(input: any, {type, ...options}: JsonSerializerOptions) {
|
|
284
|
-
if ((input && isPrimitive(input)) || !input) {
|
|
285
|
-
// prevent mongoose mapping error
|
|
286
|
-
return input;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (input && isCollection(input)) {
|
|
290
|
-
return this.execMapper(getCollectionType(input)!, input, options);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (hasJsonMethod(input)) {
|
|
294
|
-
return this.mapJSON(input, options);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
return getObjectProperties(input)
|
|
298
|
-
.filter(([, value]) => value !== undefined)
|
|
299
|
-
.reduce((obj, [key, value]) => {
|
|
300
|
-
if (isNil(value)) {
|
|
301
|
-
return {
|
|
302
|
-
...obj,
|
|
303
|
-
[key]: value
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const mapper = this.compile(classOf(value), options.groups);
|
|
308
|
-
|
|
309
|
-
return {
|
|
310
|
-
...obj,
|
|
311
|
-
[key]: mapper.fn(value, options)
|
|
312
|
-
};
|
|
313
|
-
}, {});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
private mapSet(input: any, options: JsonSerializerOptions) {
|
|
317
|
-
if (isNil(input)) {
|
|
318
|
-
return input;
|
|
319
|
-
}
|
|
320
|
-
return [...input.values()].map((item) => {
|
|
321
|
-
return this.mapItem(item, options);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
private mapArray(input: any, options: JsonSerializerOptions) {
|
|
326
|
-
if (isNil(input)) {
|
|
327
|
-
return input;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
return [].concat(input).map((item: any) => {
|
|
331
|
-
return this.mapItem(item, options);
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
private mapMap(input: any, options: JsonSerializerOptions) {
|
|
336
|
-
if (isNil(input)) {
|
|
337
|
-
return input;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
return [...input.entries()].reduce((obj, [key, item]) => {
|
|
341
|
-
return {
|
|
342
|
-
...obj,
|
|
343
|
-
[key]: this.mapItem(item, options)
|
|
344
|
-
};
|
|
345
|
-
}, {});
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
private mapItem(input: any, {id, ...options}: JsonSerializerOptions) {
|
|
349
|
-
if (!id && input) {
|
|
350
|
-
return this.compile(classOf(input), options.groups).fn(input, options);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return id ? this.execMapper(id, input, options) : input;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
private mapJSON(input: any, {id, ...options}: JsonSerializerOptions) {
|
|
357
|
-
if (isMongooseObject(input)) {
|
|
358
|
-
return input.toJSON({...options, self: undefined, id});
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
id = id || nameOf(classOf(input));
|
|
362
|
-
|
|
363
|
-
if (this.mappers[id] && (isDate(input) || isMomentObject(input))) {
|
|
364
|
-
return this.execMapper(id, input, options);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
input = input.toJSON();
|
|
368
|
-
|
|
369
|
-
return isObject(input) ? this.execMapper(id, input, options) : input;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {MetadataTypes, Type} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
import {JsonMapperMethods} from "../interfaces/JsonMapperMethods.js";
|
|
4
|
-
import {JsonMapperGlobalOptions} from "./JsonMapperGlobalOptions.js";
|
|
5
|
-
|
|
6
|
-
export interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C>, Pick<JsonMapperGlobalOptions, "strictGroups"> {
|
|
7
|
-
/**
|
|
8
|
-
* Types used to map complex types (Symbol, Number, String, etc...)
|
|
9
|
-
*/
|
|
10
|
-
types?: Map<Type<any> | Symbol | string, JsonMapperMethods>;
|
|
11
|
-
/**
|
|
12
|
-
* useAlias mapping
|
|
13
|
-
*/
|
|
14
|
-
useAlias?: boolean;
|
|
15
|
-
|
|
16
|
-
[key: string]: any;
|
|
17
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import {Writer} from "./Writer.js";
|
|
2
|
-
|
|
3
|
-
describe("Writer", () => {
|
|
4
|
-
it("should generate code for if/else condition", () => {
|
|
5
|
-
const writer = new Writer();
|
|
6
|
-
|
|
7
|
-
writer.if("condition").add("return true;").else().add("return false;");
|
|
8
|
-
|
|
9
|
-
expect(writer.toString()).toMatchSnapshot();
|
|
10
|
-
});
|
|
11
|
-
});
|
package/src/domain/Writer.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
export class Writer {
|
|
2
|
-
static object = {
|
|
3
|
-
assign(...args: string[]) {
|
|
4
|
-
return `{ ${args.filter(Boolean).join(", ")} }`;
|
|
5
|
-
}
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
protected _root?: Writer;
|
|
9
|
-
protected body: (string | Writer)[] = [];
|
|
10
|
-
private _indent: boolean;
|
|
11
|
-
|
|
12
|
-
constructor(root?: Writer) {
|
|
13
|
-
this._root = root;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static indent(lines: string[]) {
|
|
17
|
-
return lines.map((p) => `\t${p}`);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static mapper(mapperId: string, key: string, options: string) {
|
|
21
|
-
return `execMapper('${mapperId}', ${key}, ${options})`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static mapperFrom(key: string, options: string) {
|
|
25
|
-
return `execMapper(nameOf(classOf(${key})), ${key}, ${options})`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static options(...args: string[]) {
|
|
29
|
-
args = args.filter(Boolean);
|
|
30
|
-
return !args.length ? "options" : Writer.object.assign("...options", ...args);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
add(...lines: (undefined | string | Writer)[]) {
|
|
34
|
-
lines = lines.filter((line) => line !== undefined);
|
|
35
|
-
|
|
36
|
-
lines.length && this.body.push(...(lines as string | Writer[]));
|
|
37
|
-
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
arrow(...args: string[]) {
|
|
42
|
-
this.add(`(${args}) => {`);
|
|
43
|
-
|
|
44
|
-
const writer = this.new();
|
|
45
|
-
|
|
46
|
-
this.add("}");
|
|
47
|
-
|
|
48
|
-
return writer;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
build(): string[] {
|
|
52
|
-
const result = this.body.flatMap((line) => {
|
|
53
|
-
return line instanceof Writer ? line.build() : line;
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return this._indent ? result.map((item) => "\t" + item) : result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
callMapper(id: string, key: string, ...options: string[]) {
|
|
60
|
-
return this.set(key, Writer.mapper(id, key, Writer.options(...options)));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
returnCallMapper(id: string, key: string, ...options: string[]) {
|
|
64
|
-
return this.return(Writer.mapper(id, key, Writer.options(...options)));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const(name: string, line: string) {
|
|
68
|
-
this.add(`const ${name} = ${line};`);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
each(iterable: string, args: string[] = []) {
|
|
73
|
-
const writer = this.add(iterable + ".forEach((" + args.join(", ") + ") => {").new();
|
|
74
|
-
|
|
75
|
-
this.add("});");
|
|
76
|
-
|
|
77
|
-
return writer;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if(condition: string) {
|
|
81
|
-
const writer = new IfWriter(condition, this);
|
|
82
|
-
|
|
83
|
-
this.add(writer);
|
|
84
|
-
|
|
85
|
-
return writer;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
switch(input: string) {
|
|
89
|
-
const sw = new SwitchWriter(input);
|
|
90
|
-
|
|
91
|
-
this.add(sw);
|
|
92
|
-
|
|
93
|
-
return sw;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
indent(indent: boolean) {
|
|
97
|
-
this._indent = indent;
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
new(indent = true) {
|
|
102
|
-
const writer = new Writer(this.root());
|
|
103
|
-
writer.indent(indent);
|
|
104
|
-
this.add(writer);
|
|
105
|
-
|
|
106
|
-
return writer;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return(line: string) {
|
|
110
|
-
this.add(`return ${line};`);
|
|
111
|
-
return this.root();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
root() {
|
|
115
|
-
return this._root || this;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
set(left: string, right: string) {
|
|
119
|
-
this.add(`${left} = ${right};`);
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
toString() {
|
|
124
|
-
return this.build().join("\n");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
class IfWriter extends Writer {
|
|
129
|
-
protected elseWriter?: Writer;
|
|
130
|
-
|
|
131
|
-
constructor(
|
|
132
|
-
protected condition: string,
|
|
133
|
-
root: Writer
|
|
134
|
-
) {
|
|
135
|
-
super();
|
|
136
|
-
this._root = root;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
else() {
|
|
140
|
-
const writer = new Writer(this._root);
|
|
141
|
-
|
|
142
|
-
this.elseWriter = writer;
|
|
143
|
-
|
|
144
|
-
return writer;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
build() {
|
|
148
|
-
return [
|
|
149
|
-
`if (${this.condition}) {`,
|
|
150
|
-
...Writer.indent(super.build()),
|
|
151
|
-
"}",
|
|
152
|
-
this.elseWriter ? [`else {`, ...Writer.indent(this.elseWriter.build()), "}"] : []
|
|
153
|
-
].flat();
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
class SwitchWriter extends Writer {
|
|
158
|
-
#map = new Map<string, Writer>();
|
|
159
|
-
|
|
160
|
-
constructor(private input: string) {
|
|
161
|
-
super();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
case(condition: string) {
|
|
165
|
-
const writer = new Writer();
|
|
166
|
-
this.#map.set(condition, writer);
|
|
167
|
-
return writer;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
build() {
|
|
171
|
-
return [
|
|
172
|
-
`switch (${this.input}) {`,
|
|
173
|
-
...Writer.indent(
|
|
174
|
-
Array.from(this.#map.entries()).flatMap(([condition, writer]) => {
|
|
175
|
-
return [condition === "default" ? `${condition}:` : `case ${condition}:`, ...Writer.indent(writer.build())];
|
|
176
|
-
})
|
|
177
|
-
),
|
|
178
|
-
"}"
|
|
179
|
-
].flat();
|
|
180
|
-
}
|
|
181
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Automatically generated by @tsed/barrels.
|
|
3
|
-
*/
|
|
4
|
-
export * from "./components/DateMapper.js";
|
|
5
|
-
export * from "./components/PrimitiveMapper.js";
|
|
6
|
-
export * from "./components/SymbolMapper.js";
|
|
7
|
-
export * from "./decorators/afterDeserialize.js";
|
|
8
|
-
export * from "./decorators/beforeDeserialize.js";
|
|
9
|
-
export * from "./decorators/jsonMapper.js";
|
|
10
|
-
export * from "./decorators/onDeserialize.js";
|
|
11
|
-
export * from "./decorators/onSerialize.js";
|
|
12
|
-
export * from "./domain/JsonDeserializer.js";
|
|
13
|
-
export * from "./domain/JsonDeserializerOptions.js";
|
|
14
|
-
export * from "./domain/JsonMapperCompiler.js";
|
|
15
|
-
export * from "./domain/JsonMapperGlobalOptions.js";
|
|
16
|
-
export * from "./domain/JsonMapperSettings.js";
|
|
17
|
-
export * from "./domain/JsonMapperTypesContainer.js";
|
|
18
|
-
export * from "./domain/JsonSerializer.js";
|
|
19
|
-
export * from "./domain/JsonSerializerOptions.js";
|
|
20
|
-
export * from "./domain/Writer.js";
|
|
21
|
-
export * from "./hooks/alterAfterDeserialize.js";
|
|
22
|
-
export * from "./hooks/alterBeforeDeserialize.js";
|
|
23
|
-
export * from "./hooks/alterOnDeserialize.js";
|
|
24
|
-
export * from "./hooks/alterOnSerialize.js";
|
|
25
|
-
export * from "./interfaces/JsonMapperMethods.js";
|
|
26
|
-
export * from "./utils/deserialize.js";
|
|
27
|
-
export * from "./utils/getObjectProperties.js";
|
|
28
|
-
export * from "./utils/serialize.js";
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {Type} from "@tsed/core";
|
|
2
|
-
|
|
3
|
-
export interface JsonMapperCtx<T = any, C = any> {
|
|
4
|
-
collectionType: Type<C> | undefined;
|
|
5
|
-
type: Type<T> | T;
|
|
6
|
-
next: JsonMapperNext;
|
|
7
|
-
options: {
|
|
8
|
-
format?: string;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface JsonMapperNext {
|
|
13
|
-
(obj: any): any;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface JsonMapperMethods {
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
* @param obj
|
|
20
|
-
* @param {JsonMapperNext} ctx
|
|
21
|
-
* @returns {any}
|
|
22
|
-
*/
|
|
23
|
-
deserialize<T = any, C = any>(obj: any, ctx: JsonMapperCtx<T, C>): any;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
* @param obj
|
|
28
|
-
* @param ctx
|
|
29
|
-
* @returns {any}
|
|
30
|
-
*/
|
|
31
|
-
serialize(obj: any, ctx: JsonMapperCtx<any>): any;
|
|
32
|
-
}
|