@tsed/json-mapper 7.34.2 → 7.35.0-beta.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/domain/JsonMapperCompiler.js +156 -0
- package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/cjs/domain/JsonSerializer.js +214 -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 +111 -0
- package/lib/cjs/domain/Writer.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 +6 -0
- package/lib/cjs/index.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/domain/JsonMapperCompiler.js +152 -0
- package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/esm/domain/JsonSerializer.js +210 -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 +108 -0
- package/lib/esm/domain/Writer.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 +6 -0
- package/lib/esm/index.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/domain/JsonMapperCompiler.d.ts +52 -0
- package/lib/types/domain/JsonSerializer.d.ts +20 -0
- package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
- package/lib/types/domain/Writer.d.ts +34 -0
- package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/utils/getObjectProperties.d.ts +1 -0
- package/lib/types/utils/serialize.d.ts +2 -18
- package/package.json +11 -8
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonSerializer = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const schema_1 = require("@tsed/schema");
|
|
6
|
+
const alterOnSerialize_1 = require("../hooks/alterOnSerialize");
|
|
7
|
+
const getObjectProperties_1 = require("../utils/getObjectProperties");
|
|
8
|
+
const JsonMapperCompiler_1 = require("./JsonMapperCompiler");
|
|
9
|
+
const JsonMapperTypesContainer_1 = require("./JsonMapperTypesContainer");
|
|
10
|
+
const Writer_1 = require("./Writer");
|
|
11
|
+
function getBestType(type, obj) {
|
|
12
|
+
const dataType = (0, core_1.classOf)(obj);
|
|
13
|
+
if (dataType && !(0, core_1.isClassObject)(dataType)) {
|
|
14
|
+
return dataType;
|
|
15
|
+
}
|
|
16
|
+
return type || Object;
|
|
17
|
+
}
|
|
18
|
+
class JsonSerializer extends JsonMapperCompiler_1.JsonMapperCompiler {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
this.addTypeMapper(Object, this.mapObject.bind(this));
|
|
22
|
+
this.addTypeMapper(Array, this.mapArray.bind(this));
|
|
23
|
+
this.addTypeMapper(Map, this.mapMap.bind(this));
|
|
24
|
+
this.addTypeMapper(Set, this.mapSet.bind(this));
|
|
25
|
+
this.addGlobal("mapJSON", this.mapJSON.bind(this));
|
|
26
|
+
}
|
|
27
|
+
map(input, options = {}) {
|
|
28
|
+
if ((0, core_1.isNil)(input)) {
|
|
29
|
+
return input;
|
|
30
|
+
}
|
|
31
|
+
options = this.mapOptions(options);
|
|
32
|
+
const model = getBestType(options.type, input);
|
|
33
|
+
const mapper = this.compile(model, options.groups);
|
|
34
|
+
console.log(input, model);
|
|
35
|
+
return mapper.fn(input, this.createContext(options));
|
|
36
|
+
}
|
|
37
|
+
alterValue(schemaId, value, options) {
|
|
38
|
+
return (0, alterOnSerialize_1.alterOnSerialize)(this.schemes[schemaId], value, options);
|
|
39
|
+
}
|
|
40
|
+
createMapper(model, id, groups) {
|
|
41
|
+
const entity = schema_1.JsonEntityStore.from(model);
|
|
42
|
+
const properties = new Set();
|
|
43
|
+
const schemaProperties = [...(0, schema_1.getPropertiesStores)(entity).values()];
|
|
44
|
+
const writer = new Writer_1.Writer().arrow("input", "options");
|
|
45
|
+
writer.if("isNil(input)").return("input");
|
|
46
|
+
// prepare options
|
|
47
|
+
writer.const("obj", "{}");
|
|
48
|
+
writer.set("options", "{...options, self: input}");
|
|
49
|
+
// detect some special case
|
|
50
|
+
writer.add(this.mapPrecondition(id));
|
|
51
|
+
if (!schemaProperties.length) {
|
|
52
|
+
return writer.return("input").root().toString();
|
|
53
|
+
}
|
|
54
|
+
// properties
|
|
55
|
+
writer.add(...schemaProperties.flatMap((propertyStore) => {
|
|
56
|
+
properties.add(propertyStore.propertyKey);
|
|
57
|
+
if (propertyStore.schema?.$hooks?.has("groups") && this.alterGroups(propertyStore.schema, groups)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
return this.mapProperty(propertyStore, id, groups);
|
|
61
|
+
}));
|
|
62
|
+
// discriminator
|
|
63
|
+
writer.add(this.mapDiscriminator(entity));
|
|
64
|
+
// additional properties
|
|
65
|
+
writer.add(this.mapAdditionalProperties(entity, properties));
|
|
66
|
+
return writer.return("obj").root().toString();
|
|
67
|
+
}
|
|
68
|
+
mapOptions({ groups = false, useAlias = true, types, ...options }) {
|
|
69
|
+
const customMappers = {};
|
|
70
|
+
types = types || (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
|
|
71
|
+
types.forEach((mapper, model) => {
|
|
72
|
+
if (![Array, Set, Map].includes(model)) {
|
|
73
|
+
this.addTypeMapper(model, mapper.serialize.bind(mapper));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
...options,
|
|
78
|
+
groups,
|
|
79
|
+
useAlias,
|
|
80
|
+
customMappers
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
mapProperty(propertyStore, id, groups) {
|
|
84
|
+
const key = String(propertyStore.propertyKey);
|
|
85
|
+
const aliasKey = String(propertyStore.parent.schema.getAliasOf(key) || key);
|
|
86
|
+
const schemaId = `${id}:${key}`;
|
|
87
|
+
let writer = new Writer_1.Writer().add(`// Map ${key} ${id} ${groups || ""}`);
|
|
88
|
+
// ignore hook (deprecated)
|
|
89
|
+
if (propertyStore.schema?.$hooks?.has("ignore")) {
|
|
90
|
+
this.schemes[schemaId] = propertyStore.schema;
|
|
91
|
+
writer = writer.if(`!alterIgnore('${schemaId}', {...options, self: input})`);
|
|
92
|
+
}
|
|
93
|
+
writer = writer.add(`let ${key} = input.${key};`).if(`${key} !== undefined`);
|
|
94
|
+
const format = propertyStore.itemSchema.get("format");
|
|
95
|
+
const formatOpts = format && `options: {format: '${format}'}`;
|
|
96
|
+
// pre hook
|
|
97
|
+
const hasSerializer = propertyStore.schema?.$hooks?.has("onSerialize");
|
|
98
|
+
if (hasSerializer) {
|
|
99
|
+
this.schemes[schemaId] = propertyStore.schema;
|
|
100
|
+
const opts = Writer_1.Writer.options(formatOpts);
|
|
101
|
+
writer.set(key, `alterValue('${schemaId}', ${key}, ${opts})`);
|
|
102
|
+
}
|
|
103
|
+
if (propertyStore.isCollection) {
|
|
104
|
+
const type = propertyStore.getBestType();
|
|
105
|
+
const nestedMapper = this.compile(type, groups);
|
|
106
|
+
writer.callMapper((0, core_1.nameOf)(propertyStore.collectionType), key, `id: '${nestedMapper.id}'`, formatOpts);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const type = propertyStore.getBestType();
|
|
110
|
+
const nestedMapper = this.compile(type, groups);
|
|
111
|
+
const fill = (writer) => writer.callMapper(nestedMapper.id, key, formatOpts);
|
|
112
|
+
if (hasSerializer) {
|
|
113
|
+
fill(writer.if(`${key} === input.${key}`));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
fill(writer);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (aliasKey !== key) {
|
|
120
|
+
writer.set(`obj[options.useAlias ? '${aliasKey}' : '${key}']`, key);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
writer.set(`obj.${key}`, key);
|
|
124
|
+
}
|
|
125
|
+
return writer.root();
|
|
126
|
+
}
|
|
127
|
+
mapPrecondition(id) {
|
|
128
|
+
const writer = new Writer_1.Writer();
|
|
129
|
+
writer.if("input && isCollection(input)").return(Writer_1.Writer.mapperFrom("input", `{...options, id: '${id}'}`));
|
|
130
|
+
writer.if("hasJsonMethod(input)").return(`mapJSON(input, {...options, id: '${id}'})`);
|
|
131
|
+
return writer;
|
|
132
|
+
}
|
|
133
|
+
mapDiscriminator(entity) {
|
|
134
|
+
if (entity.discriminatorAncestor) {
|
|
135
|
+
const discriminator = entity.discriminatorAncestor.schema.discriminator();
|
|
136
|
+
const type = discriminator.getDefaultValue(entity.target);
|
|
137
|
+
if (type) {
|
|
138
|
+
const writer = new Writer_1.Writer();
|
|
139
|
+
writer.if(`!obj.${discriminator.propertyName}`).set(`obj.${discriminator.propertyName}`, `'${type}'`);
|
|
140
|
+
return writer;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
mapAdditionalProperties(entity, properties) {
|
|
145
|
+
const additionalProperties = !!entity.schema.get("additionalProperties");
|
|
146
|
+
if (additionalProperties) {
|
|
147
|
+
const exclude = [...properties.values()].map((key) => `'${key}'`).join(", ");
|
|
148
|
+
const writer = new Writer_1.Writer();
|
|
149
|
+
writer.add("// add additional properties");
|
|
150
|
+
writer.each("objectKeys(input)", ["key"]).if(`![${exclude}].includes(key)`).set("obj[key]", "input[key]");
|
|
151
|
+
return writer;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
mapObject(input, { type, ...options }) {
|
|
155
|
+
if (input && (0, core_1.isCollection)(input)) {
|
|
156
|
+
return this.execMapper((0, core_1.nameOf)((0, core_1.classOf)(input)), input, options);
|
|
157
|
+
}
|
|
158
|
+
if ((0, core_1.hasJsonMethod)(input)) {
|
|
159
|
+
return this.mapJSON(input, options);
|
|
160
|
+
}
|
|
161
|
+
return (0, getObjectProperties_1.getObjectProperties)(input)
|
|
162
|
+
.filter(([, value]) => value !== undefined)
|
|
163
|
+
.reduce((obj, [key, value]) => {
|
|
164
|
+
if ((0, core_1.isNil)(value)) {
|
|
165
|
+
return {
|
|
166
|
+
...obj,
|
|
167
|
+
[key]: value
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const mapper = this.compile((0, core_1.classOf)(value), options.groups);
|
|
171
|
+
return {
|
|
172
|
+
...obj,
|
|
173
|
+
[key]: mapper.fn(value, options)
|
|
174
|
+
};
|
|
175
|
+
}, {});
|
|
176
|
+
}
|
|
177
|
+
mapSet(input, options) {
|
|
178
|
+
return [...input.values()].map((item) => {
|
|
179
|
+
return this.mapItem(item, options);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
mapArray(input, options) {
|
|
183
|
+
return [].concat(input).map((item) => {
|
|
184
|
+
return this.mapItem(item, options);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
mapMap(input, options) {
|
|
188
|
+
return [...input.entries()].reduce((obj, [key, item]) => {
|
|
189
|
+
return {
|
|
190
|
+
...obj,
|
|
191
|
+
[key]: this.mapItem(item, options)
|
|
192
|
+
};
|
|
193
|
+
}, {});
|
|
194
|
+
}
|
|
195
|
+
mapItem(input, { id, ...options }) {
|
|
196
|
+
if (!id && input) {
|
|
197
|
+
return this.compile((0, core_1.classOf)(input), options.groups).fn(input, options);
|
|
198
|
+
}
|
|
199
|
+
return id ? this.execMapper(id, input, options) : input;
|
|
200
|
+
}
|
|
201
|
+
mapJSON(input, { id, ...options }) {
|
|
202
|
+
if ((0, core_1.isMongooseObject)(input)) {
|
|
203
|
+
return input.toJSON({ ...options, id });
|
|
204
|
+
}
|
|
205
|
+
id = id || (0, core_1.nameOf)((0, core_1.classOf)(input));
|
|
206
|
+
if (this.mappers[id] && ((0, core_1.isDate)(input) || (0, core_1.isMomentObject)(input))) {
|
|
207
|
+
return this.execMapper(id, input, options);
|
|
208
|
+
}
|
|
209
|
+
input = input.toJSON();
|
|
210
|
+
return (0, core_1.isObject)(input) ? this.execMapper(id, input, options) : input;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.JsonSerializer = JsonSerializer;
|
|
214
|
+
//# sourceMappingURL=JsonSerializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonSerializer.js","sourceRoot":"","sources":["../../../src/domain/JsonSerializer.ts"],"names":[],"mappings":";;;AAAA,qCAYoB;AACpB,yCAAqG;AACrG,gEAA2D;AAC3D,sEAAiE;AACjE,6DAAwD;AACxD,yEAA8D;AAE9D,qCAAgC;AAEhC,SAAS,WAAW,CAAC,IAAe,EAAE,GAAQ;IAC5C,MAAM,QAAQ,GAAG,IAAA,cAAO,EAAC,GAAG,CAAC,CAAC;IAE9B,IAAI,QAAQ,IAAI,CAAC,IAAA,oBAAa,EAAC,QAAQ,CAAC,EAAE;QACxC,OAAO,QAAQ,CAAC;KACjB;IAED,OA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AAC,IAAI,CAAC,IAAA,aAAM,EAAC,KAAK,CAAC,IAAI,IAAA,qBAAc,EAAC,KAAK,CAAC,CAAC,EAAE;YAChE,OAAO,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;SAC5C;QAED,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QAEvB,OAAO,IAAA,eAAQ,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACvE,CAAC;CACF;AA9PD,wCA8PC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonSerializerOptions.js","sourceRoot":"","sources":["../../../src/domain/JsonSerializerOptions.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Writer = void 0;
|
|
4
|
+
class Writer {
|
|
5
|
+
static object = {
|
|
6
|
+
assign(...args) {
|
|
7
|
+
return `{ ${args.filter(Boolean).join(", ")} }`;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
_root;
|
|
11
|
+
body = [];
|
|
12
|
+
_indent;
|
|
13
|
+
constructor(root) {
|
|
14
|
+
this._root = root;
|
|
15
|
+
}
|
|
16
|
+
static indent(lines) {
|
|
17
|
+
return lines.map((p) => `\t${p}`);
|
|
18
|
+
}
|
|
19
|
+
static mapper(mapperId, key, options) {
|
|
20
|
+
return `execMapper('${mapperId}', ${key}, ${options})`;
|
|
21
|
+
}
|
|
22
|
+
static mapperFrom(key, options) {
|
|
23
|
+
return `execMapper(nameOf(classOf(${key})), ${key}, ${options})`;
|
|
24
|
+
}
|
|
25
|
+
static options(...args) {
|
|
26
|
+
args = args.filter(Boolean);
|
|
27
|
+
return !args.length ? "options" : Writer.object.assign("...options", ...args);
|
|
28
|
+
}
|
|
29
|
+
add(...lines) {
|
|
30
|
+
lines = lines.filter((line) => line !== undefined);
|
|
31
|
+
lines.length && this.body.push(...lines);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
arrow(...args) {
|
|
35
|
+
this.add(`(${args}) => {`);
|
|
36
|
+
const writer = this.new();
|
|
37
|
+
this.add("}");
|
|
38
|
+
return writer;
|
|
39
|
+
}
|
|
40
|
+
build() {
|
|
41
|
+
const result = this.body.flatMap((line) => {
|
|
42
|
+
return line instanceof Writer ? line.build() : line;
|
|
43
|
+
});
|
|
44
|
+
return this._indent ? result.map((item) => "\t" + item) : result;
|
|
45
|
+
}
|
|
46
|
+
callMapper(id, key, ...options) {
|
|
47
|
+
return this.set(key, Writer.mapper(id, key, Writer.options(...options)));
|
|
48
|
+
}
|
|
49
|
+
const(name, line) {
|
|
50
|
+
this.add(`const ${name} = ${line};`);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
each(iterable, args = []) {
|
|
54
|
+
const writer = this.add(iterable + ".forEach((" + args.join(", ") + ") => {").new();
|
|
55
|
+
this.add("});");
|
|
56
|
+
return writer;
|
|
57
|
+
}
|
|
58
|
+
if(condition) {
|
|
59
|
+
const writer = new IfWriter(condition, this);
|
|
60
|
+
this.add(writer);
|
|
61
|
+
return writer;
|
|
62
|
+
}
|
|
63
|
+
indent(indent) {
|
|
64
|
+
this._indent = indent;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
new(indent = true) {
|
|
68
|
+
const writer = new Writer(this.root());
|
|
69
|
+
writer.indent(indent);
|
|
70
|
+
this.add(writer);
|
|
71
|
+
return writer;
|
|
72
|
+
}
|
|
73
|
+
return(line) {
|
|
74
|
+
this.add(`return ${line};`);
|
|
75
|
+
return this.root();
|
|
76
|
+
}
|
|
77
|
+
root() {
|
|
78
|
+
return this._root || this;
|
|
79
|
+
}
|
|
80
|
+
set(left, right) {
|
|
81
|
+
this.add(`${left} = ${right};`);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
toString() {
|
|
85
|
+
return this.build().join("\n");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.Writer = Writer;
|
|
89
|
+
class IfWriter extends Writer {
|
|
90
|
+
condition;
|
|
91
|
+
elseWriter;
|
|
92
|
+
constructor(condition, root) {
|
|
93
|
+
super();
|
|
94
|
+
this.condition = condition;
|
|
95
|
+
this._root = root;
|
|
96
|
+
}
|
|
97
|
+
else() {
|
|
98
|
+
const writer = new Writer(this._root);
|
|
99
|
+
this.elseWriter = writer;
|
|
100
|
+
return writer;
|
|
101
|
+
}
|
|
102
|
+
build() {
|
|
103
|
+
return [
|
|
104
|
+
`if (${this.condition}) {`,
|
|
105
|
+
...Writer.indent(super.build()),
|
|
106
|
+
"}",
|
|
107
|
+
this.elseWriter ? [`else {`, ...Writer.indent(this.elseWriter.build()), "}"] : []
|
|
108
|
+
].flat();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=Writer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Writer.js","sourceRoot":"","sources":["../../../src/domain/Writer.ts"],"names":[],"mappings":";;;AAAA,MAAa,MAAM;IACjB,MAAM,CAAC,MAAM,GAAG;QACd,MAAM,CAAC,GAAG,IAAc;YACtB,OAAO,KAAK,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAClD,CAAC;KACF,CAAC;IAEQ,KAAK,CAAU;IACf,IAAI,GAAwB,EAAE,CAAC;IACjC,OAAO,CAAU;IAEzB,YAAY,IAAa;QACvB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,KAAe;QAC3B,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAA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AAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE;SAClF,CAAC,IAAI,EAAE,CAAC;IACX,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.alterOnSerialize = void 0;
|
|
4
|
+
function alterOnSerialize(schema, value, options) {
|
|
5
|
+
return schema.$hooks.alter("onSerialize", value, [options]);
|
|
6
|
+
}
|
|
7
|
+
exports.alterOnSerialize = alterOnSerialize;
|
|
8
|
+
//# sourceMappingURL=alterOnSerialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alterOnSerialize.js","sourceRoot":"","sources":["../../../src/hooks/alterOnSerialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,gBAAgB,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IACvF,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC;AAFD,4CAEC"}
|
package/lib/cjs/index.js
CHANGED
|
@@ -15,12 +15,18 @@ tslib_1.__exportStar(require("./decorators/beforeDeserialize"), exports);
|
|
|
15
15
|
tslib_1.__exportStar(require("./decorators/jsonMapper"), exports);
|
|
16
16
|
tslib_1.__exportStar(require("./decorators/onDeserialize"), exports);
|
|
17
17
|
tslib_1.__exportStar(require("./decorators/onSerialize"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./domain/JsonMapperCompiler"), exports);
|
|
18
19
|
tslib_1.__exportStar(require("./domain/JsonMapperContext"), exports);
|
|
19
20
|
tslib_1.__exportStar(require("./domain/JsonMapperSettings"), exports);
|
|
20
21
|
tslib_1.__exportStar(require("./domain/JsonMapperTypesContainer"), exports);
|
|
22
|
+
tslib_1.__exportStar(require("./domain/JsonSerializer"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./domain/JsonSerializerOptions"), exports);
|
|
24
|
+
tslib_1.__exportStar(require("./domain/Writer"), exports);
|
|
21
25
|
tslib_1.__exportStar(require("./hooks/alterAfterDeserialize"), exports);
|
|
22
26
|
tslib_1.__exportStar(require("./hooks/alterBeforeDeserialize"), exports);
|
|
27
|
+
tslib_1.__exportStar(require("./hooks/alterOnSerialize"), exports);
|
|
23
28
|
tslib_1.__exportStar(require("./interfaces/JsonMapperMethods"), exports);
|
|
24
29
|
tslib_1.__exportStar(require("./utils/deserialize"), exports);
|
|
30
|
+
tslib_1.__exportStar(require("./utils/getObjectProperties"), exports);
|
|
25
31
|
tslib_1.__exportStar(require("./utils/serialize"), exports);
|
|
26
32
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,mEAAyC;AACzC,kEAAwC;AACxC,iEAAuC;AACvC,uEAA6C;AAC7C,iEAAuC;AACvC,oEAA0C;AAC1C,wEAA8C;AAC9C,yEAA+C;AAC/C,kEAAwC;AACxC,qEAA2C;AAC3C,mEAAyC;AACzC,qEAA2C;AAC3C,sEAA4C;AAC5C,4EAAkD;AAClD,wEAA8C;AAC9C,yEAA+C;AAC/C,yEAA+C;AAC/C,8DAAoC;AACpC,4DAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,mEAAyC;AACzC,kEAAwC;AACxC,iEAAuC;AACvC,uEAA6C;AAC7C,iEAAuC;AACvC,oEAA0C;AAC1C,wEAA8C;AAC9C,yEAA+C;AAC/C,kEAAwC;AACxC,qEAA2C;AAC3C,mEAAyC;AACzC,sEAA4C;AAC5C,qEAA2C;AAC3C,sEAA4C;AAC5C,4EAAkD;AAClD,kEAAwC;AACxC,yEAA+C;AAC/C,0DAAgC;AAChC,wEAA8C;AAC9C,yEAA+C;AAC/C,mEAAyC;AACzC,yEAA+C;AAC/C,8DAAoC;AACpC,sEAA4C;AAC5C,4DAAkC"}
|
|
@@ -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;AAEH,IAAa,UAAU,GAAvB,MAAa,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,IA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