@tsed/json-mapper 6.95.6 → 6.95.10
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/utils/serialize.js +9 -7
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/utils/serialize.js +9 -7
- package/lib/esm/utils/serialize.js.map +1 -1
- package/package.json +7 -7
- package/lib/cjs/components/ArrayMapper.d.ts +0 -10
- package/lib/cjs/components/DateMapper.d.ts +0 -11
- package/lib/cjs/components/MapMapper.d.ts +0 -12
- package/lib/cjs/components/PrimitiveMapper.d.ts +0 -10
- package/lib/cjs/components/SetMapper.d.ts +0 -10
- package/lib/cjs/components/SymbolMapper.d.ts +0 -11
- package/lib/cjs/components/index.d.ts +0 -6
- package/lib/cjs/decorators/afterDeserialize.d.ts +0 -33
- package/lib/cjs/decorators/beforeDeserialize.d.ts +0 -33
- package/lib/cjs/decorators/jsonMapper.d.ts +0 -7
- package/lib/cjs/decorators/onDeserialize.d.ts +0 -23
- package/lib/cjs/decorators/onSerialize.d.ts +0 -23
- package/lib/cjs/domain/JsonMapperContext.d.ts +0 -20
- package/lib/cjs/domain/JsonMapperTypesContainer.d.ts +0 -10
- package/lib/cjs/hooks/alterAfterDeserialize.d.ts +0 -4
- package/lib/cjs/hooks/alterBeforeDeserialize.d.ts +0 -4
- package/lib/cjs/index.d.ts +0 -11
- package/lib/cjs/interfaces/JsonMapperMethods.d.ts +0 -25
- package/lib/cjs/utils/deserialize.d.ts +0 -41
- package/lib/cjs/utils/serialize.d.ts +0 -16
|
@@ -73,6 +73,7 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
73
73
|
if ((0, core_1.isEmpty)(obj)) {
|
|
74
74
|
return obj;
|
|
75
75
|
}
|
|
76
|
+
let currentType = type;
|
|
76
77
|
const types = options.types ? options.types : (0, JsonMapperTypesContainer_1.getJsonMapperTypes)();
|
|
77
78
|
options.groups = groups;
|
|
78
79
|
// FIX custom serialization function from @tsed/mongoose and bson
|
|
@@ -83,14 +84,15 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
83
84
|
return serialize(obj.toJSON(), { ...options, type: (0, core_1.classOf)(obj) });
|
|
84
85
|
}
|
|
85
86
|
if (!(0, core_1.isCollection)(obj)) {
|
|
86
|
-
options.type =
|
|
87
|
+
options.type = currentType = getBestType(currentType, obj);
|
|
87
88
|
}
|
|
88
89
|
else if (!options.collectionType) {
|
|
89
|
-
|
|
90
|
-
options.
|
|
90
|
+
currentType = (0, core_1.classOf)(obj);
|
|
91
|
+
options.type = type;
|
|
92
|
+
options.collectionType = currentType;
|
|
91
93
|
}
|
|
92
94
|
const context = new JsonMapperContext_1.JsonMapperContext({
|
|
93
|
-
type,
|
|
95
|
+
type: currentType,
|
|
94
96
|
options,
|
|
95
97
|
next: (data) => serialize(data, {
|
|
96
98
|
...options,
|
|
@@ -98,15 +100,15 @@ function serialize(obj, { type, collectionType, groups = false, ...options } = {
|
|
|
98
100
|
type: options.type
|
|
99
101
|
})
|
|
100
102
|
});
|
|
101
|
-
if (types.has(
|
|
102
|
-
const jsonMapper = types.get(
|
|
103
|
+
if (types.has(currentType)) {
|
|
104
|
+
const jsonMapper = types.get(currentType);
|
|
103
105
|
return jsonMapper.serialize(obj, context);
|
|
104
106
|
}
|
|
105
107
|
if ((0, core_1.isArray)(obj)) {
|
|
106
108
|
// Serialize Array class like
|
|
107
109
|
return (_a = types.get(Array)) === null || _a === void 0 ? void 0 : _a.serialize(obj, context);
|
|
108
110
|
}
|
|
109
|
-
return !(0, core_1.isClassObject)(
|
|
111
|
+
return !(0, core_1.isClassObject)(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
110
112
|
}
|
|
111
113
|
exports.serialize = serialize;
|
|
112
114
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mapp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CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;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;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"]}
|
|
@@ -69,6 +69,7 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
69
69
|
if (isEmpty(obj)) {
|
|
70
70
|
return obj;
|
|
71
71
|
}
|
|
72
|
+
let currentType = type;
|
|
72
73
|
const types = options.types ? options.types : getJsonMapperTypes();
|
|
73
74
|
options.groups = groups;
|
|
74
75
|
// FIX custom serialization function from @tsed/mongoose and bson
|
|
@@ -79,14 +80,15 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
79
80
|
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
80
81
|
}
|
|
81
82
|
if (!isCollection(obj)) {
|
|
82
|
-
options.type =
|
|
83
|
+
options.type = currentType = getBestType(currentType, obj);
|
|
83
84
|
}
|
|
84
85
|
else if (!options.collectionType) {
|
|
85
|
-
|
|
86
|
-
options.
|
|
86
|
+
currentType = classOf(obj);
|
|
87
|
+
options.type = type;
|
|
88
|
+
options.collectionType = currentType;
|
|
87
89
|
}
|
|
88
90
|
const context = new JsonMapperContext({
|
|
89
|
-
type,
|
|
91
|
+
type: currentType,
|
|
90
92
|
options,
|
|
91
93
|
next: (data) => serialize(data, {
|
|
92
94
|
...options,
|
|
@@ -94,14 +96,14 @@ export function serialize(obj, { type, collectionType, groups = false, ...option
|
|
|
94
96
|
type: options.type
|
|
95
97
|
})
|
|
96
98
|
});
|
|
97
|
-
if (types.has(
|
|
98
|
-
const jsonMapper = types.get(
|
|
99
|
+
if (types.has(currentType)) {
|
|
100
|
+
const jsonMapper = types.get(currentType);
|
|
99
101
|
return jsonMapper.serialize(obj, context);
|
|
100
102
|
}
|
|
101
103
|
if (isArray(obj)) {
|
|
102
104
|
// Serialize Array class like
|
|
103
105
|
return (_a = types.get(Array)) === null || _a === void 0 ? void 0 : _a.serialize(obj, context);
|
|
104
106
|
}
|
|
105
|
-
return !isClassObject(
|
|
107
|
+
return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
106
108
|
}
|
|
107
109
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAiB,UAAU,EAAO,MAAM,YAAY,CAAC;AACtI,OAAO,EAAC,WAAW,EAAE,mBAAmB,EAAE,eAAe,EAA8B,MAAM,cAAc,CAAC;AAC5G,OAAO,eAAe,CAAC;AACvB,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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AC,EAAC,CAAC,CAAC;KAClE;IAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE;QACtB,OAAO,CAAC,IAAI,GAAG,WAAW,GAAG,WAAW,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;KAC5D;SAAM,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE;QAClC,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;QAC3B,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QACpB,OAAO,CAAC,cAAc,GAAG,WAAW,CAAC;KACtC;IAED,MAAM,OAAO,GAAG,IAAI,iBAAiB,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,IAAI,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;QAC1B,MAAM,UAAU,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAE,CAAC;QAE3C,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;IAED,IAAI,OAAO,CAAC,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,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/json-mapper",
|
|
3
|
-
"version": "6.95.
|
|
3
|
+
"version": "6.95.10",
|
|
4
4
|
"description": "Json mapper module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
"tslib": "2.2.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@tsed/core": "6.95.
|
|
37
|
-
"@tsed/exceptions": "6.95.
|
|
38
|
-
"@tsed/schema": "6.95.
|
|
36
|
+
"@tsed/core": "6.95.10",
|
|
37
|
+
"@tsed/exceptions": "6.95.10",
|
|
38
|
+
"@tsed/schema": "6.95.10"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@tsed/core": "^6.95.
|
|
42
|
-
"@tsed/exceptions": "^6.95.
|
|
43
|
-
"@tsed/schema": "^6.95.
|
|
41
|
+
"@tsed/core": "^6.95.10",
|
|
42
|
+
"@tsed/exceptions": "^6.95.10",
|
|
43
|
+
"@tsed/schema": "^6.95.10"
|
|
44
44
|
},
|
|
45
45
|
"repository": "https://github.com/tsedio/tsed",
|
|
46
46
|
"bugs": {
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { JsonMapperCtx, JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for `Array` type.
|
|
4
|
-
* @jsonmapper
|
|
5
|
-
* @component
|
|
6
|
-
*/
|
|
7
|
-
export declare class ArrayMapper implements JsonMapperMethods {
|
|
8
|
-
deserialize<T = any>(data: any, options: JsonMapperCtx): T[];
|
|
9
|
-
serialize(data: any[], options: JsonMapperCtx): any;
|
|
10
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for `Date` type.
|
|
4
|
-
* @jsonmapper
|
|
5
|
-
* @component
|
|
6
|
-
*/
|
|
7
|
-
export declare class DateMapper implements JsonMapperMethods {
|
|
8
|
-
deserialize(data: string | number): Date;
|
|
9
|
-
deserialize(data: boolean | null | undefined): boolean | null | undefined;
|
|
10
|
-
serialize(object: Date): any;
|
|
11
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { JsonMapperCtx, JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for `Map` type.
|
|
4
|
-
* @jsonmapper
|
|
5
|
-
* @component
|
|
6
|
-
*/
|
|
7
|
-
export declare class MapMapper implements JsonMapperMethods {
|
|
8
|
-
deserialize<T = any, C = Map<string, T>>(data: {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
}, ctx: JsonMapperCtx<T, C>): Map<string, T>;
|
|
11
|
-
serialize<T>(data: Map<string, T>, ctx: JsonMapperCtx): any;
|
|
12
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { JsonMapperCtx, JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for the `String`, `Number` and `Boolean` types.
|
|
4
|
-
* @jsonmapper
|
|
5
|
-
* @component
|
|
6
|
-
*/
|
|
7
|
-
export declare class PrimitiveMapper implements JsonMapperMethods {
|
|
8
|
-
deserialize<T>(data: any, ctx: JsonMapperCtx): string | number | boolean | void | null;
|
|
9
|
-
serialize(object: string | number | boolean): string | number | boolean;
|
|
10
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { JsonMapperCtx, JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for the `Set` Type.
|
|
4
|
-
* @jsonmapper
|
|
5
|
-
* @component
|
|
6
|
-
*/
|
|
7
|
-
export declare class SetMapper implements JsonMapperMethods {
|
|
8
|
-
deserialize<T>(data: any, ctx: JsonMapperCtx): Set<T>;
|
|
9
|
-
serialize<T>(data: Set<T>, ctx: JsonMapperCtx): any[];
|
|
10
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
2
|
-
/**
|
|
3
|
-
* Mapper for the `Symbol` type.
|
|
4
|
-
*
|
|
5
|
-
* @jsonmapper
|
|
6
|
-
* @component
|
|
7
|
-
*/
|
|
8
|
-
export declare class SymbolMapper implements JsonMapperMethods {
|
|
9
|
-
deserialize(data: string): symbol;
|
|
10
|
-
serialize(object: Symbol): any;
|
|
11
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { JsonHookContext } from "@tsed/schema";
|
|
2
|
-
export interface AfterDeserializeCallback {
|
|
3
|
-
(value: any, ctx: JsonHookContext): any;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Call the function after JsonMapper.
|
|
7
|
-
*
|
|
8
|
-
* ## Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* @AfterDeserialize((data: Model) => { // After deserialize get the instance class
|
|
12
|
-
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
13
|
-
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
14
|
-
* } else {
|
|
15
|
-
* data.prop2 = 'VALUE2';
|
|
16
|
-
* return data;
|
|
17
|
-
* }
|
|
18
|
-
* })
|
|
19
|
-
* class Model {
|
|
20
|
-
* @Property()
|
|
21
|
-
* prop1: string;
|
|
22
|
-
* @Property()
|
|
23
|
-
* prop2: string;
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @returns {Function}
|
|
28
|
-
* @decorator
|
|
29
|
-
* @schema
|
|
30
|
-
* @property
|
|
31
|
-
* @param cb
|
|
32
|
-
*/
|
|
33
|
-
export declare function AfterDeserialize(cb: AfterDeserializeCallback): ClassDecorator;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { JsonHookContext } from "@tsed/schema";
|
|
2
|
-
export interface BeforeDeserializeCallback {
|
|
3
|
-
(value: any, ctx: JsonHookContext): any;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Call the function before JsonMapper.
|
|
7
|
-
*
|
|
8
|
-
* ## Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* @BeforeDeserialize((data: Record<string, unknown>) => { // Before deserialize get the data
|
|
12
|
-
* if (data.prop1 === 'VALUE' && data.prop2 !== 'VALUE2') {
|
|
13
|
-
* throw BadRequest('MyMessage'); // or throw a ValidationError
|
|
14
|
-
* } else {
|
|
15
|
-
* data.prop2 = 'VALUE2';
|
|
16
|
-
* return data;
|
|
17
|
-
* }
|
|
18
|
-
* })
|
|
19
|
-
* class Model {
|
|
20
|
-
* @Property()
|
|
21
|
-
* prop1: string;
|
|
22
|
-
* @Property()
|
|
23
|
-
* prop2: string;
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @returns {Function}
|
|
28
|
-
* @decorator
|
|
29
|
-
* @schema
|
|
30
|
-
* @property
|
|
31
|
-
* @param cb
|
|
32
|
-
*/
|
|
33
|
-
export declare function BeforeDeserialize(cb: BeforeDeserializeCallback): ClassDecorator;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { JsonHookContext } from "@tsed/schema";
|
|
2
|
-
export interface OnDeserializeCallback {
|
|
3
|
-
(value: any, ctx: JsonHookContext): any;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Call the function before property deserialization.
|
|
7
|
-
*
|
|
8
|
-
* ## Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* class Model {
|
|
12
|
-
* @OnDeserialize(v => v + 1)
|
|
13
|
-
* property: string;
|
|
14
|
-
* }
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @returns {Function}
|
|
18
|
-
* @decorator
|
|
19
|
-
* @schema
|
|
20
|
-
* @property
|
|
21
|
-
* @param cb
|
|
22
|
-
*/
|
|
23
|
-
export declare function OnDeserialize(cb: OnDeserializeCallback): PropertyDecorator;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { JsonHookContext } from "@tsed/schema";
|
|
2
|
-
export interface OnSerializeCallback {
|
|
3
|
-
(value: any, ctx: JsonHookContext): any;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Call the function before property serialization.
|
|
7
|
-
*
|
|
8
|
-
* ## Example
|
|
9
|
-
*
|
|
10
|
-
* ```typescript
|
|
11
|
-
* class Model {
|
|
12
|
-
* @OnSerialize(v => v + 1)
|
|
13
|
-
* property: string;
|
|
14
|
-
* }
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @returns {Function}
|
|
18
|
-
* @decorator
|
|
19
|
-
* @schema
|
|
20
|
-
* @property
|
|
21
|
-
* @param cb
|
|
22
|
-
*/
|
|
23
|
-
export declare function OnSerialize(cb: OnSerializeCallback): PropertyDecorator;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Type } from "@tsed/core";
|
|
2
|
-
import { JsonMapperCtx } from "../interfaces/JsonMapperMethods";
|
|
3
|
-
/**
|
|
4
|
-
* @ignore
|
|
5
|
-
*/
|
|
6
|
-
export interface JsonMapperContextProps extends Partial<Omit<JsonMapperCtx, "next">> {
|
|
7
|
-
next: (data: any, options: any) => void;
|
|
8
|
-
options: any;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* @ignore
|
|
12
|
-
*/
|
|
13
|
-
export declare class JsonMapperContext implements JsonMapperCtx {
|
|
14
|
-
readonly collectionType: Type<any> | undefined;
|
|
15
|
-
readonly type: Type<any> | any;
|
|
16
|
-
readonly options: any;
|
|
17
|
-
private _next;
|
|
18
|
-
constructor({ type, collectionType, next, options }: JsonMapperContextProps);
|
|
19
|
-
next(data: any): void;
|
|
20
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Type } from "@tsed/core";
|
|
2
|
-
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
3
|
-
/**
|
|
4
|
-
* @ignore
|
|
5
|
-
*/
|
|
6
|
-
export declare function registerJsonTypeMapper(type: Type<any>, token: Type<JsonMapperMethods>): void;
|
|
7
|
-
/**
|
|
8
|
-
* @ignore
|
|
9
|
-
*/
|
|
10
|
-
export declare function getJsonMapperTypes(): WeakMap<Type<any> | Symbol, JsonMapperMethods>;
|
package/lib/cjs/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from "./components";
|
|
2
|
-
export * from "./decorators/jsonMapper";
|
|
3
|
-
export * from "./decorators/onDeserialize";
|
|
4
|
-
export * from "./decorators/onSerialize";
|
|
5
|
-
export * from "./decorators/afterDeserialize";
|
|
6
|
-
export * from "./decorators/beforeDeserialize";
|
|
7
|
-
export * from "./domain/JsonMapperContext";
|
|
8
|
-
export * from "./domain/JsonMapperTypesContainer";
|
|
9
|
-
export * from "./interfaces/JsonMapperMethods";
|
|
10
|
-
export * from "./utils/deserialize";
|
|
11
|
-
export * from "./utils/serialize";
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Type } from "@tsed/core";
|
|
2
|
-
export interface JsonMapperCtx<T = any, C = any> {
|
|
3
|
-
collectionType: Type<C> | undefined;
|
|
4
|
-
type: Type<T> | T;
|
|
5
|
-
next: JsonMapperNext;
|
|
6
|
-
}
|
|
7
|
-
export interface JsonMapperNext {
|
|
8
|
-
(obj: any): any;
|
|
9
|
-
}
|
|
10
|
-
export interface JsonMapperMethods {
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @param obj
|
|
14
|
-
* @param {JsonMapperNext} ctx
|
|
15
|
-
* @returns {any}
|
|
16
|
-
*/
|
|
17
|
-
deserialize<T = any, C = any>(obj: any, ctx: JsonMapperCtx<T, C>): any;
|
|
18
|
-
/**
|
|
19
|
-
*
|
|
20
|
-
* @param obj
|
|
21
|
-
* @param ctx
|
|
22
|
-
* @returns {any}
|
|
23
|
-
*/
|
|
24
|
-
serialize(obj: any, ctx: JsonMapperCtx<any>): any;
|
|
25
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { MetadataTypes, Type } from "@tsed/core";
|
|
2
|
-
import { JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import "../components";
|
|
4
|
-
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
5
|
-
export interface JsonDeserializerOptions<T = any, C = any> extends MetadataTypes<T, C> {
|
|
6
|
-
/**
|
|
7
|
-
* Types used to map complex types (Symbol, Array, Set, Map)
|
|
8
|
-
*/
|
|
9
|
-
types?: WeakMap<Type<any>, JsonMapperMethods>;
|
|
10
|
-
/**
|
|
11
|
-
* useAlias mapping
|
|
12
|
-
*/
|
|
13
|
-
useAlias?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Accept additionalProperties or ignore it
|
|
16
|
-
*/
|
|
17
|
-
additionalProperties?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Use the store which have all metadata to deserialize correctly the model. This
|
|
20
|
-
* property is useful when you deal with metadata parameters.
|
|
21
|
-
*/
|
|
22
|
-
store?: JsonEntityStore;
|
|
23
|
-
/**
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
groups?: string[] | false;
|
|
27
|
-
[key: string]: any;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Transform given plain object to class.
|
|
31
|
-
* @param src
|
|
32
|
-
* @param options
|
|
33
|
-
*/
|
|
34
|
-
export declare function plainObjectToClass<T = any>(src: any, options: JsonDeserializerOptions): T;
|
|
35
|
-
/**
|
|
36
|
-
* Transform given source to class base on the given `options.type`.
|
|
37
|
-
*
|
|
38
|
-
* @param src
|
|
39
|
-
* @param options
|
|
40
|
-
*/
|
|
41
|
-
export declare function deserialize<T = any>(src: any, options?: JsonDeserializerOptions): T;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { MetadataTypes, Type } from "@tsed/core";
|
|
2
|
-
import "../components";
|
|
3
|
-
import { JsonMapperMethods } from "../interfaces/JsonMapperMethods";
|
|
4
|
-
export interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {
|
|
5
|
-
/**
|
|
6
|
-
* Types used to map complex types (Symbol, Array, Set, Map)
|
|
7
|
-
*/
|
|
8
|
-
types?: Map<Type<any>, JsonMapperMethods>;
|
|
9
|
-
/**
|
|
10
|
-
* useAlias mapping
|
|
11
|
-
*/
|
|
12
|
-
useAlias?: boolean;
|
|
13
|
-
[key: string]: any;
|
|
14
|
-
}
|
|
15
|
-
export declare function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>): {};
|
|
16
|
-
export declare function serialize(obj: any, { type, collectionType, groups, ...options }?: JsonSerializerOptions): any;
|