@tsed/json-mapper 7.0.0-beta.8 → 7.0.0-rc.2
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/coverage.json +6 -0
- package/jest.config.js +9 -0
- package/lib/cjs/domain/JsonMapperContext.js +4 -0
- package/lib/cjs/domain/JsonMapperContext.js.map +1 -1
- package/lib/cjs/hooks/alterAfterDeserialize.js +1 -2
- package/lib/cjs/hooks/alterAfterDeserialize.js.map +1 -1
- package/lib/cjs/hooks/alterBeforeDeserialize.js +1 -2
- package/lib/cjs/hooks/alterBeforeDeserialize.js.map +1 -1
- package/lib/cjs/utils/deserialize.js +28 -13
- package/lib/cjs/utils/deserialize.js.map +1 -1
- package/lib/cjs/utils/serialize.js +13 -5
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/domain/JsonMapperContext.js +4 -0
- package/lib/esm/domain/JsonMapperContext.js.map +1 -1
- package/lib/esm/hooks/alterAfterDeserialize.js +1 -2
- package/lib/esm/hooks/alterAfterDeserialize.js.map +1 -1
- package/lib/esm/hooks/alterBeforeDeserialize.js +1 -2
- package/lib/esm/hooks/alterBeforeDeserialize.js.map +1 -1
- package/lib/esm/utils/deserialize.js +29 -14
- package/lib/esm/utils/deserialize.js.map +1 -1
- package/lib/esm/utils/serialize.js +14 -6
- package/lib/esm/utils/serialize.js.map +1 -1
- package/package.json +15 -11
- package/readme.md +3 -2
package/coverage.json
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "json-mapper"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: require("./coverage.json")
|
|
8
|
+
}
|
|
9
|
+
};
|
|
@@ -6,6 +6,10 @@ const core_1 = require("@tsed/core");
|
|
|
6
6
|
* @ignore
|
|
7
7
|
*/
|
|
8
8
|
class JsonMapperContext {
|
|
9
|
+
collectionType;
|
|
10
|
+
type;
|
|
11
|
+
options;
|
|
12
|
+
_next;
|
|
9
13
|
constructor({ type, collectionType, next, options }) {
|
|
10
14
|
this.type = type;
|
|
11
15
|
this.collectionType = (0, core_1.isCollection)(collectionType) ? collectionType : undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonMapperContext.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperContext.ts"],"names":[],"mappings":";;;AAAA,qCAA8C;AAS9C;;GAEG;AACH,MAAa,iBAAiB;
|
|
1
|
+
{"version":3,"file":"JsonMapperContext.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperContext.ts"],"names":[],"mappings":";;;AAAA,qCAA8C;AAS9C;;GAEG;AACH,MAAa,iBAAiB;IACnB,cAAc,CAAwB;IACtC,IAAI,CAAkB;IACtB,OAAO,CAAM;IACd,KAAK,CAAoC;IAEjD,YAAY,EAAC,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,EAAyB;QACvE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,cAAc,GAAG,IAAA,mBAAY,EAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC;QAChF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,IAAI,CAAC,IAAS;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACtB,GAAG,IAAI,CAAC,OAAO;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;CACF;AAnBD,8CAmBC","sourcesContent":["import {isCollection, Type} from \"@tsed/core\";\nimport {JsonMapperCtx} from \"../interfaces/JsonMapperMethods\";\n/**\n * @ignore\n */\nexport interface JsonMapperContextProps extends Partial<Omit<JsonMapperCtx, \"next\">> {\n next: (data: any, options: any) => void;\n options: any;\n}\n/**\n * @ignore\n */\nexport class JsonMapperContext implements JsonMapperCtx {\n readonly collectionType: Type<any> | undefined;\n readonly type: Type<any> | any;\n readonly options: any;\n private _next: (data: any, options: any) => void;\n\n constructor({type, collectionType, next, options}: JsonMapperContextProps) {\n this.type = type;\n this.collectionType = isCollection(collectionType) ? collectionType : undefined;\n this._next = next;\n this.options = options;\n }\n\n next(data: any) {\n return this._next(data, {\n ...this.options,\n type: this.type\n });\n }\n}\n"]}
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.alterAfterDeserialize = void 0;
|
|
4
4
|
function alterAfterDeserialize(data, schema, options) {
|
|
5
|
-
|
|
6
|
-
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("afterDeserialize", data, [options]);
|
|
5
|
+
return schema?.$hooks?.alter("afterDeserialize", data, [options]);
|
|
7
6
|
}
|
|
8
7
|
exports.alterAfterDeserialize = alterAfterDeserialize;
|
|
9
8
|
//# sourceMappingURL=alterAfterDeserialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alterAfterDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterAfterDeserialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,qBAAqB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY
|
|
1
|
+
{"version":3,"file":"alterAfterDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterAfterDeserialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,qBAAqB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;IACpF,OAAO,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,kBAAkB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACpE,CAAC;AAFD,sDAEC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterAfterDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"afterDeserialize\", data, [options]);\n}\n"]}
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.alterBeforeDeserialize = void 0;
|
|
4
4
|
function alterBeforeDeserialize(data, schema, options) {
|
|
5
|
-
|
|
6
|
-
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("beforeDeserialize", data, [options]);
|
|
5
|
+
return schema?.$hooks?.alter("beforeDeserialize", data, [options]);
|
|
7
6
|
}
|
|
8
7
|
exports.alterBeforeDeserialize = alterBeforeDeserialize;
|
|
9
8
|
//# sourceMappingURL=alterBeforeDeserialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alterBeforeDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterBeforeDeserialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,sBAAsB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY
|
|
1
|
+
{"version":3,"file":"alterBeforeDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterBeforeDeserialize.ts"],"names":[],"mappings":";;;AAEA,SAAgB,sBAAsB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;IACrF,OAAO,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,mBAAmB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACrE,CAAC;AAFD,wDAEC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterBeforeDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"beforeDeserialize\", data, [options]);\n}\n"]}
|
|
@@ -23,7 +23,6 @@ function alterValue(schema, value, options) {
|
|
|
23
23
|
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
24
24
|
}
|
|
25
25
|
function transformCollection(src, options) {
|
|
26
|
-
var _a;
|
|
27
26
|
const { types, type = Object, collectionType } = options;
|
|
28
27
|
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
29
28
|
type,
|
|
@@ -31,17 +30,16 @@ function transformCollection(src, options) {
|
|
|
31
30
|
options,
|
|
32
31
|
next: (data, { collectionType, ...options }) => deserialize(data, options)
|
|
33
32
|
});
|
|
34
|
-
return
|
|
33
|
+
return types?.get(options.collectionType)?.deserialize(src, context);
|
|
35
34
|
}
|
|
36
35
|
function transformType(src, options) {
|
|
37
|
-
var _a;
|
|
38
36
|
const { types, type = Object } = options;
|
|
39
37
|
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
40
38
|
type,
|
|
41
39
|
options,
|
|
42
40
|
next: (data, { type, ...options }) => deserialize(data, options)
|
|
43
41
|
});
|
|
44
|
-
return
|
|
42
|
+
return types?.get(type)?.deserialize(src, context);
|
|
45
43
|
}
|
|
46
44
|
function mapItemOptions(propStore, options) {
|
|
47
45
|
const itemOpts = {
|
|
@@ -63,6 +61,13 @@ function mapItemOptions(propStore, options) {
|
|
|
63
61
|
}
|
|
64
62
|
return itemOpts;
|
|
65
63
|
}
|
|
64
|
+
function getAdditionalProperties(nbProps, store, options) {
|
|
65
|
+
const additionalProperties = store.schema.get("additionalProperties");
|
|
66
|
+
if ((0, core_1.isBoolean)(additionalProperties) || (0, core_1.isClass)(additionalProperties)) {
|
|
67
|
+
return additionalProperties;
|
|
68
|
+
}
|
|
69
|
+
return nbProps === 0 || !!options.additionalProperties;
|
|
70
|
+
}
|
|
66
71
|
/**
|
|
67
72
|
* Transform given plain object to class.
|
|
68
73
|
* @param src
|
|
@@ -75,7 +80,7 @@ function plainObjectToClass(src, options) {
|
|
|
75
80
|
const { type, store = schema_1.JsonEntityStore.from(type) } = options;
|
|
76
81
|
const propertiesMap = (0, schema_1.getProperties)(store, { ...options, withIgnoredProps: true });
|
|
77
82
|
let keys = new Set((0, core_1.objectKeys)(src));
|
|
78
|
-
const additionalProperties = propertiesMap.size
|
|
83
|
+
const additionalProperties = getAdditionalProperties(propertiesMap.size, store, options);
|
|
79
84
|
src = (0, alterBeforeDeserialize_1.alterBeforeDeserialize)(src, store.schema, options);
|
|
80
85
|
const out = new type(src);
|
|
81
86
|
propertiesMap.forEach((propStore) => {
|
|
@@ -94,14 +99,25 @@ function plainObjectToClass(src, options) {
|
|
|
94
99
|
type: value === src[key] ? itemOptions.type : undefined,
|
|
95
100
|
collectionType: propStore.collectionType
|
|
96
101
|
});
|
|
97
|
-
if (value !== undefined) {
|
|
102
|
+
if (value !== undefined && !propStore.schema.isReadOnly) {
|
|
98
103
|
out[propStore.propertyName] = value;
|
|
99
104
|
}
|
|
100
105
|
});
|
|
101
106
|
if (additionalProperties) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
if ((0, core_1.isBoolean)(additionalProperties)) {
|
|
108
|
+
keys.forEach((key) => {
|
|
109
|
+
out[key] = src[key];
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const type = additionalProperties.getComputedType();
|
|
114
|
+
keys.forEach((key) => {
|
|
115
|
+
out[key] = deserialize(src[key], {
|
|
116
|
+
...options,
|
|
117
|
+
type
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
105
121
|
}
|
|
106
122
|
return (0, alterAfterDeserialize_1.alterAfterDeserialize)(out, store.schema, options);
|
|
107
123
|
}
|
|
@@ -113,7 +129,7 @@ function buildOptions(options) {
|
|
|
113
129
|
}
|
|
114
130
|
options.type = options.store.computedType;
|
|
115
131
|
options.collectionType = options.store.collectionType;
|
|
116
|
-
|
|
132
|
+
options.store = undefined;
|
|
117
133
|
}
|
|
118
134
|
return {
|
|
119
135
|
groups: false,
|
|
@@ -130,7 +146,6 @@ function buildOptions(options) {
|
|
|
130
146
|
* @param options
|
|
131
147
|
*/
|
|
132
148
|
function deserialize(src, options = {}) {
|
|
133
|
-
var _a, _b;
|
|
134
149
|
options = buildOptions(options);
|
|
135
150
|
if (!isDeserializable(src, options)) {
|
|
136
151
|
return src;
|
|
@@ -139,12 +154,12 @@ function deserialize(src, options = {}) {
|
|
|
139
154
|
options.collectionType = Array;
|
|
140
155
|
}
|
|
141
156
|
if (options.collectionType) {
|
|
142
|
-
if (!
|
|
157
|
+
if (!options.types?.has(options.collectionType)) {
|
|
143
158
|
throw new Error(`${(0, core_1.nameOf)(options.collectionType)} is not supported by JsonMapper.`);
|
|
144
159
|
}
|
|
145
160
|
return transformCollection(src, options);
|
|
146
161
|
}
|
|
147
|
-
if (
|
|
162
|
+
if (options.types?.has(options.type)) {
|
|
148
163
|
return transformType(src, options);
|
|
149
164
|
}
|
|
150
165
|
// class converter
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,qCAA4F;AAC5F,yCAAyH;AACzH,qCAAmC;AACnC,oCAAkC;AAClC,mCAAiC;AACjC,yCAAuC;AACvC,mCAAiC;AACjC,sCAAoC;AACpC,mEAA8D;AAC9D,iFAAsE;AACtE,0EAAqE;AACrE,4EAAuE;AA6BvE,SAAS,gBAAgB,CAAC,GAAQ,EAAE,OAAgC;IAClE,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,IAAI,IAAA,YAAK,EAAC,GAAG,CAAC,CAAC,IAAI,GAAG,KAAK,SAAS,EAAE;QACjE,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CAAC,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAA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AO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,IAAA,cAAO,EAAC,GAAG,CAAC,EAAE;QAC3C,OAAO,CAAC,cAAc,GAAG,KAAK,CAAC;KAChC;IAED,IAAI,OAAO,CAAC,cAAc,EAAE;QAC1B,IAAI,CAAC,CAAA,MAAA,OAAO,CAAC,KAAK,0CAAE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA,EAAE;YAC/C,MAAM,IAAI,KAAK,CAAC,GAAG,IAAA,aAAM,EAAC,OAAO,CAAC,cAAc,CAAC,kCAAkC,CAAC,CAAC;SACtF;QAED,OAAO,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC1C;IAED,IAAI,MAAA,OAAO,CAAC,KAAK,0CAAE,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACpC,OAAO,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACpC;IAED,kBAAkB;IAClB,OAAO,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAzBD,kCAyBC","sourcesContent":["import {isArray, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonEntityStore, JsonHookContext, JsonPropertyStore, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {alterAfterDeserialize} from \"../hooks/alterAfterDeserialize\";\nimport {alterBeforeDeserialize} from \"../hooks/alterBeforeDeserialize\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: WeakMap<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n /**\n * Accept additionalProperties or ignore it\n */\n additionalProperties?: boolean;\n /**\n * Use the store which have all metadata to deserialize correctly the model. This\n * property is useful when you deal with metadata parameters.\n */\n store?: JsonEntityStore;\n /**\n *\n */\n groups?: string[] | false;\n\n [key: string]: any;\n}\n\nfunction isDeserializable(obj: any, options: JsonDeserializerOptions) {\n if ((!!options.collectionType && isNil(obj)) || obj === undefined) {\n return false;\n }\n\n return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onDeserialize\", value, [options]);\n}\n\nfunction transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object, collectionType} = options;\n\n const context = new JsonMapperContext({\n type,\n collectionType,\n options,\n next: (data, {collectionType, ...options}) => deserialize(data, options)\n });\n\n return types?.get(options.collectionType)?.deserialize<T>(src, context);\n}\n\nfunction transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object} = options;\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data, {type, ...options}) => deserialize(data, options)\n });\n\n return types?.get(type)?.deserialize<T>(src, context);\n}\n\nfunction mapItemOptions(propStore: JsonPropertyStore, options: JsonDeserializerOptions) {\n const itemOpts: JsonDeserializerOptions = {\n ...options,\n store: undefined,\n type: propStore.computedType\n };\n\n if (propStore.schema.hasGenerics) {\n itemOpts.nestedGenerics = propStore.schema.nestedGenerics;\n } else if (propStore.schema.isGeneric && options.nestedGenerics) {\n const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;\n const genericLabels = propStore.parent.schema.genericLabels || [];\n\n itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (itemOpts.type instanceof JsonSchema) {\n itemOpts.type = itemOpts.type.getTarget();\n }\n\n itemOpts.nestedGenerics = nestedGenerics;\n }\n\n return itemOpts;\n}\n\n/**\n * Transform given plain object to class.\n * @param src\n * @param options\n */\nexport function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {\n if (isEmpty(src)) {\n return src;\n }\n\n const {type, store = JsonEntityStore.from(type)} = options;\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = new Set<any>(objectKeys(src));\n const additionalProperties = propertiesMap.size ? !!store.schema.get(\"additionalProperties\") || options.additionalProperties : true;\n\n src = alterBeforeDeserialize(src, store.schema, options);\n\n const out: any = new type(src);\n\n propertiesMap.forEach((propStore) => {\n const key = options.useAlias\n ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName\n : propStore.propertyName;\n\n keys.delete(key);\n\n if (alterIgnore(propStore.schema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n const itemOptions = mapItemOptions(propStore, options);\n\n value = deserialize(value, {\n ...itemOptions,\n self: src,\n type: value === src[key] ? itemOptions.type : undefined,\n collectionType: propStore.collectionType\n });\n\n if (value !== undefined) {\n out[propStore.propertyName] = value;\n }\n });\n\n if (additionalProperties) {\n keys.forEach((key) => {\n out[key] = src[key];\n });\n }\n\n return alterAfterDeserialize(out, store.schema, options);\n}\n\nfunction buildOptions(options: JsonDeserializerOptions<any, any>): any {\n if (options.store instanceof JsonEntityStore) {\n if (options.store.parameter && options.store.parameter.nestedGenerics.length) {\n options.nestedGenerics = options.store.parameter.nestedGenerics;\n }\n\n options.type = options.store.computedType;\n options.collectionType = options.store.collectionType;\n\n delete options.store;\n }\n\n return {\n groups: false,\n useAlias: true,\n ...options,\n type: options.type ? options.type : undefined,\n types: options.types ? options.types : getJsonMapperTypes()\n };\n}\n\n/**\n * Transform given source to class base on the given `options.type`.\n *\n * @param src\n * @param options\n */\nexport function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {\n options = buildOptions(options);\n\n if (!isDeserializable(src, options)) {\n return src;\n }\n\n if (!options.collectionType && isArray(src)) {\n options.collectionType = Array;\n }\n\n if (options.collectionType) {\n if (!options.types?.has(options.collectionType)) {\n throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);\n }\n\n return transformCollection(src, options);\n }\n\n if (options.types?.has(options.type)) {\n return transformType(src, options);\n }\n\n // class converter\n return plainObjectToClass(src, options);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,qCAAgH;AAChH,yCAAyI;AACzI,qCAAmC;AACnC,oCAAkC;AAClC,mCAAiC;AACjC,yCAAuC;AACvC,mCAAiC;AACjC,sCAAoC;AACpC,mEAA8D;AAC9D,iFAAsE;AACtE,0EAAqE;AACrE,4EAAuE;AA6BvE,SAAS,gBAAgB,CAAC,GAAQ,EAAE,OAAgC;IAClE,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,IAAI,IAAA,YAAK,EAAC,GAAG,CAAC,CAAC,IAAI,GAAG,KAAK,SAAS,EAAE;QACjE,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CAAC,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAChE,CAAC;AAED,SAAS,mBAAmB,CAAU,GAAQ,EAAE,OAA0C;IACxF,MAAM,EAAC,KAAK,EAAE,IAAI,GAAG,MAAM,EAAE,cAAc,EAAC,GAAG,OAAO,CAAC;IAEvD,MAAM,OAAO,GAAG,IAAI,qCAAiB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AAI,CAAC,CAAC,CAAC,SAAS;QAC7C,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,IAAA,6CAAkB,GAAE;KAC5D,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,WAAW,CAAU,GAAQ,EAAE,UAAmC,EAAE;IAClF,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAEhC,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE;QACnC,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,IAAA,cAAO,EAAC,GAAG,CAAC,EAAE;QAC3C,OAAO,CAAC,cAAc,GAAG,KAAK,CAAC;KAChC;IAED,IAAI,OAAO,CAAC,cAAc,EAAE;QAC1B,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;YAC/C,MAAM,IAAI,KAAK,CAAC,GAAG,IAAA,aAAM,EAAC,OAAO,CAAC,cAAc,CAAC,kCAAkC,CAAC,CAAC;SACtF;QAED,OAAO,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC1C;IAED,IAAI,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACpC,OAAO,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACpC;IAED,kBAAkB;IAClB,OAAO,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAzBD,kCAyBC","sourcesContent":["import {isArray, isBoolean, isClass, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonClassStore, JsonEntityStore, JsonHookContext, JsonPropertyStore, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {alterAfterDeserialize} from \"../hooks/alterAfterDeserialize\";\nimport {alterBeforeDeserialize} from \"../hooks/alterBeforeDeserialize\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: WeakMap<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n /**\n * Accept additionalProperties or ignore it\n */\n additionalProperties?: boolean;\n /**\n * Use the store which have all metadata to deserialize correctly the model. This\n * property is useful when you deal with metadata parameters.\n */\n store?: JsonEntityStore;\n /**\n *\n */\n groups?: string[] | false;\n\n [key: string]: any;\n}\n\nfunction isDeserializable(obj: any, options: JsonDeserializerOptions) {\n if ((!!options.collectionType && isNil(obj)) || obj === undefined) {\n return false;\n }\n\n return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onDeserialize\", value, [options]);\n}\n\nfunction transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object, collectionType} = options;\n\n const context = new JsonMapperContext({\n type,\n collectionType,\n options,\n next: (data, {collectionType, ...options}) => deserialize(data, options)\n });\n\n return types?.get(options.collectionType)?.deserialize<T>(src, context);\n}\n\nfunction transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object} = options;\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data, {type, ...options}) => deserialize(data, options)\n });\n\n return types?.get(type)?.deserialize<T>(src, context);\n}\n\nfunction mapItemOptions(propStore: JsonPropertyStore, options: JsonDeserializerOptions) {\n const itemOpts: JsonDeserializerOptions = {\n ...options,\n store: undefined,\n type: propStore.computedType\n };\n\n if (propStore.schema.hasGenerics) {\n itemOpts.nestedGenerics = propStore.schema.nestedGenerics;\n } else if (propStore.schema.isGeneric && options.nestedGenerics) {\n const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;\n const genericLabels = propStore.parent.schema.genericLabels || [];\n\n itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (itemOpts.type instanceof JsonSchema) {\n itemOpts.type = itemOpts.type.getTarget();\n }\n\n itemOpts.nestedGenerics = nestedGenerics;\n }\n\n return itemOpts;\n}\n\nfunction getAdditionalProperties(\n nbProps: number,\n store: JsonEntityStore,\n options: JsonDeserializerOptions<any, any>\n): boolean | JsonSchema {\n const additionalProperties = store.schema.get(\"additionalProperties\");\n\n if (isBoolean(additionalProperties) || isClass(additionalProperties)) {\n return additionalProperties;\n }\n\n return nbProps === 0 || !!options.additionalProperties;\n}\n\n/**\n * Transform given plain object to class.\n * @param src\n * @param options\n */\nexport function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {\n if (isEmpty(src)) {\n return src;\n }\n\n const {type, store = JsonEntityStore.from(type)} = options;\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = new Set<any>(objectKeys(src));\n\n const additionalProperties = getAdditionalProperties(propertiesMap.size, store, options);\n src = alterBeforeDeserialize(src, store.schema, options);\n\n const out: any = new type(src);\n\n propertiesMap.forEach((propStore) => {\n const key = options.useAlias\n ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName\n : propStore.propertyName;\n\n keys.delete(key);\n\n if (alterIgnore(propStore.schema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n const itemOptions = mapItemOptions(propStore, options);\n\n value = deserialize(value, {\n ...itemOptions,\n self: src,\n type: value === src[key] ? itemOptions.type : undefined,\n collectionType: propStore.collectionType\n });\n\n if (value !== undefined && !propStore.schema.isReadOnly) {\n out[propStore.propertyName] = value;\n }\n });\n\n if (additionalProperties) {\n if (isBoolean(additionalProperties)) {\n keys.forEach((key) => {\n out[key] = src[key];\n });\n } else {\n const type = additionalProperties.getComputedType();\n keys.forEach((key) => {\n out[key] = deserialize(src[key], {\n ...options,\n type\n });\n });\n }\n }\n\n return alterAfterDeserialize(out, store.schema, options);\n}\n\nfunction buildOptions(options: JsonDeserializerOptions<any, any>): any {\n if (options.store instanceof JsonEntityStore) {\n if (options.store.parameter && options.store.parameter.nestedGenerics.length) {\n options.nestedGenerics = options.store.parameter.nestedGenerics;\n }\n\n options.type = options.store.computedType;\n options.collectionType = options.store.collectionType;\n options.store = undefined;\n }\n\n return {\n groups: false,\n useAlias: true,\n ...options,\n type: options.type ? options.type : undefined,\n types: options.types ? options.types : getJsonMapperTypes()\n };\n}\n\n/**\n * Transform given source to class base on the given `options.type`.\n *\n * @param src\n * @param options\n */\nexport function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {\n options = buildOptions(options);\n\n if (!isDeserializable(src, options)) {\n return src;\n }\n\n if (!options.collectionType && isArray(src)) {\n options.collectionType = Array;\n }\n\n if (options.collectionType) {\n if (!options.types?.has(options.collectionType)) {\n throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);\n }\n\n return transformCollection(src, options);\n }\n\n if (options.types?.has(options.type)) {\n return transformType(src, options);\n }\n\n // class converter\n return plainObjectToClass(src, options);\n}\n"]}
|
|
@@ -86,8 +86,16 @@ function getBestType(type, obj) {
|
|
|
86
86
|
}
|
|
87
87
|
return type || Object;
|
|
88
88
|
}
|
|
89
|
+
function hasJsonMethod(obj) {
|
|
90
|
+
return typeof obj.toJSON === "function";
|
|
91
|
+
}
|
|
92
|
+
function isMongooseObject(obj) {
|
|
93
|
+
return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;
|
|
94
|
+
}
|
|
95
|
+
function isMomentObject(obj) {
|
|
96
|
+
return obj && obj?._isAMomentObject;
|
|
97
|
+
}
|
|
89
98
|
function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
90
|
-
var _a;
|
|
91
99
|
if ((0, core_1.isEmpty)(obj)) {
|
|
92
100
|
return obj;
|
|
93
101
|
}
|
|
@@ -95,10 +103,10 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
95
103
|
const types = options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
|
|
96
104
|
options.groups = groups;
|
|
97
105
|
// FIX custom serialization function from @tsed/mongoose and bson
|
|
98
|
-
if ((
|
|
106
|
+
if (isMongooseObject(obj)) {
|
|
99
107
|
return obj.toJSON(options);
|
|
100
108
|
}
|
|
101
|
-
if (
|
|
109
|
+
if (hasJsonMethod(obj) && !isMomentObject(obj) && !(0, core_1.isDate)(obj)) {
|
|
102
110
|
return serialize(obj.toJSON(), { ...options, type: (0, core_1.classOf)(obj) });
|
|
103
111
|
}
|
|
104
112
|
if (!(0, core_1.isCollection)(obj)) {
|
|
@@ -123,12 +131,12 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
123
131
|
const jsonMapper = mapper;
|
|
124
132
|
return jsonMapper.serialize(obj, context);
|
|
125
133
|
}
|
|
126
|
-
else if (obj
|
|
134
|
+
else if (obj?._isAMomentObject) {
|
|
127
135
|
return obj.toJSON();
|
|
128
136
|
}
|
|
129
137
|
if ((0, core_1.isArray)(obj)) {
|
|
130
138
|
// Serialize Array class like
|
|
131
|
-
return
|
|
139
|
+
return types.get(Array)?.serialize(obj, context);
|
|
132
140
|
}
|
|
133
141
|
return !(0, core_1.isClassObject)(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
134
142
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":";;;AAAA,qCAYoB;AACpB,yCAA4G;AAC5G,qCAAmC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,CAAC;QACpC,IAAI,EAAE,WAAW;QACjB,OAAO;QACP,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CACb,SAAS,CAAC,IAAI,EAAE;YACd,GAAG,OAAO;YACV,cAAc,EAAE,SAAS;YACzB,IAAI,EAAE,OAAO,CAAC,IAAI;SACnB,CAAC;KACL,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAA,aAAM,EAAC,WAAW,CAAC,CAAC,CAAC;IACxE,IAAI,MAAM,EAAE;QACV,MAAM,UAAU,GAAG,MAAO,CAAC;QAE3B,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;SAAM,IAAI,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,gBAAgB,EAAE;QAChC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;KACrB;IAED,IAAI,IAAA,cAAO,EAAC,GAAG,CAAC,EAAE;QAChB,6BAA6B;QAC7B,OAAO,MAAA,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,0CAAE,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAClD;IAED,OAAO,CAAC,IAAA,oBAAa,EAAC,WAAW,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACjG,CAAC;AArDD,8BAqDC","sourcesContent":["import {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n value = serialize(value, {\n useAlias,\n self: obj,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if ((typeof obj.toJSON === \"function\" && obj.$isMongooseModelPrototype) || obj._bsontype) {\n return obj.toJSON(options);\n }\n\n if (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}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QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACjG,CAAC;AAtDD,8BAsDC","sourcesContent":["import {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isDate,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n\n value = serialize(value, {\n useAlias,\n self: obj,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nfunction hasJsonMethod(obj: any) {\n return typeof obj.toJSON === \"function\";\n}\n\nfunction isMongooseObject(obj: any) {\n return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;\n}\n\nfunction isMomentObject(obj: any) {\n return obj && obj?._isAMomentObject;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if (isMongooseObject(obj)) {\n return obj.toJSON(options);\n }\n\n if (hasJsonMethod(obj) && !isMomentObject(obj) && !isDate(obj)) {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
|
|
@@ -3,6 +3,10 @@ import { isCollection } from "@tsed/core";
|
|
|
3
3
|
* @ignore
|
|
4
4
|
*/
|
|
5
5
|
export class JsonMapperContext {
|
|
6
|
+
collectionType;
|
|
7
|
+
type;
|
|
8
|
+
options;
|
|
9
|
+
_next;
|
|
6
10
|
constructor({ type, collectionType, next, options }) {
|
|
7
11
|
this.type = type;
|
|
8
12
|
this.collectionType = isCollection(collectionType) ? collectionType : undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonMapperContext.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAO,MAAM,YAAY,CAAC;AAS9C;;GAEG;AACH,MAAM,OAAO,iBAAiB;
|
|
1
|
+
{"version":3,"file":"JsonMapperContext.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAO,MAAM,YAAY,CAAC;AAS9C;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACnB,cAAc,CAAwB;IACtC,IAAI,CAAkB;IACtB,OAAO,CAAM;IACd,KAAK,CAAoC;IAEjD,YAAY,EAAC,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,EAAyB;QACvE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,cAAc,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC;QAChF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,IAAI,CAAC,IAAS;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACtB,GAAG,IAAI,CAAC,OAAO;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {isCollection, Type} from \"@tsed/core\";\nimport {JsonMapperCtx} from \"../interfaces/JsonMapperMethods\";\n/**\n * @ignore\n */\nexport interface JsonMapperContextProps extends Partial<Omit<JsonMapperCtx, \"next\">> {\n next: (data: any, options: any) => void;\n options: any;\n}\n/**\n * @ignore\n */\nexport class JsonMapperContext implements JsonMapperCtx {\n readonly collectionType: Type<any> | undefined;\n readonly type: Type<any> | any;\n readonly options: any;\n private _next: (data: any, options: any) => void;\n\n constructor({type, collectionType, next, options}: JsonMapperContextProps) {\n this.type = type;\n this.collectionType = isCollection(collectionType) ? collectionType : undefined;\n this._next = next;\n this.options = options;\n }\n\n next(data: any) {\n return this._next(data, {\n ...this.options,\n type: this.type\n });\n }\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export function alterAfterDeserialize(data, schema, options) {
|
|
2
|
-
|
|
3
|
-
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("afterDeserialize", data, [options]);
|
|
2
|
+
return schema?.$hooks?.alter("afterDeserialize", data, [options]);
|
|
4
3
|
}
|
|
5
4
|
//# sourceMappingURL=alterAfterDeserialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alterAfterDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterAfterDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,qBAAqB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY
|
|
1
|
+
{"version":3,"file":"alterAfterDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterAfterDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,qBAAqB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;IACpF,OAAO,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,kBAAkB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterAfterDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"afterDeserialize\", data, [options]);\n}\n"]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export function alterBeforeDeserialize(data, schema, options) {
|
|
2
|
-
|
|
3
|
-
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("beforeDeserialize", data, [options]);
|
|
2
|
+
return schema?.$hooks?.alter("beforeDeserialize", data, [options]);
|
|
4
3
|
}
|
|
5
4
|
//# sourceMappingURL=alterBeforeDeserialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alterBeforeDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterBeforeDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,sBAAsB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY
|
|
1
|
+
{"version":3,"file":"alterBeforeDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterBeforeDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,sBAAsB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;IACrF,OAAO,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,mBAAmB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACrE,CAAC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterBeforeDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"beforeDeserialize\", data, [options]);\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArray, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
1
|
+
import { isArray, isBoolean, isClass, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
2
|
import { alterIgnore, getProperties, JsonEntityStore, JsonSchema } from "@tsed/schema";
|
|
3
3
|
import "../components/ArrayMapper.js";
|
|
4
4
|
import "../components/DateMapper.js";
|
|
@@ -20,7 +20,6 @@ function alterValue(schema, value, options) {
|
|
|
20
20
|
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
21
21
|
}
|
|
22
22
|
function transformCollection(src, options) {
|
|
23
|
-
var _a;
|
|
24
23
|
const { types, type = Object, collectionType } = options;
|
|
25
24
|
const context = new JsonMapperContext({
|
|
26
25
|
type,
|
|
@@ -28,17 +27,16 @@ function transformCollection(src, options) {
|
|
|
28
27
|
options,
|
|
29
28
|
next: (data, { collectionType, ...options }) => deserialize(data, options)
|
|
30
29
|
});
|
|
31
|
-
return
|
|
30
|
+
return types?.get(options.collectionType)?.deserialize(src, context);
|
|
32
31
|
}
|
|
33
32
|
function transformType(src, options) {
|
|
34
|
-
var _a;
|
|
35
33
|
const { types, type = Object } = options;
|
|
36
34
|
const context = new JsonMapperContext({
|
|
37
35
|
type,
|
|
38
36
|
options,
|
|
39
37
|
next: (data, { type, ...options }) => deserialize(data, options)
|
|
40
38
|
});
|
|
41
|
-
return
|
|
39
|
+
return types?.get(type)?.deserialize(src, context);
|
|
42
40
|
}
|
|
43
41
|
function mapItemOptions(propStore, options) {
|
|
44
42
|
const itemOpts = {
|
|
@@ -60,6 +58,13 @@ function mapItemOptions(propStore, options) {
|
|
|
60
58
|
}
|
|
61
59
|
return itemOpts;
|
|
62
60
|
}
|
|
61
|
+
function getAdditionalProperties(nbProps, store, options) {
|
|
62
|
+
const additionalProperties = store.schema.get("additionalProperties");
|
|
63
|
+
if (isBoolean(additionalProperties) || isClass(additionalProperties)) {
|
|
64
|
+
return additionalProperties;
|
|
65
|
+
}
|
|
66
|
+
return nbProps === 0 || !!options.additionalProperties;
|
|
67
|
+
}
|
|
63
68
|
/**
|
|
64
69
|
* Transform given plain object to class.
|
|
65
70
|
* @param src
|
|
@@ -72,7 +77,7 @@ export function plainObjectToClass(src, options) {
|
|
|
72
77
|
const { type, store = JsonEntityStore.from(type) } = options;
|
|
73
78
|
const propertiesMap = getProperties(store, { ...options, withIgnoredProps: true });
|
|
74
79
|
let keys = new Set(objectKeys(src));
|
|
75
|
-
const additionalProperties = propertiesMap.size
|
|
80
|
+
const additionalProperties = getAdditionalProperties(propertiesMap.size, store, options);
|
|
76
81
|
src = alterBeforeDeserialize(src, store.schema, options);
|
|
77
82
|
const out = new type(src);
|
|
78
83
|
propertiesMap.forEach((propStore) => {
|
|
@@ -91,14 +96,25 @@ export function plainObjectToClass(src, options) {
|
|
|
91
96
|
type: value === src[key] ? itemOptions.type : undefined,
|
|
92
97
|
collectionType: propStore.collectionType
|
|
93
98
|
});
|
|
94
|
-
if (value !== undefined) {
|
|
99
|
+
if (value !== undefined && !propStore.schema.isReadOnly) {
|
|
95
100
|
out[propStore.propertyName] = value;
|
|
96
101
|
}
|
|
97
102
|
});
|
|
98
103
|
if (additionalProperties) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
104
|
+
if (isBoolean(additionalProperties)) {
|
|
105
|
+
keys.forEach((key) => {
|
|
106
|
+
out[key] = src[key];
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const type = additionalProperties.getComputedType();
|
|
111
|
+
keys.forEach((key) => {
|
|
112
|
+
out[key] = deserialize(src[key], {
|
|
113
|
+
...options,
|
|
114
|
+
type
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
102
118
|
}
|
|
103
119
|
return alterAfterDeserialize(out, store.schema, options);
|
|
104
120
|
}
|
|
@@ -109,7 +125,7 @@ function buildOptions(options) {
|
|
|
109
125
|
}
|
|
110
126
|
options.type = options.store.computedType;
|
|
111
127
|
options.collectionType = options.store.collectionType;
|
|
112
|
-
|
|
128
|
+
options.store = undefined;
|
|
113
129
|
}
|
|
114
130
|
return {
|
|
115
131
|
groups: false,
|
|
@@ -126,7 +142,6 @@ function buildOptions(options) {
|
|
|
126
142
|
* @param options
|
|
127
143
|
*/
|
|
128
144
|
export function deserialize(src, options = {}) {
|
|
129
|
-
var _a, _b;
|
|
130
145
|
options = buildOptions(options);
|
|
131
146
|
if (!isDeserializable(src, options)) {
|
|
132
147
|
return src;
|
|
@@ -135,12 +150,12 @@ export function deserialize(src, options = {}) {
|
|
|
135
150
|
options.collectionType = Array;
|
|
136
151
|
}
|
|
137
152
|
if (options.collectionType) {
|
|
138
|
-
if (!
|
|
153
|
+
if (!options.types?.has(options.collectionType)) {
|
|
139
154
|
throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);
|
|
140
155
|
}
|
|
141
156
|
return transformCollection(src, options);
|
|
142
157
|
}
|
|
143
|
-
if (
|
|
158
|
+
if (options.types?.has(options.type)) {
|
|
144
159
|
return transformType(src, options);
|
|
145
160
|
}
|
|
146
161
|
// class converter
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAiB,MAAM,EAAE,UAAU,EAAO,MAAM,YAAY,CAAC;AAC5F,OAAO,EAAC,WAAW,EAAE,aAAa,EAAE,eAAe,EAAsC,UAAU,EAAC,MAAM,c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;KAC1C;IAED,IAAI,MAAA,OAAO,CAAC,KAAK,0CAAE,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACpC,OAAO,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACpC;IAED,kBAAkB;IAClB,OAAO,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import {isArray, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonEntityStore, JsonHookContext, JsonPropertyStore, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {alterAfterDeserialize} from \"../hooks/alterAfterDeserialize\";\nimport {alterBeforeDeserialize} from \"../hooks/alterBeforeDeserialize\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: WeakMap<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n /**\n * Accept additionalProperties or ignore it\n */\n additionalProperties?: boolean;\n /**\n * Use the store which have all metadata to deserialize correctly the model. This\n * property is useful when you deal with metadata parameters.\n */\n store?: JsonEntityStore;\n /**\n *\n */\n groups?: string[] | false;\n\n [key: string]: any;\n}\n\nfunction isDeserializable(obj: any, options: JsonDeserializerOptions) {\n if ((!!options.collectionType && isNil(obj)) || obj === undefined) {\n return false;\n }\n\n return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onDeserialize\", value, [options]);\n}\n\nfunction transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object, collectionType} = options;\n\n const context = new JsonMapperContext({\n type,\n collectionType,\n options,\n next: (data, {collectionType, ...options}) => deserialize(data, options)\n });\n\n return types?.get(options.collectionType)?.deserialize<T>(src, context);\n}\n\nfunction transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object} = options;\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data, {type, ...options}) => deserialize(data, options)\n });\n\n return types?.get(type)?.deserialize<T>(src, context);\n}\n\nfunction mapItemOptions(propStore: JsonPropertyStore, options: JsonDeserializerOptions) {\n const itemOpts: JsonDeserializerOptions = {\n ...options,\n store: undefined,\n type: propStore.computedType\n };\n\n if (propStore.schema.hasGenerics) {\n itemOpts.nestedGenerics = propStore.schema.nestedGenerics;\n } else if (propStore.schema.isGeneric && options.nestedGenerics) {\n const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;\n const genericLabels = propStore.parent.schema.genericLabels || [];\n\n itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (itemOpts.type instanceof JsonSchema) {\n itemOpts.type = itemOpts.type.getTarget();\n }\n\n itemOpts.nestedGenerics = nestedGenerics;\n }\n\n return itemOpts;\n}\n\n/**\n * Transform given plain object to class.\n * @param src\n * @param options\n */\nexport function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {\n if (isEmpty(src)) {\n return src;\n }\n\n const {type, store = JsonEntityStore.from(type)} = options;\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = new Set<any>(objectKeys(src));\n const additionalProperties = propertiesMap.size ? !!store.schema.get(\"additionalProperties\") || options.additionalProperties : true;\n\n src = alterBeforeDeserialize(src, store.schema, options);\n\n const out: any = new type(src);\n\n propertiesMap.forEach((propStore) => {\n const key = options.useAlias\n ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName\n : propStore.propertyName;\n\n keys.delete(key);\n\n if (alterIgnore(propStore.schema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n const itemOptions = mapItemOptions(propStore, options);\n\n value = deserialize(value, {\n ...itemOptions,\n self: src,\n type: value === src[key] ? itemOptions.type : undefined,\n collectionType: propStore.collectionType\n });\n\n if (value !== undefined) {\n out[propStore.propertyName] = value;\n }\n });\n\n if (additionalProperties) {\n keys.forEach((key) => {\n out[key] = src[key];\n });\n }\n\n return alterAfterDeserialize(out, store.schema, options);\n}\n\nfunction buildOptions(options: JsonDeserializerOptions<any, any>): any {\n if (options.store instanceof JsonEntityStore) {\n if (options.store.parameter && options.store.parameter.nestedGenerics.length) {\n options.nestedGenerics = options.store.parameter.nestedGenerics;\n }\n\n options.type = options.store.computedType;\n options.collectionType = options.store.collectionType;\n\n delete options.store;\n }\n\n return {\n groups: false,\n useAlias: true,\n ...options,\n type: options.type ? options.type : undefined,\n types: options.types ? options.types : getJsonMapperTypes()\n };\n}\n\n/**\n * Transform given source to class base on the given `options.type`.\n *\n * @param src\n * @param options\n */\nexport function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {\n options = buildOptions(options);\n\n if (!isDeserializable(src, options)) {\n return src;\n }\n\n if (!options.collectionType && isArray(src)) {\n options.collectionType = Array;\n }\n\n if (options.collectionType) {\n if (!options.types?.has(options.collectionType)) {\n throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);\n }\n\n return transformCollection(src, options);\n }\n\n if (options.types?.has(options.type)) {\n return transformType(src, options);\n }\n\n // class converter\n return plainObjectToClass(src, options);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAiB,MAAM,EAAE,UAAU,EAAO,MAAM,YAAY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;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAU,GAAQ,EAAE,UAAmC,EAAE;IAClF,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAEhC,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE;QACnC,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAC3C,OAAO,CAAC,cAAc,GAAG,KAAK,CAAC;KAChC;IAED,IAAI,OAAO,CAAC,cAAc,EAAE;QAC1B,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE;YAC/C,MAAM,IAAI,KAAK,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,kCAAkC,CAAC,CAAC;SACtF;QAED,OAAO,mBAAmB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC1C;IAED,IAAI,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;QACpC,OAAO,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KACpC;IAED,kBAAkB;IAClB,OAAO,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["import {isArray, isBoolean, isClass, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonClassStore, JsonEntityStore, JsonHookContext, JsonPropertyStore, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {alterAfterDeserialize} from \"../hooks/alterAfterDeserialize\";\nimport {alterBeforeDeserialize} from \"../hooks/alterBeforeDeserialize\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: WeakMap<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n /**\n * Accept additionalProperties or ignore it\n */\n additionalProperties?: boolean;\n /**\n * Use the store which have all metadata to deserialize correctly the model. This\n * property is useful when you deal with metadata parameters.\n */\n store?: JsonEntityStore;\n /**\n *\n */\n groups?: string[] | false;\n\n [key: string]: any;\n}\n\nfunction isDeserializable(obj: any, options: JsonDeserializerOptions) {\n if ((!!options.collectionType && isNil(obj)) || obj === undefined) {\n return false;\n }\n\n return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onDeserialize\", value, [options]);\n}\n\nfunction transformCollection<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object, collectionType} = options;\n\n const context = new JsonMapperContext({\n type,\n collectionType,\n options,\n next: (data, {collectionType, ...options}) => deserialize(data, options)\n });\n\n return types?.get(options.collectionType)?.deserialize<T>(src, context);\n}\n\nfunction transformType<T = any>(src: any, options: JsonDeserializerOptions<any, any>): T {\n const {types, type = Object} = options;\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data, {type, ...options}) => deserialize(data, options)\n });\n\n return types?.get(type)?.deserialize<T>(src, context);\n}\n\nfunction mapItemOptions(propStore: JsonPropertyStore, options: JsonDeserializerOptions) {\n const itemOpts: JsonDeserializerOptions = {\n ...options,\n store: undefined,\n type: propStore.computedType\n };\n\n if (propStore.schema.hasGenerics) {\n itemOpts.nestedGenerics = propStore.schema.nestedGenerics;\n } else if (propStore.schema.isGeneric && options.nestedGenerics) {\n const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;\n const genericLabels = propStore.parent.schema.genericLabels || [];\n\n itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (itemOpts.type instanceof JsonSchema) {\n itemOpts.type = itemOpts.type.getTarget();\n }\n\n itemOpts.nestedGenerics = nestedGenerics;\n }\n\n return itemOpts;\n}\n\nfunction getAdditionalProperties(\n nbProps: number,\n store: JsonEntityStore,\n options: JsonDeserializerOptions<any, any>\n): boolean | JsonSchema {\n const additionalProperties = store.schema.get(\"additionalProperties\");\n\n if (isBoolean(additionalProperties) || isClass(additionalProperties)) {\n return additionalProperties;\n }\n\n return nbProps === 0 || !!options.additionalProperties;\n}\n\n/**\n * Transform given plain object to class.\n * @param src\n * @param options\n */\nexport function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T {\n if (isEmpty(src)) {\n return src;\n }\n\n const {type, store = JsonEntityStore.from(type)} = options;\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = new Set<any>(objectKeys(src));\n\n const additionalProperties = getAdditionalProperties(propertiesMap.size, store, options);\n src = alterBeforeDeserialize(src, store.schema, options);\n\n const out: any = new type(src);\n\n propertiesMap.forEach((propStore) => {\n const key = options.useAlias\n ? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName\n : propStore.propertyName;\n\n keys.delete(key);\n\n if (alterIgnore(propStore.schema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n const itemOptions = mapItemOptions(propStore, options);\n\n value = deserialize(value, {\n ...itemOptions,\n self: src,\n type: value === src[key] ? itemOptions.type : undefined,\n collectionType: propStore.collectionType\n });\n\n if (value !== undefined && !propStore.schema.isReadOnly) {\n out[propStore.propertyName] = value;\n }\n });\n\n if (additionalProperties) {\n if (isBoolean(additionalProperties)) {\n keys.forEach((key) => {\n out[key] = src[key];\n });\n } else {\n const type = additionalProperties.getComputedType();\n keys.forEach((key) => {\n out[key] = deserialize(src[key], {\n ...options,\n type\n });\n });\n }\n }\n\n return alterAfterDeserialize(out, store.schema, options);\n}\n\nfunction buildOptions(options: JsonDeserializerOptions<any, any>): any {\n if (options.store instanceof JsonEntityStore) {\n if (options.store.parameter && options.store.parameter.nestedGenerics.length) {\n options.nestedGenerics = options.store.parameter.nestedGenerics;\n }\n\n options.type = options.store.computedType;\n options.collectionType = options.store.collectionType;\n options.store = undefined;\n }\n\n return {\n groups: false,\n useAlias: true,\n ...options,\n type: options.type ? options.type : undefined,\n types: options.types ? options.types : getJsonMapperTypes()\n };\n}\n\n/**\n * Transform given source to class base on the given `options.type`.\n *\n * @param src\n * @param options\n */\nexport function deserialize<T = any>(src: any, options: JsonDeserializerOptions = {}): T {\n options = buildOptions(options);\n\n if (!isDeserializable(src, options)) {\n return src;\n }\n\n if (!options.collectionType && isArray(src)) {\n options.collectionType = Array;\n }\n\n if (options.collectionType) {\n if (!options.types?.has(options.collectionType)) {\n throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);\n }\n\n return transformCollection(src, options);\n }\n\n if (options.types?.has(options.type)) {\n return transformType(src, options);\n }\n\n // class converter\n return plainObjectToClass(src, options);\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
1
|
+
import { classOf, isArray, isClassObject, isCollection, isDate, isEmpty, isFunction, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
2
|
import { alterIgnore, getPropertiesStores, JsonEntityStore } from "@tsed/schema";
|
|
3
3
|
import "../components/ArrayMapper.js";
|
|
4
4
|
import "../components/DateMapper.js";
|
|
@@ -82,8 +82,16 @@ function getBestType(type, obj) {
|
|
|
82
82
|
}
|
|
83
83
|
return type || Object;
|
|
84
84
|
}
|
|
85
|
+
function hasJsonMethod(obj) {
|
|
86
|
+
return typeof obj.toJSON === "function";
|
|
87
|
+
}
|
|
88
|
+
function isMongooseObject(obj) {
|
|
89
|
+
return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;
|
|
90
|
+
}
|
|
91
|
+
function isMomentObject(obj) {
|
|
92
|
+
return obj && obj?._isAMomentObject;
|
|
93
|
+
}
|
|
85
94
|
export function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
86
|
-
var _a;
|
|
87
95
|
if (isEmpty(obj)) {
|
|
88
96
|
return obj;
|
|
89
97
|
}
|
|
@@ -91,10 +99,10 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
91
99
|
const types = options.types ? options.types : getJsonMapperTypes();
|
|
92
100
|
options.groups = groups;
|
|
93
101
|
// FIX custom serialization function from @tsed/mongoose and bson
|
|
94
|
-
if ((
|
|
102
|
+
if (isMongooseObject(obj)) {
|
|
95
103
|
return obj.toJSON(options);
|
|
96
104
|
}
|
|
97
|
-
if (
|
|
105
|
+
if (hasJsonMethod(obj) && !isMomentObject(obj) && !isDate(obj)) {
|
|
98
106
|
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
99
107
|
}
|
|
100
108
|
if (!isCollection(obj)) {
|
|
@@ -119,12 +127,12 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
119
127
|
const jsonMapper = mapper;
|
|
120
128
|
return jsonMapper.serialize(obj, context);
|
|
121
129
|
}
|
|
122
|
-
else if (obj
|
|
130
|
+
else if (obj?._isAMomentObject) {
|
|
123
131
|
return obj.toJSON();
|
|
124
132
|
}
|
|
125
133
|
if (isArray(obj)) {
|
|
126
134
|
// Serialize Array class like
|
|
127
|
-
return
|
|
135
|
+
return types.get(Array)?.serialize(obj, context);
|
|
128
136
|
}
|
|
129
137
|
return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
130
138
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"m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\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n value = serialize(value, {\n useAlias,\n self: obj,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if ((typeof obj.toJSON === \"function\" && obj.$isMongooseModelPrototype) || obj._bsontype) {\n return obj.toJSON(options);\n }\n\n if (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,OAAO,EACP,aAAa,EACb,YAAY,EACZ,MAAM,EACN,OAAO,EACP,UAAU,EACV,KAAK,EAEL,MAAM,EACN,UAAU,EAEX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,WAAW,EAAE,mBAAmB,EAAE,eAAe,EAA8B,MAAM,cAAc,CAAC;AAC5G,OAAO,2BAA2B,CAAC;AACnC,OAAO,0BAA0B,CAAC;AAClC,OAAO,yBAAyB,CAAC;AACjC,OAAO,+BAA+B,CAAC;AACvC,OAAO,yBAAyB,CAAC;AACjC,OAAO,4BAA4B,CAAC;AACpC,OAAO,EAAC,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAC,kBAAkB,EAAC,MAAM,oCAAoC,CAAC;AAgBtE,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,mBAAmB,CAAC,UAA2B,EAAE,GAAQ;IAChE,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;IAErE,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QAClB,8CAA8C;QAC9C,UAAU,CAAC,GAAG,CAAC,CA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UAAU,GAAG,MAAO,CAAC;QAE3B,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;SAAM,IAAI,GAAG,EAAE,gBAAgB,EAAE;QAChC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;KACrB;IAED,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAChB,6BAA6B;QAC7B,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAClD;IAED,OAAO,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACjG,CAAC","sourcesContent":["import {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isDate,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components/ArrayMapper\";\nimport \"../components/DateMapper\";\nimport \"../components/MapMapper\";\nimport \"../components/PrimitiveMapper\";\nimport \"../components/SetMapper\";\nimport \"../components/SymbolMapper\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n\n value = serialize(value, {\n useAlias,\n self: obj,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nfunction hasJsonMethod(obj: any) {\n return typeof obj.toJSON === \"function\";\n}\n\nfunction isMongooseObject(obj: any) {\n return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;\n}\n\nfunction isMomentObject(obj: any) {\n return obj && obj?._isAMomentObject;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if (isMongooseObject(obj)) {\n return obj.toJSON(options);\n }\n\n if (hasJsonMethod(obj) && !isMomentObject(obj) && !isDate(obj)) {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/json-mapper",
|
|
3
|
-
"version": "7.0.0-
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
4
4
|
"description": "Json mapper module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
18
18
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
19
19
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
20
|
-
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
|
|
20
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
21
|
+
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
|
|
21
22
|
},
|
|
22
23
|
"keywords": [
|
|
23
24
|
"TypeScript",
|
|
@@ -32,17 +33,17 @@
|
|
|
32
33
|
"tsed"
|
|
33
34
|
],
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"tslib": "2.
|
|
36
|
+
"tslib": "2.4.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@tsed/core": "7.0.0-
|
|
39
|
-
"@tsed/exceptions": "7.0.0-
|
|
40
|
-
"@tsed/schema": "7.0.0-
|
|
39
|
+
"@tsed/core": "7.0.0-rc.2",
|
|
40
|
+
"@tsed/exceptions": "7.0.0-rc.2",
|
|
41
|
+
"@tsed/schema": "7.0.0-rc.2"
|
|
41
42
|
},
|
|
42
43
|
"peerDependencies": {
|
|
43
|
-
"@tsed/core": "^7.0.0-
|
|
44
|
-
"@tsed/exceptions": "^7.0.0-
|
|
45
|
-
"@tsed/schema": "^7.0.0-
|
|
44
|
+
"@tsed/core": "^7.0.0-rc.2",
|
|
45
|
+
"@tsed/exceptions": "^7.0.0-rc.2",
|
|
46
|
+
"@tsed/schema": "^7.0.0-rc.2"
|
|
46
47
|
},
|
|
47
48
|
"peerDependenciesMeta": {
|
|
48
49
|
"@tsed/core": {
|
|
@@ -59,7 +60,10 @@
|
|
|
59
60
|
"bugs": {
|
|
60
61
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
61
62
|
},
|
|
62
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
63
|
+
"homepage": "https://github.com/tsedio/tsed/tree/rc/packages/specs/json-mapper",
|
|
63
64
|
"author": "Romain Lenzotti",
|
|
64
|
-
"license": "MIT"
|
|
65
|
+
"license": "MIT",
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"tag": "rc"
|
|
68
|
+
}
|
|
65
69
|
}
|
package/readme.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
12
|
[](https://github.com/semantic-release/semantic-release)
|
|
13
13
|
[](https://github.com/prettier/prettier)
|
|
14
|
-
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
15
16
|
|
|
16
17
|
</div>
|
|
17
18
|
|
|
@@ -68,7 +69,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
|
|
|
68
69
|
|
|
69
70
|
The MIT License (MIT)
|
|
70
71
|
|
|
71
|
-
Copyright (c) 2016 -
|
|
72
|
+
Copyright (c) 2016 - 2022 Romain Lenzotti
|
|
72
73
|
|
|
73
74
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
74
75
|
|