@tsed/json-mapper 7.0.0-beta.1 → 7.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/ArrayMapper.js +1 -1
- package/lib/cjs/components/DateMapper.js +1 -1
- package/lib/cjs/components/MapMapper.js +1 -1
- package/lib/cjs/components/PrimitiveMapper.js +1 -1
- package/lib/cjs/components/SetMapper.js +1 -1
- package/lib/cjs/components/SymbolMapper.js +1 -1
- package/lib/cjs/domain/JsonMapperTypesContainer.js.map +1 -1
- package/lib/cjs/index.js +21 -11
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/utils/deserialize.js +34 -20
- package/lib/cjs/utils/deserialize.js.map +1 -1
- package/lib/cjs/utils/serialize.js +27 -5
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/components/ArrayMapper.js +1 -1
- package/lib/esm/components/DateMapper.js +1 -1
- package/lib/esm/components/MapMapper.js +1 -1
- package/lib/esm/components/PrimitiveMapper.js +1 -1
- package/lib/esm/components/SetMapper.js +1 -1
- package/lib/esm/components/SymbolMapper.js +1 -1
- package/lib/esm/decorators/jsonMapper.js +1 -1
- package/lib/esm/domain/JsonMapperTypesContainer.js.map +1 -1
- package/lib/esm/index.js +21 -11
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/package.json +3 -0
- package/lib/esm/utils/deserialize.js +38 -24
- package/lib/esm/utils/deserialize.js.map +1 -1
- package/lib/esm/utils/serialize.js +30 -8
- package/lib/esm/utils/serialize.js.map +1 -1
- package/lib/types/domain/JsonMapperTypesContainer.d.ts +2 -2
- package/lib/types/index.d.ts +13 -3
- package/lib/types/utils/deserialize.d.ts +6 -1
- package/lib/types/utils/serialize.d.ts +7 -2
- package/package.json +24 -11
- package/readme.md +1 -0
- package/tsconfig.compile.esm.json +1 -12
- package/lib/cjs/components/index.js +0 -10
- package/lib/cjs/components/index.js.map +0 -1
- package/lib/esm/components/index.js +0 -7
- package/lib/esm/components/index.js.map +0 -1
- package/lib/types/components/index.d.ts +0 -6
|
@@ -16,7 +16,7 @@ let ArrayMapper = class ArrayMapper {
|
|
|
16
16
|
return [].concat(data).map((item) => options.next(item));
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
|
-
ArrayMapper =
|
|
19
|
+
ArrayMapper = tslib_1.__decorate([
|
|
20
20
|
(0, jsonMapper_1.JsonMapper)(Array)
|
|
21
21
|
], ArrayMapper);
|
|
22
22
|
exports.ArrayMapper = ArrayMapper;
|
|
@@ -23,7 +23,7 @@ let DateMapper = class DateMapper {
|
|
|
23
23
|
return new Date(object).toISOString();
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
DateMapper =
|
|
26
|
+
DateMapper = tslib_1.__decorate([
|
|
27
27
|
(0, jsonMapper_1.JsonMapper)(Date)
|
|
28
28
|
], DateMapper);
|
|
29
29
|
exports.DateMapper = DateMapper;
|
|
@@ -38,7 +38,7 @@ let PrimitiveMapper = class PrimitiveMapper {
|
|
|
38
38
|
return object;
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
-
PrimitiveMapper =
|
|
41
|
+
PrimitiveMapper = tslib_1.__decorate([
|
|
42
42
|
(0, jsonMapper_1.JsonMapper)(String, Number, Boolean)
|
|
43
43
|
], PrimitiveMapper);
|
|
44
44
|
exports.PrimitiveMapper = PrimitiveMapper;
|
|
@@ -17,7 +17,7 @@ let SymbolMapper = class SymbolMapper {
|
|
|
17
17
|
return object.toString().replace("Symbol(", "").replace(")", "");
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
SymbolMapper =
|
|
20
|
+
SymbolMapper = tslib_1.__decorate([
|
|
21
21
|
(0, jsonMapper_1.JsonMapper)(Symbol)
|
|
22
22
|
], SymbolMapper);
|
|
23
23
|
exports.SymbolMapper = SymbolMapper;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonMapperTypesContainer.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperTypesContainer.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,yCAAyC;AACzC,MAAM,wBAAwB,
|
|
1
|
+
{"version":3,"file":"JsonMapperTypesContainer.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperTypesContainer.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH,yCAAyC;AACzC,MAAM,wBAAwB,GAAwD,IAAI,GAAG,EAAE,CAAC;AAChG;;GAEG;AACH,SAAgB,sBAAsB,CAAC,IAAiC,EAAE,KAA8B;IACtG,wBAAwB,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC;AAClD,CAAC;AAFD,wDAEC;AACD;;GAEG;AACH,SAAgB,kBAAkB;IAChC,OAAO,wBAAwB,CAAC;AAClC,CAAC;AAFD,gDAEC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nconst JsonMapperTypesContainer: Map<Type<any> | Symbol | string, JsonMapperMethods> = new Map();\n/**\n * @ignore\n */\nexport function registerJsonTypeMapper(type: Type<any> | Symbol | string, token: Type<JsonMapperMethods>) {\n JsonMapperTypesContainer.set(type, new token());\n}\n/**\n * @ignore\n */\nexport function getJsonMapperTypes(): Map<Type<any> | Symbol | string, JsonMapperMethods> {\n return JsonMapperTypesContainer;\n}\n"]}
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Automatically generated by barrelsby.
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const tslib_1 = require("tslib");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
tslib_1.__exportStar(require("./components/ArrayMapper"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./components/DateMapper"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./components/MapMapper"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./components/PrimitiveMapper"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./components/SetMapper"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./components/SymbolMapper"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./decorators/afterDeserialize"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./decorators/beforeDeserialize"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./decorators/jsonMapper"), exports);
|
|
16
|
+
tslib_1.__exportStar(require("./decorators/onDeserialize"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./decorators/onSerialize"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./domain/JsonMapperContext"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./domain/JsonMapperTypesContainer"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./hooks/alterAfterDeserialize"), exports);
|
|
21
|
+
tslib_1.__exportStar(require("./hooks/alterBeforeDeserialize"), exports);
|
|
22
|
+
tslib_1.__exportStar(require("./interfaces/JsonMapperMethods"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./utils/deserialize"), exports);
|
|
24
|
+
tslib_1.__exportStar(require("./utils/serialize"), exports);
|
|
15
25
|
//# 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":";;;
|
|
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,4EAAkD;AAClD,wEAA8C;AAC9C,yEAA+C;AAC/C,yEAA+C;AAC/C,8DAAoC;AACpC,4DAAkC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./components/ArrayMapper\";\nexport * from \"./components/DateMapper\";\nexport * from \"./components/MapMapper\";\nexport * from \"./components/PrimitiveMapper\";\nexport * from \"./components/SetMapper\";\nexport * from \"./components/SymbolMapper\";\nexport * from \"./decorators/afterDeserialize\";\nexport * from \"./decorators/beforeDeserialize\";\nexport * from \"./decorators/jsonMapper\";\nexport * from \"./decorators/onDeserialize\";\nexport * from \"./decorators/onSerialize\";\nexport * from \"./domain/JsonMapperContext\";\nexport * from \"./domain/JsonMapperTypesContainer\";\nexport * from \"./hooks/alterAfterDeserialize\";\nexport * from \"./hooks/alterBeforeDeserialize\";\nexport * from \"./interfaces/JsonMapperMethods\";\nexport * from \"./utils/deserialize\";\nexport * from \"./utils/serialize\";\n"]}
|
|
@@ -3,7 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.deserialize = exports.plainObjectToClass = void 0;
|
|
4
4
|
const core_1 = require("@tsed/core");
|
|
5
5
|
const schema_1 = require("@tsed/schema");
|
|
6
|
-
require("../components");
|
|
6
|
+
require("../components/ArrayMapper");
|
|
7
|
+
require("../components/DateMapper");
|
|
8
|
+
require("../components/MapMapper");
|
|
9
|
+
require("../components/PrimitiveMapper");
|
|
10
|
+
require("../components/SetMapper");
|
|
11
|
+
require("../components/SymbolMapper");
|
|
7
12
|
const JsonMapperContext_1 = require("../domain/JsonMapperContext");
|
|
8
13
|
const JsonMapperTypesContainer_1 = require("../domain/JsonMapperTypesContainer");
|
|
9
14
|
const alterAfterDeserialize_1 = require("../hooks/alterAfterDeserialize");
|
|
@@ -38,6 +43,26 @@ function transformType(src, options) {
|
|
|
38
43
|
});
|
|
39
44
|
return (_a = types === null || types === void 0 ? void 0 : types.get(type)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
40
45
|
}
|
|
46
|
+
function mapItemOptions(propStore, options) {
|
|
47
|
+
const itemOpts = {
|
|
48
|
+
...options,
|
|
49
|
+
store: undefined,
|
|
50
|
+
type: propStore.computedType
|
|
51
|
+
};
|
|
52
|
+
if (propStore.schema.hasGenerics) {
|
|
53
|
+
itemOpts.nestedGenerics = propStore.schema.nestedGenerics;
|
|
54
|
+
}
|
|
55
|
+
else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
56
|
+
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
57
|
+
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
58
|
+
itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
59
|
+
if (itemOpts.type instanceof schema_1.JsonSchema) {
|
|
60
|
+
itemOpts.type = itemOpts.type.getTarget();
|
|
61
|
+
}
|
|
62
|
+
itemOpts.nestedGenerics = nestedGenerics;
|
|
63
|
+
}
|
|
64
|
+
return itemOpts;
|
|
65
|
+
}
|
|
41
66
|
/**
|
|
42
67
|
* Transform given plain object to class.
|
|
43
68
|
* @param src
|
|
@@ -47,9 +72,9 @@ function plainObjectToClass(src, options) {
|
|
|
47
72
|
if ((0, core_1.isEmpty)(src)) {
|
|
48
73
|
return src;
|
|
49
74
|
}
|
|
50
|
-
const { type, store = schema_1.JsonEntityStore.from(type)
|
|
75
|
+
const { type, store = schema_1.JsonEntityStore.from(type) } = options;
|
|
51
76
|
const propertiesMap = (0, schema_1.getProperties)(store, { ...options, withIgnoredProps: true });
|
|
52
|
-
let keys = (0, core_1.objectKeys)(src);
|
|
77
|
+
let keys = new Set((0, core_1.objectKeys)(src));
|
|
53
78
|
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
|
|
54
79
|
src = (0, alterBeforeDeserialize_1.alterBeforeDeserialize)(src, store.schema, options);
|
|
55
80
|
const out = new type(src);
|
|
@@ -57,27 +82,16 @@ function plainObjectToClass(src, options) {
|
|
|
57
82
|
const key = options.useAlias
|
|
58
83
|
? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName
|
|
59
84
|
: propStore.propertyName;
|
|
60
|
-
keys
|
|
61
|
-
|
|
62
|
-
if ((0, schema_1.alterIgnore)(propStore.itemSchema, options)) {
|
|
85
|
+
keys.delete(key);
|
|
86
|
+
if ((0, schema_1.alterIgnore)(propStore.schema, options)) {
|
|
63
87
|
return;
|
|
64
88
|
}
|
|
65
89
|
let value = alterValue(propStore.schema, src[key], { ...options, self: src });
|
|
66
|
-
|
|
67
|
-
next.nestedGenerics = propStore.schema.nestedGenerics;
|
|
68
|
-
}
|
|
69
|
-
else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
70
|
-
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
71
|
-
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
72
|
-
next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
73
|
-
if (next.type instanceof schema_1.JsonSchema) {
|
|
74
|
-
next.type = next.type.getTarget();
|
|
75
|
-
}
|
|
76
|
-
next.nestedGenerics = nestedGenerics;
|
|
77
|
-
}
|
|
90
|
+
const itemOptions = mapItemOptions(propStore, options);
|
|
78
91
|
value = deserialize(value, {
|
|
79
|
-
...
|
|
80
|
-
|
|
92
|
+
...itemOptions,
|
|
93
|
+
self: src,
|
|
94
|
+
type: value === src[key] ? itemOptions.type : undefined,
|
|
81
95
|
collectionType: propStore.collectionType
|
|
82
96
|
});
|
|
83
97
|
if (value !== undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":";;;AAAA,qCAA4F;AAC5F,yCAAsG;AACtG,yBAAuB;AACvB,mEAA8D;AAC9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AC7C,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,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, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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\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), ...next} = options;\n\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = objectKeys(src);\n const additionalProperties = propertiesMap.size ? !!store.schema.get(\"additionalProperties\") || options.additionalProperties : true;\n src = alterBeforeDeserialize(src, store.schema, options);\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 = keys.filter((k) => k !== key);\n\n next.type = propStore.computedType;\n\n if (alterIgnore(propStore.itemSchema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n if (propStore.schema.hasGenerics) {\n next.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 next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (next.type instanceof JsonSchema) {\n next.type = next.type.getTarget();\n }\n\n next.nestedGenerics = nestedGenerics;\n }\n\n value = deserialize(value, {\n ...next,\n type: value === src[key] ? next.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,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,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,OAA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AA,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"]}
|
|
@@ -3,7 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.serialize = exports.classToPlainObject = void 0;
|
|
4
4
|
const core_1 = require("@tsed/core");
|
|
5
5
|
const schema_1 = require("@tsed/schema");
|
|
6
|
-
require("../components");
|
|
6
|
+
require("../components/ArrayMapper");
|
|
7
|
+
require("../components/DateMapper");
|
|
8
|
+
require("../components/MapMapper");
|
|
9
|
+
require("../components/PrimitiveMapper");
|
|
10
|
+
require("../components/SetMapper");
|
|
11
|
+
require("../components/SymbolMapper");
|
|
7
12
|
const JsonMapperContext_1 = require("../domain/JsonMapperContext");
|
|
8
13
|
const JsonMapperTypesContainer_1 = require("../domain/JsonMapperTypesContainer");
|
|
9
14
|
function alterValue(schema, value, options) {
|
|
@@ -32,7 +37,11 @@ function getType(propStore, value) {
|
|
|
32
37
|
function classToPlainObject(obj, options) {
|
|
33
38
|
const { useAlias = true, type, ...props } = options;
|
|
34
39
|
const entity = schema_1.JsonEntityStore.from(type || obj);
|
|
35
|
-
|
|
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);
|
|
36
45
|
const schema = propStore.schema;
|
|
37
46
|
if ((0, schema_1.alterIgnore)(schema, { useAlias, ...props, self: obj })) {
|
|
38
47
|
return newObj;
|
|
@@ -40,6 +49,7 @@ function classToPlainObject(obj, options) {
|
|
|
40
49
|
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
41
50
|
value = serialize(value, {
|
|
42
51
|
useAlias,
|
|
52
|
+
self: obj,
|
|
43
53
|
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
44
54
|
collectionType: propStore.collectionType,
|
|
45
55
|
...props
|
|
@@ -53,6 +63,14 @@ function classToPlainObject(obj, options) {
|
|
|
53
63
|
[key]: value
|
|
54
64
|
};
|
|
55
65
|
}, {});
|
|
66
|
+
if (additionalProperties) {
|
|
67
|
+
(0, core_1.objectKeys)(obj).forEach((key) => {
|
|
68
|
+
if (!properties.has(key)) {
|
|
69
|
+
out[key] = obj[key];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
56
74
|
}
|
|
57
75
|
exports.classToPlainObject = classToPlainObject;
|
|
58
76
|
function toObject(obj, options) {
|
|
@@ -80,7 +98,7 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
80
98
|
if ((typeof obj.toJSON === "function" && obj.$isMongooseModelPrototype) || obj._bsontype) {
|
|
81
99
|
return obj.toJSON(options);
|
|
82
100
|
}
|
|
83
|
-
if (typeof obj.toJSON === "function") {
|
|
101
|
+
if (!(obj && (obj === null || obj === void 0 ? void 0 : obj._isAMomentObject)) && typeof obj.toJSON === "function") {
|
|
84
102
|
return serialize(obj.toJSON(), { ...options, type: (0, core_1.classOf)(obj) });
|
|
85
103
|
}
|
|
86
104
|
if (!(0, core_1.isCollection)(obj)) {
|
|
@@ -100,10 +118,14 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
100
118
|
type: options.type
|
|
101
119
|
})
|
|
102
120
|
});
|
|
103
|
-
|
|
104
|
-
|
|
121
|
+
const mapper = types.get(currentType) || types.get((0, core_1.nameOf)(currentType));
|
|
122
|
+
if (mapper) {
|
|
123
|
+
const jsonMapper = mapper;
|
|
105
124
|
return jsonMapper.serialize(obj, context);
|
|
106
125
|
}
|
|
126
|
+
else if (obj === null || obj === void 0 ? void 0 : obj._isAMomentObject) {
|
|
127
|
+
return obj.toJSON();
|
|
128
|
+
}
|
|
107
129
|
if ((0, core_1.isArray)(obj)) {
|
|
108
130
|
// Serialize Array class like
|
|
109
131
|
return (_a = types.get(Array)) === null || _a === void 0 ? void 0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AAE,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;AAlDD,8BAkDC","sourcesContent":["import {classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, MetadataTypes, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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 return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {\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 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\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 (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 if (types.has(currentType)) {\n const jsonMapper = types.get(currentType)!;\n\n return jsonMapper.serialize(obj, context);\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,qCAYoB;AACpB,yCAA4G;AAC5G,qCAAmC;AACnC,oCAAkC;AAClC,mCAAiC;AACjC,yCAAuC;AACvC,mCAAiC;AACjC,sCAAoC;AACpC,mEAA8D;AAC9D,iFAAsE;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,IAAA,4BAAmB,EAAC,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;IAErE,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QAClB,8CAA8C;QAC9C,IAAA,iBAAU,EAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC9B,MAAM,SAAS,GAAG,wBAAe,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YAC/D,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC;KACJ;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,mBAAmB,CAAC,GAAQ;IACnC,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;AAED,SAAS,OAAO,CA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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,6 +1,6 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
2
|
import { BadRequest } from "@tsed/exceptions";
|
|
3
|
-
import { JsonMapper } from "../decorators/jsonMapper";
|
|
3
|
+
import { JsonMapper } from "../decorators/jsonMapper.js";
|
|
4
4
|
/**
|
|
5
5
|
* Mapper for the `String`, `Number` and `Boolean` types.
|
|
6
6
|
* @jsonmapper
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JsonMapperTypesContainer.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperTypesContainer.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,yCAAyC;AACzC,MAAM,wBAAwB,
|
|
1
|
+
{"version":3,"file":"JsonMapperTypesContainer.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperTypesContainer.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,yCAAyC;AACzC,MAAM,wBAAwB,GAAwD,IAAI,GAAG,EAAE,CAAC;AAChG;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAiC,EAAE,KAA8B;IACtG,wBAAwB,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC;AAClD,CAAC;AACD;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO,wBAAwB,CAAC;AAClC,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nconst JsonMapperTypesContainer: Map<Type<any> | Symbol | string, JsonMapperMethods> = new Map();\n/**\n * @ignore\n */\nexport function registerJsonTypeMapper(type: Type<any> | Symbol | string, token: Type<JsonMapperMethods>) {\n JsonMapperTypesContainer.set(type, new token());\n}\n/**\n * @ignore\n */\nexport function getJsonMapperTypes(): Map<Type<any> | Symbol | string, JsonMapperMethods> {\n return JsonMapperTypesContainer;\n}\n"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
6
|
-
export * from "./
|
|
7
|
-
export * from "./
|
|
8
|
-
export * from "./
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./
|
|
11
|
-
export * from "./
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./components/ArrayMapper.js";
|
|
5
|
+
export * from "./components/DateMapper.js";
|
|
6
|
+
export * from "./components/MapMapper.js";
|
|
7
|
+
export * from "./components/PrimitiveMapper.js";
|
|
8
|
+
export * from "./components/SetMapper.js";
|
|
9
|
+
export * from "./components/SymbolMapper.js";
|
|
10
|
+
export * from "./decorators/afterDeserialize.js";
|
|
11
|
+
export * from "./decorators/beforeDeserialize.js";
|
|
12
|
+
export * from "./decorators/jsonMapper.js";
|
|
13
|
+
export * from "./decorators/onDeserialize.js";
|
|
14
|
+
export * from "./decorators/onSerialize.js";
|
|
15
|
+
export * from "./domain/JsonMapperContext.js";
|
|
16
|
+
export * from "./domain/JsonMapperTypesContainer.js";
|
|
17
|
+
export * from "./hooks/alterAfterDeserialize.js";
|
|
18
|
+
export * from "./hooks/alterBeforeDeserialize.js";
|
|
19
|
+
export * from "./interfaces/JsonMapperMethods.js";
|
|
20
|
+
export * from "./utils/deserialize.js";
|
|
21
|
+
export * from "./utils/serialize.js";
|
|
12
22
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./components/ArrayMapper\";\nexport * from \"./components/DateMapper\";\nexport * from \"./components/MapMapper\";\nexport * from \"./components/PrimitiveMapper\";\nexport * from \"./components/SetMapper\";\nexport * from \"./components/SymbolMapper\";\nexport * from \"./decorators/afterDeserialize\";\nexport * from \"./decorators/beforeDeserialize\";\nexport * from \"./decorators/jsonMapper\";\nexport * from \"./decorators/onDeserialize\";\nexport * from \"./decorators/onSerialize\";\nexport * from \"./domain/JsonMapperContext\";\nexport * from \"./domain/JsonMapperTypesContainer\";\nexport * from \"./hooks/alterAfterDeserialize\";\nexport * from \"./hooks/alterBeforeDeserialize\";\nexport * from \"./interfaces/JsonMapperMethods\";\nexport * from \"./utils/deserialize\";\nexport * from \"./utils/serialize\";\n"]}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { isArray, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
2
|
import { alterIgnore, getProperties, JsonEntityStore, JsonSchema } from "@tsed/schema";
|
|
3
|
-
import "../components";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
3
|
+
import "../components/ArrayMapper.js";
|
|
4
|
+
import "../components/DateMapper.js";
|
|
5
|
+
import "../components/MapMapper.js";
|
|
6
|
+
import "../components/PrimitiveMapper.js";
|
|
7
|
+
import "../components/SetMapper.js";
|
|
8
|
+
import "../components/SymbolMapper.js";
|
|
9
|
+
import { JsonMapperContext } from "../domain/JsonMapperContext.js";
|
|
10
|
+
import { getJsonMapperTypes } from "../domain/JsonMapperTypesContainer.js";
|
|
11
|
+
import { alterAfterDeserialize } from "../hooks/alterAfterDeserialize.js";
|
|
12
|
+
import { alterBeforeDeserialize } from "../hooks/alterBeforeDeserialize.js";
|
|
8
13
|
function isDeserializable(obj, options) {
|
|
9
14
|
if ((!!options.collectionType && isNil(obj)) || obj === undefined) {
|
|
10
15
|
return false;
|
|
@@ -35,6 +40,26 @@ function transformType(src, options) {
|
|
|
35
40
|
});
|
|
36
41
|
return (_a = types === null || types === void 0 ? void 0 : types.get(type)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
37
42
|
}
|
|
43
|
+
function mapItemOptions(propStore, options) {
|
|
44
|
+
const itemOpts = {
|
|
45
|
+
...options,
|
|
46
|
+
store: undefined,
|
|
47
|
+
type: propStore.computedType
|
|
48
|
+
};
|
|
49
|
+
if (propStore.schema.hasGenerics) {
|
|
50
|
+
itemOpts.nestedGenerics = propStore.schema.nestedGenerics;
|
|
51
|
+
}
|
|
52
|
+
else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
53
|
+
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
54
|
+
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
55
|
+
itemOpts.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
56
|
+
if (itemOpts.type instanceof JsonSchema) {
|
|
57
|
+
itemOpts.type = itemOpts.type.getTarget();
|
|
58
|
+
}
|
|
59
|
+
itemOpts.nestedGenerics = nestedGenerics;
|
|
60
|
+
}
|
|
61
|
+
return itemOpts;
|
|
62
|
+
}
|
|
38
63
|
/**
|
|
39
64
|
* Transform given plain object to class.
|
|
40
65
|
* @param src
|
|
@@ -44,9 +69,9 @@ export function plainObjectToClass(src, options) {
|
|
|
44
69
|
if (isEmpty(src)) {
|
|
45
70
|
return src;
|
|
46
71
|
}
|
|
47
|
-
const { type, store = JsonEntityStore.from(type)
|
|
72
|
+
const { type, store = JsonEntityStore.from(type) } = options;
|
|
48
73
|
const propertiesMap = getProperties(store, { ...options, withIgnoredProps: true });
|
|
49
|
-
let keys = objectKeys(src);
|
|
74
|
+
let keys = new Set(objectKeys(src));
|
|
50
75
|
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
|
|
51
76
|
src = alterBeforeDeserialize(src, store.schema, options);
|
|
52
77
|
const out = new type(src);
|
|
@@ -54,27 +79,16 @@ export function plainObjectToClass(src, options) {
|
|
|
54
79
|
const key = options.useAlias
|
|
55
80
|
? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName
|
|
56
81
|
: propStore.propertyName;
|
|
57
|
-
keys
|
|
58
|
-
|
|
59
|
-
if (alterIgnore(propStore.itemSchema, options)) {
|
|
82
|
+
keys.delete(key);
|
|
83
|
+
if (alterIgnore(propStore.schema, options)) {
|
|
60
84
|
return;
|
|
61
85
|
}
|
|
62
86
|
let value = alterValue(propStore.schema, src[key], { ...options, self: src });
|
|
63
|
-
|
|
64
|
-
next.nestedGenerics = propStore.schema.nestedGenerics;
|
|
65
|
-
}
|
|
66
|
-
else if (propStore.schema.isGeneric && options.nestedGenerics) {
|
|
67
|
-
const [genericTypes = [], ...nestedGenerics] = options.nestedGenerics;
|
|
68
|
-
const genericLabels = propStore.parent.schema.genericLabels || [];
|
|
69
|
-
next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;
|
|
70
|
-
if (next.type instanceof JsonSchema) {
|
|
71
|
-
next.type = next.type.getTarget();
|
|
72
|
-
}
|
|
73
|
-
next.nestedGenerics = nestedGenerics;
|
|
74
|
-
}
|
|
87
|
+
const itemOptions = mapItemOptions(propStore, options);
|
|
75
88
|
value = deserialize(value, {
|
|
76
|
-
...
|
|
77
|
-
|
|
89
|
+
...itemOptions,
|
|
90
|
+
self: src,
|
|
91
|
+
type: value === src[key] ? itemOptions.type : undefined,
|
|
78
92
|
collectionType: propStore.collectionType
|
|
79
93
|
});
|
|
80
94
|
if (value !== undefined) {
|
|
@@ -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,EAAmB,UAAU,EAAC,MAAM,cAAc,CAAC;AACtG,OAAO,eAAe,CAAC;AACvB,OAAO,EAAC,iBAAiB,EAAC,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAC,kBAAkB,EAAC,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAC,qBAAqB,EAAC,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAC,sBAAsB,EAAC,MAAM,iCAAiC,CAAC;AA6BvE,SAAS,gBAAgB,CAAC,GAAQ,EAAE,OAAgC;IAClE,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,GAAG,KAAK,SAAS,EAAE;QACjE,OAAO,KAAK,CAAC;KACd;IAED,OAAO,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,C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AG,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,MAAM,CAAC,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","sourcesContent":["import {isArray, isEmpty, isNil, MetadataTypes, nameOf, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getProperties, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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\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), ...next} = options;\n\n const propertiesMap = getProperties(store, {...options, withIgnoredProps: true});\n\n let keys = objectKeys(src);\n const additionalProperties = propertiesMap.size ? !!store.schema.get(\"additionalProperties\") || options.additionalProperties : true;\n src = alterBeforeDeserialize(src, store.schema, options);\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 = keys.filter((k) => k !== key);\n\n next.type = propStore.computedType;\n\n if (alterIgnore(propStore.itemSchema, options)) {\n return;\n }\n\n let value = alterValue(propStore.schema, src[key], {...options, self: src});\n\n if (propStore.schema.hasGenerics) {\n next.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 next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;\n\n if (next.type instanceof JsonSchema) {\n next.type = next.type.getTarget();\n }\n\n next.nestedGenerics = nestedGenerics;\n }\n\n value = deserialize(value, {\n ...next,\n type: value === src[key] ? next.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,OAAO,EAAE,KAAK,EAAiB,MAAM,EAAE,UAAU,EAAO,MAAM,YAAY,CAAC;AAC5F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AC,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","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,8 +1,13 @@
|
|
|
1
|
-
import { classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, objectKeys } from "@tsed/core";
|
|
1
|
+
import { classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
2
|
import { alterIgnore, getPropertiesStores, JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import "../components";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
3
|
+
import "../components/ArrayMapper.js";
|
|
4
|
+
import "../components/DateMapper.js";
|
|
5
|
+
import "../components/MapMapper.js";
|
|
6
|
+
import "../components/PrimitiveMapper.js";
|
|
7
|
+
import "../components/SetMapper.js";
|
|
8
|
+
import "../components/SymbolMapper.js";
|
|
9
|
+
import { JsonMapperContext } from "../domain/JsonMapperContext.js";
|
|
10
|
+
import { getJsonMapperTypes } from "../domain/JsonMapperTypesContainer.js";
|
|
6
11
|
function alterValue(schema, value, options) {
|
|
7
12
|
return schema.$hooks.alter("onSerialize", value, [options]);
|
|
8
13
|
}
|
|
@@ -29,7 +34,11 @@ function getType(propStore, value) {
|
|
|
29
34
|
export function classToPlainObject(obj, options) {
|
|
30
35
|
const { useAlias = true, type, ...props } = options;
|
|
31
36
|
const entity = JsonEntityStore.from(type || obj);
|
|
32
|
-
|
|
37
|
+
const additionalProperties = !!entity.schema.get("additionalProperties");
|
|
38
|
+
const schemaProperties = getSchemaProperties(entity, obj);
|
|
39
|
+
const properties = new Set();
|
|
40
|
+
const out = schemaProperties.reduce((newObj, [key, propStore]) => {
|
|
41
|
+
properties.add(key);
|
|
33
42
|
const schema = propStore.schema;
|
|
34
43
|
if (alterIgnore(schema, { useAlias, ...props, self: obj })) {
|
|
35
44
|
return newObj;
|
|
@@ -37,6 +46,7 @@ export function classToPlainObject(obj, options) {
|
|
|
37
46
|
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
38
47
|
value = serialize(value, {
|
|
39
48
|
useAlias,
|
|
49
|
+
self: obj,
|
|
40
50
|
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
41
51
|
collectionType: propStore.collectionType,
|
|
42
52
|
...props
|
|
@@ -50,6 +60,14 @@ export function classToPlainObject(obj, options) {
|
|
|
50
60
|
[key]: value
|
|
51
61
|
};
|
|
52
62
|
}, {});
|
|
63
|
+
if (additionalProperties) {
|
|
64
|
+
objectKeys(obj).forEach((key) => {
|
|
65
|
+
if (!properties.has(key)) {
|
|
66
|
+
out[key] = obj[key];
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
53
71
|
}
|
|
54
72
|
function toObject(obj, options) {
|
|
55
73
|
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
@@ -76,7 +94,7 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
76
94
|
if ((typeof obj.toJSON === "function" && obj.$isMongooseModelPrototype) || obj._bsontype) {
|
|
77
95
|
return obj.toJSON(options);
|
|
78
96
|
}
|
|
79
|
-
if (typeof obj.toJSON === "function") {
|
|
97
|
+
if (!(obj && (obj === null || obj === void 0 ? void 0 : obj._isAMomentObject)) && typeof obj.toJSON === "function") {
|
|
80
98
|
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
81
99
|
}
|
|
82
100
|
if (!isCollection(obj)) {
|
|
@@ -96,10 +114,14 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
96
114
|
type: options.type
|
|
97
115
|
})
|
|
98
116
|
});
|
|
99
|
-
|
|
100
|
-
|
|
117
|
+
const mapper = types.get(currentType) || types.get(nameOf(currentType));
|
|
118
|
+
if (mapper) {
|
|
119
|
+
const jsonMapper = mapper;
|
|
101
120
|
return jsonMapper.serialize(obj, context);
|
|
102
121
|
}
|
|
122
|
+
else if (obj === null || obj === void 0 ? void 0 : obj._isAMomentObject) {
|
|
123
|
+
return obj.toJSON();
|
|
124
|
+
}
|
|
103
125
|
if (isArray(obj)) {
|
|
104
126
|
// Serialize Array class like
|
|
105
127
|
return (_a = types.get(Array)) === null || _a === void 0 ? void 0 : _a.serialize(obj, context);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ser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AE;QAChB,6BAA6B;QAC7B,OAAO,MAAA,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,0CAAE,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 {classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, MetadataTypes, objectKeys, Type} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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 return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {\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 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\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 (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 if (types.has(currentType)) {\n const jsonMapper = types.get(currentType)!;\n\n return jsonMapper.serialize(obj, context);\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,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,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC9B,MAAM,SAAS,GAAG,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,MAA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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 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"]}
|
|
@@ -3,8 +3,8 @@ import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
|
3
3
|
/**
|
|
4
4
|
* @ignore
|
|
5
5
|
*/
|
|
6
|
-
export declare function registerJsonTypeMapper(type: Type<any
|
|
6
|
+
export declare function registerJsonTypeMapper(type: Type<any> | Symbol | string, token: Type<JsonMapperMethods>): void;
|
|
7
7
|
/**
|
|
8
8
|
* @ignore
|
|
9
9
|
*/
|
|
10
|
-
export declare function getJsonMapperTypes():
|
|
10
|
+
export declare function getJsonMapperTypes(): Map<Type<any> | Symbol | string, JsonMapperMethods>;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./components/ArrayMapper";
|
|
5
|
+
export * from "./components/DateMapper";
|
|
6
|
+
export * from "./components/MapMapper";
|
|
7
|
+
export * from "./components/PrimitiveMapper";
|
|
8
|
+
export * from "./components/SetMapper";
|
|
9
|
+
export * from "./components/SymbolMapper";
|
|
10
|
+
export * from "./decorators/afterDeserialize";
|
|
11
|
+
export * from "./decorators/beforeDeserialize";
|
|
2
12
|
export * from "./decorators/jsonMapper";
|
|
3
13
|
export * from "./decorators/onDeserialize";
|
|
4
14
|
export * from "./decorators/onSerialize";
|
|
5
|
-
export * from "./decorators/afterDeserialize";
|
|
6
|
-
export * from "./decorators/beforeDeserialize";
|
|
7
15
|
export * from "./domain/JsonMapperContext";
|
|
8
16
|
export * from "./domain/JsonMapperTypesContainer";
|
|
17
|
+
export * from "./hooks/alterAfterDeserialize";
|
|
18
|
+
export * from "./hooks/alterBeforeDeserialize";
|
|
9
19
|
export * from "./interfaces/JsonMapperMethods";
|
|
10
20
|
export * from "./utils/deserialize";
|
|
11
21
|
export * from "./utils/serialize";
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { MetadataTypes, Type } from "@tsed/core";
|
|
2
2
|
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import "../components";
|
|
3
|
+
import "../components/ArrayMapper";
|
|
4
|
+
import "../components/DateMapper";
|
|
5
|
+
import "../components/MapMapper";
|
|
6
|
+
import "../components/PrimitiveMapper";
|
|
7
|
+
import "../components/SetMapper";
|
|
8
|
+
import "../components/SymbolMapper";
|
|
4
9
|
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
5
10
|
export interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {
|
|
6
11
|
/**
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { MetadataTypes, Type } from "@tsed/core";
|
|
2
|
-
import "../components";
|
|
2
|
+
import "../components/ArrayMapper";
|
|
3
|
+
import "../components/DateMapper";
|
|
4
|
+
import "../components/MapMapper";
|
|
5
|
+
import "../components/PrimitiveMapper";
|
|
6
|
+
import "../components/SetMapper";
|
|
7
|
+
import "../components/SymbolMapper";
|
|
3
8
|
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
4
9
|
export interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {
|
|
5
10
|
/**
|
|
@@ -12,5 +17,5 @@ export interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T
|
|
|
12
17
|
useAlias?: boolean;
|
|
13
18
|
[key: string]: any;
|
|
14
19
|
}
|
|
15
|
-
export declare function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>):
|
|
20
|
+
export declare function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>): any;
|
|
16
21
|
export declare function serialize(obj: any, { type, collectionType, groups, ...options }?: JsonSerializerOptions): any;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/json-mapper",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.4",
|
|
4
4
|
"description": "Json mapper module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/cjs/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
8
9
|
"typings": "./lib/types/index.d.ts",
|
|
9
10
|
"exports": {
|
|
10
11
|
"types": "./lib/types/index.d.ts",
|
|
@@ -13,9 +14,10 @@
|
|
|
13
14
|
"default": "./lib/esm/index.js"
|
|
14
15
|
},
|
|
15
16
|
"scripts": {
|
|
16
|
-
"build": "yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
17
18
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
|
-
"build:esm": "tsc --build tsconfig.compile.esm.json"
|
|
19
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
20
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
|
|
19
21
|
},
|
|
20
22
|
"keywords": [
|
|
21
23
|
"TypeScript",
|
|
@@ -30,23 +32,34 @@
|
|
|
30
32
|
"tsed"
|
|
31
33
|
],
|
|
32
34
|
"dependencies": {
|
|
33
|
-
"tslib": "2.
|
|
35
|
+
"tslib": "2.3.1"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@tsed/core": "7.0.0-beta.
|
|
37
|
-
"@tsed/exceptions": "7.0.0-beta.
|
|
38
|
-
"@tsed/schema": "7.0.0-beta.
|
|
38
|
+
"@tsed/core": "7.0.0-beta.4",
|
|
39
|
+
"@tsed/exceptions": "7.0.0-beta.4",
|
|
40
|
+
"@tsed/schema": "7.0.0-beta.4"
|
|
39
41
|
},
|
|
40
42
|
"peerDependencies": {
|
|
41
|
-
"@tsed/core": "^7.0.0-beta.
|
|
42
|
-
"@tsed/exceptions": "^7.0.0-beta.
|
|
43
|
-
"@tsed/schema": "^7.0.0-beta.
|
|
43
|
+
"@tsed/core": "^7.0.0-beta.4",
|
|
44
|
+
"@tsed/exceptions": "^7.0.0-beta.4",
|
|
45
|
+
"@tsed/schema": "^7.0.0-beta.4"
|
|
46
|
+
},
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"@tsed/core": {
|
|
49
|
+
"optional": false
|
|
50
|
+
},
|
|
51
|
+
"@tsed/exceptions": {
|
|
52
|
+
"optional": false
|
|
53
|
+
},
|
|
54
|
+
"@tsed/schema": {
|
|
55
|
+
"optional": false
|
|
56
|
+
}
|
|
44
57
|
},
|
|
45
58
|
"repository": "https://github.com/tsedio/tsed",
|
|
46
59
|
"bugs": {
|
|
47
60
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
48
61
|
},
|
|
49
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
62
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/specs/json-mapper",
|
|
50
63
|
"author": "Romain Lenzotti",
|
|
51
64
|
"license": "MIT"
|
|
52
65
|
}
|
package/readme.md
CHANGED
|
@@ -49,6 +49,7 @@ npm install --save @tsed/schema @tsed/json-mapper
|
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
## Contributors
|
|
52
|
+
|
|
52
53
|
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
53
54
|
|
|
54
55
|
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
@@ -8,16 +8,5 @@
|
|
|
8
8
|
"declaration": true,
|
|
9
9
|
"declarationDir": "./lib/types"
|
|
10
10
|
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
"node_modules",
|
|
13
|
-
"test",
|
|
14
|
-
"lib",
|
|
15
|
-
"benchmark",
|
|
16
|
-
"coverage",
|
|
17
|
-
"spec",
|
|
18
|
-
"**/*.benchmark.ts",
|
|
19
|
-
"**/*.spec.ts",
|
|
20
|
-
"keys",
|
|
21
|
-
"jest.config.js"
|
|
22
|
-
]
|
|
11
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
23
12
|
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
(0, tslib_1.__exportStar)(require("./ArrayMapper"), exports);
|
|
5
|
-
(0, tslib_1.__exportStar)(require("./DateMapper"), exports);
|
|
6
|
-
(0, tslib_1.__exportStar)(require("./MapMapper"), exports);
|
|
7
|
-
(0, tslib_1.__exportStar)(require("./PrimitiveMapper"), exports);
|
|
8
|
-
(0, tslib_1.__exportStar)(require("./SetMapper"), exports);
|
|
9
|
-
(0, tslib_1.__exportStar)(require("./SymbolMapper"), exports);
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":";;;AAAA,6DAA8B;AAC9B,4DAA6B;AAC7B,2DAA4B;AAC5B,iEAAkC;AAClC,2DAA4B;AAC5B,8DAA+B","sourcesContent":["export * from \"./ArrayMapper\";\nexport * from \"./DateMapper\";\nexport * from \"./MapMapper\";\nexport * from \"./PrimitiveMapper\";\nexport * from \"./SetMapper\";\nexport * from \"./SymbolMapper\";\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC","sourcesContent":["export * from \"./ArrayMapper\";\nexport * from \"./DateMapper\";\nexport * from \"./MapMapper\";\nexport * from \"./PrimitiveMapper\";\nexport * from \"./SetMapper\";\nexport * from \"./SymbolMapper\";\n"]}
|