@tsed/json-mapper 6.88.1 → 6.92.0
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/{components → cjs/components}/ArrayMapper.d.ts +0 -0
- package/lib/cjs/components/ArrayMapper.js +23 -0
- package/lib/cjs/components/ArrayMapper.js.map +1 -0
- package/lib/{components → cjs/components}/DateMapper.d.ts +0 -0
- package/lib/cjs/components/DateMapper.js +30 -0
- package/lib/cjs/components/DateMapper.js.map +1 -0
- package/lib/{components → cjs/components}/MapMapper.d.ts +0 -0
- package/lib/cjs/components/MapMapper.js +30 -0
- package/lib/cjs/components/MapMapper.js.map +1 -0
- package/lib/{components → cjs/components}/PrimitiveMapper.d.ts +0 -0
- package/lib/cjs/components/PrimitiveMapper.js +45 -0
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SetMapper.d.ts +0 -0
- package/lib/cjs/components/SetMapper.js +30 -0
- package/lib/cjs/components/SetMapper.js.map +1 -0
- package/lib/{components → cjs/components}/SymbolMapper.d.ts +0 -0
- package/lib/cjs/components/SymbolMapper.js +24 -0
- package/lib/cjs/components/SymbolMapper.js.map +1 -0
- package/lib/{components → cjs/components}/index.d.ts +0 -0
- package/lib/cjs/components/index.js +10 -0
- package/lib/cjs/components/index.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/afterDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/afterDeserialize.js +39 -0
- package/lib/cjs/decorators/afterDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/beforeDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/beforeDeserialize.js +39 -0
- package/lib/cjs/decorators/beforeDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/jsonMapper.d.ts +0 -0
- package/lib/cjs/decorators/jsonMapper.js +18 -0
- package/lib/cjs/decorators/jsonMapper.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onDeserialize.d.ts +0 -0
- package/lib/cjs/decorators/onDeserialize.js +29 -0
- package/lib/cjs/decorators/onDeserialize.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/onSerialize.d.ts +0 -0
- package/lib/cjs/decorators/onSerialize.js +29 -0
- package/lib/cjs/decorators/onSerialize.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperContext.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperContext.js +23 -0
- package/lib/cjs/domain/JsonMapperContext.js.map +1 -0
- package/lib/{domain → cjs/domain}/JsonMapperTypesContainer.d.ts +0 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js +23 -0
- package/lib/cjs/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterAfterDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js +9 -0
- package/lib/cjs/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/{hooks → cjs/hooks}/alterBeforeDeserialize.d.ts +0 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js +9 -0
- package/lib/cjs/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{interfaces → cjs/interfaces}/JsonMapperMethods.d.ts +0 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js +3 -0
- package/lib/cjs/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/{utils → cjs/utils}/deserialize.d.ts +0 -0
- package/lib/cjs/utils/deserialize.js +140 -0
- package/lib/cjs/utils/deserialize.js.map +1 -0
- package/lib/{utils → cjs/utils}/serialize.d.ts +0 -0
- package/lib/cjs/utils/serialize.js +112 -0
- package/lib/cjs/utils/serialize.js.map +1 -0
- package/lib/esm/components/ArrayMapper.js +20 -0
- package/lib/esm/components/ArrayMapper.js.map +1 -0
- package/lib/esm/components/DateMapper.js +27 -0
- package/lib/esm/components/DateMapper.js.map +1 -0
- package/lib/esm/components/MapMapper.js +27 -0
- package/lib/esm/components/MapMapper.js.map +1 -0
- package/lib/esm/components/PrimitiveMapper.js +42 -0
- package/lib/esm/components/PrimitiveMapper.js.map +1 -0
- package/lib/esm/components/SetMapper.js +27 -0
- package/lib/esm/components/SetMapper.js.map +1 -0
- package/lib/esm/components/SymbolMapper.js +21 -0
- package/lib/esm/components/SymbolMapper.js.map +1 -0
- package/lib/esm/components/index.js +7 -0
- package/lib/esm/components/index.js.map +1 -0
- package/lib/esm/decorators/afterDeserialize.js +35 -0
- package/lib/esm/decorators/afterDeserialize.js.map +1 -0
- package/lib/esm/decorators/beforeDeserialize.js +35 -0
- package/lib/esm/decorators/beforeDeserialize.js.map +1 -0
- package/lib/esm/decorators/jsonMapper.js +14 -0
- package/lib/esm/decorators/jsonMapper.js.map +1 -0
- package/lib/esm/decorators/onDeserialize.js +25 -0
- package/lib/esm/decorators/onDeserialize.js.map +1 -0
- package/lib/esm/decorators/onSerialize.js +25 -0
- package/lib/esm/decorators/onSerialize.js.map +1 -0
- package/lib/esm/domain/JsonMapperContext.js +19 -0
- package/lib/esm/domain/JsonMapperContext.js.map +1 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js +18 -0
- package/lib/esm/domain/JsonMapperTypesContainer.js.map +1 -0
- package/lib/esm/hooks/alterAfterDeserialize.js +5 -0
- package/lib/esm/hooks/alterAfterDeserialize.js.map +1 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js +5 -0
- package/lib/esm/hooks/alterBeforeDeserialize.js.map +1 -0
- package/lib/esm/index.js +12 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/JsonMapperMethods.js +2 -0
- package/lib/esm/interfaces/JsonMapperMethods.js.map +1 -0
- package/lib/esm/utils/deserialize.js +135 -0
- package/lib/esm/utils/deserialize.js.map +1 -0
- package/lib/esm/utils/serialize.js +107 -0
- package/lib/esm/utils/serialize.js.map +1 -0
- package/lib/types/components/ArrayMapper.d.ts +10 -0
- package/lib/types/components/DateMapper.d.ts +11 -0
- package/lib/types/components/MapMapper.d.ts +12 -0
- package/lib/types/components/PrimitiveMapper.d.ts +10 -0
- package/lib/types/components/SetMapper.d.ts +10 -0
- package/lib/types/components/SymbolMapper.d.ts +11 -0
- package/lib/types/components/index.d.ts +6 -0
- package/lib/types/decorators/afterDeserialize.d.ts +33 -0
- package/lib/types/decorators/beforeDeserialize.d.ts +33 -0
- package/lib/types/decorators/jsonMapper.d.ts +7 -0
- package/lib/types/decorators/onDeserialize.d.ts +23 -0
- package/lib/types/decorators/onSerialize.d.ts +23 -0
- package/lib/types/domain/JsonMapperContext.d.ts +20 -0
- package/lib/types/domain/JsonMapperTypesContainer.d.ts +10 -0
- package/lib/types/hooks/alterAfterDeserialize.d.ts +4 -0
- package/lib/types/hooks/alterBeforeDeserialize.d.ts +4 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/interfaces/JsonMapperMethods.d.ts +25 -0
- package/lib/types/utils/deserialize.d.ts +41 -0
- package/lib/types/utils/serialize.d.ts +16 -0
- package/package.json +18 -14
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index.js +0 -719
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -708
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { JsonEntityFn } from "@tsed/schema";
|
|
2
|
+
/**
|
|
3
|
+
* Call the function before property deserialization.
|
|
4
|
+
*
|
|
5
|
+
* ## Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* class Model {
|
|
9
|
+
* @OnDeserialize(v => v + 1)
|
|
10
|
+
* property: string;
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @returns {Function}
|
|
15
|
+
* @decorator
|
|
16
|
+
* @schema
|
|
17
|
+
* @property
|
|
18
|
+
* @param cb
|
|
19
|
+
*/
|
|
20
|
+
export function OnDeserialize(cb) {
|
|
21
|
+
return JsonEntityFn((storedJson) => {
|
|
22
|
+
storedJson.schema.$hooks.on("onDeserialize", cb);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=onDeserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onDeserialize.js","sourceRoot":"","sources":["../../../src/decorators/onDeserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,YAAY,EAAC,MAAM,cAAc,CAAC;AAM3D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,aAAa,CAAC,EAAyB;IACrD,OAAO,YAAY,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {JsonHookContext, JsonEntityFn} from \"@tsed/schema\";\n\nexport interface OnDeserializeCallback {\n (value: any, ctx: JsonHookContext): any;\n}\n\n/**\n * Call the function before property deserialization.\n *\n * ## Example\n *\n * ```typescript\n * class Model {\n * @OnDeserialize(v => v + 1)\n * property: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @schema\n * @property\n * @param cb\n */\nexport function OnDeserialize(cb: OnDeserializeCallback): PropertyDecorator {\n return JsonEntityFn((storedJson) => {\n storedJson.schema.$hooks.on(\"onDeserialize\", cb);\n });\n}\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { JsonEntityFn } from "@tsed/schema";
|
|
2
|
+
/**
|
|
3
|
+
* Call the function before property serialization.
|
|
4
|
+
*
|
|
5
|
+
* ## Example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* class Model {
|
|
9
|
+
* @OnSerialize(v => v + 1)
|
|
10
|
+
* property: string;
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @returns {Function}
|
|
15
|
+
* @decorator
|
|
16
|
+
* @schema
|
|
17
|
+
* @property
|
|
18
|
+
* @param cb
|
|
19
|
+
*/
|
|
20
|
+
export function OnSerialize(cb) {
|
|
21
|
+
return JsonEntityFn((storedJson) => {
|
|
22
|
+
storedJson.schema.$hooks.on("onSerialize", cb);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=onSerialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onSerialize.js","sourceRoot":"","sources":["../../../src/decorators/onSerialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAkB,MAAM,cAAc,CAAC;AAM3D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,WAAW,CAAC,EAAuB;IACjD,OAAO,YAAY,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {JsonEntityFn, JsonHookContext} from \"@tsed/schema\";\n\nexport interface OnSerializeCallback {\n (value: any, ctx: JsonHookContext): any;\n}\n\n/**\n * Call the function before property serialization.\n *\n * ## Example\n *\n * ```typescript\n * class Model {\n * @OnSerialize(v => v + 1)\n * property: string;\n * }\n * ```\n *\n * @returns {Function}\n * @decorator\n * @schema\n * @property\n * @param cb\n */\nexport function OnSerialize(cb: OnSerializeCallback): PropertyDecorator {\n return JsonEntityFn((storedJson) => {\n storedJson.schema.$hooks.on(\"onSerialize\", cb);\n });\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isCollection } from "@tsed/core";
|
|
2
|
+
/**
|
|
3
|
+
* @ignore
|
|
4
|
+
*/
|
|
5
|
+
export class JsonMapperContext {
|
|
6
|
+
constructor({ type, collectionType, next, options }) {
|
|
7
|
+
this.type = type;
|
|
8
|
+
this.collectionType = isCollection(collectionType) ? collectionType : undefined;
|
|
9
|
+
this._next = next;
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
next(data) {
|
|
13
|
+
return this._next(data, {
|
|
14
|
+
...this.options,
|
|
15
|
+
type: this.type
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=JsonMapperContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonMapperContext.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAO,MAAM,YAAY,CAAC;AAS9C;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAM5B,YAAY,EAAC,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,OAAO,EAAyB;QACvE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,cAAc,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC;QAChF,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,IAAI,CAAC,IAAS;QACZ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACtB,GAAG,IAAI,CAAC,OAAO;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {isCollection, Type} from \"@tsed/core\";\nimport {JsonMapperCtx} from \"../interfaces/JsonMapperMethods\";\n/**\n * @ignore\n */\nexport interface JsonMapperContextProps extends Partial<Omit<JsonMapperCtx, \"next\">> {\n next: (data: any, options: any) => void;\n options: any;\n}\n/**\n * @ignore\n */\nexport class JsonMapperContext implements JsonMapperCtx {\n readonly collectionType: Type<any> | undefined;\n readonly type: Type<any> | any;\n readonly options: any;\n private _next: (data: any, options: any) => void;\n\n constructor({type, collectionType, next, options}: JsonMapperContextProps) {\n this.type = type;\n this.collectionType = isCollection(collectionType) ? collectionType : undefined;\n this._next = next;\n this.options = options;\n }\n\n next(data: any) {\n return this._next(data, {\n ...this.options,\n type: this.type\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ignore
|
|
3
|
+
*/
|
|
4
|
+
// tslint:disable-next-line:variable-name
|
|
5
|
+
const JsonMapperTypesContainer = new Map();
|
|
6
|
+
/**
|
|
7
|
+
* @ignore
|
|
8
|
+
*/
|
|
9
|
+
export function registerJsonTypeMapper(type, token) {
|
|
10
|
+
JsonMapperTypesContainer.set(type, new token());
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @ignore
|
|
14
|
+
*/
|
|
15
|
+
export function getJsonMapperTypes() {
|
|
16
|
+
return JsonMapperTypesContainer;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=JsonMapperTypesContainer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonMapperTypesContainer.js","sourceRoot":"","sources":["../../../src/domain/JsonMapperTypesContainer.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,yCAAyC;AACzC,MAAM,wBAAwB,GAA+C,IAAI,GAAG,EAAE,CAAC;AACvF;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAe,EAAE,KAA8B;IACpF,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, JsonMapperMethods> = new Map();\n/**\n * @ignore\n */\nexport function registerJsonTypeMapper(type: Type<any>, token: Type<JsonMapperMethods>) {\n JsonMapperTypesContainer.set(type, new token());\n}\n/**\n * @ignore\n */\nexport function getJsonMapperTypes(): WeakMap<Type<any> | Symbol, JsonMapperMethods> {\n return JsonMapperTypesContainer;\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function alterAfterDeserialize(data, schema, options) {
|
|
2
|
+
var _a;
|
|
3
|
+
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("afterDeserialize", data, [options]);
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=alterAfterDeserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alterAfterDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterAfterDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,qBAAqB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;;IACpF,OAAO,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,0CAAE,KAAK,CAAC,kBAAkB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterAfterDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"afterDeserialize\", data, [options]);\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function alterBeforeDeserialize(data, schema, options) {
|
|
2
|
+
var _a;
|
|
3
|
+
return (_a = schema === null || schema === void 0 ? void 0 : schema.$hooks) === null || _a === void 0 ? void 0 : _a.alter("beforeDeserialize", data, [options]);
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=alterBeforeDeserialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alterBeforeDeserialize.js","sourceRoot":"","sources":["../../../src/hooks/alterBeforeDeserialize.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,sBAAsB,CAAC,IAAS,EAAE,MAAuB,EAAE,OAAY;;IACrF,OAAO,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,0CAAE,KAAK,CAAC,mBAAmB,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AACrE,CAAC","sourcesContent":["import {Hooks} from \"@tsed/core\";\n\nexport function alterBeforeDeserialize(data: any, schema: {$hooks: Hooks}, options: any) {\n return schema?.$hooks?.alter(\"beforeDeserialize\", data, [options]);\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mCAAmC,CAAC;AAClD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC","sourcesContent":["export * from \"./components\";\nexport * from \"./decorators/jsonMapper\";\nexport * from \"./decorators/onDeserialize\";\nexport * from \"./decorators/onSerialize\";\nexport * from \"./decorators/afterDeserialize\";\nexport * from \"./decorators/beforeDeserialize\";\nexport * from \"./domain/JsonMapperContext\";\nexport * from \"./domain/JsonMapperTypesContainer\";\nexport * from \"./interfaces/JsonMapperMethods\";\nexport * from \"./utils/deserialize\";\nexport * from \"./utils/serialize\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonMapperMethods.js","sourceRoot":"","sources":["../../../src/interfaces/JsonMapperMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\n\nexport interface JsonMapperCtx<T = any, C = any> {\n collectionType: Type<C> | undefined;\n type: Type<T> | T;\n next: JsonMapperNext;\n}\n\nexport interface JsonMapperNext {\n (obj: any): any;\n}\n\nexport interface JsonMapperMethods {\n /**\n *\n * @param obj\n * @param {JsonMapperNext} ctx\n * @returns {any}\n */\n deserialize<T = any, C = any>(obj: any, ctx: JsonMapperCtx<T, C>): any;\n\n /**\n *\n * @param obj\n * @param ctx\n * @returns {any}\n */\n serialize(obj: any, ctx: JsonMapperCtx<any>): any;\n}\n"]}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { isArray, isEmpty, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
|
+
import { alterIgnore, getProperties, JsonEntityStore, JsonSchema } from "@tsed/schema";
|
|
3
|
+
import "../components";
|
|
4
|
+
import { JsonMapperContext } from "../domain/JsonMapperContext";
|
|
5
|
+
import { getJsonMapperTypes } from "../domain/JsonMapperTypesContainer";
|
|
6
|
+
import { alterAfterDeserialize } from "../hooks/alterAfterDeserialize";
|
|
7
|
+
import { alterBeforeDeserialize } from "../hooks/alterBeforeDeserialize";
|
|
8
|
+
function isDeserializable(obj, options) {
|
|
9
|
+
if ((!!options.collectionType && isNil(obj)) || obj === undefined) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return !(isEmpty(options.type) || (options.type === Object && !options.collectionType));
|
|
13
|
+
}
|
|
14
|
+
function alterValue(schema, value, options) {
|
|
15
|
+
return schema.$hooks.alter("onDeserialize", value, [options]);
|
|
16
|
+
}
|
|
17
|
+
function transformCollection(src, options) {
|
|
18
|
+
var _a;
|
|
19
|
+
const { types, type = Object, collectionType } = options;
|
|
20
|
+
const context = new JsonMapperContext({
|
|
21
|
+
type,
|
|
22
|
+
collectionType,
|
|
23
|
+
options,
|
|
24
|
+
next: (data, { collectionType, ...options }) => deserialize(data, options)
|
|
25
|
+
});
|
|
26
|
+
return (_a = types === null || types === void 0 ? void 0 : types.get(options.collectionType)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
27
|
+
}
|
|
28
|
+
function transformType(src, options) {
|
|
29
|
+
var _a;
|
|
30
|
+
const { types, type = Object } = options;
|
|
31
|
+
const context = new JsonMapperContext({
|
|
32
|
+
type,
|
|
33
|
+
options,
|
|
34
|
+
next: (data, { type, ...options }) => deserialize(data, options)
|
|
35
|
+
});
|
|
36
|
+
return (_a = types === null || types === void 0 ? void 0 : types.get(type)) === null || _a === void 0 ? void 0 : _a.deserialize(src, context);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Transform given plain object to class.
|
|
40
|
+
* @param src
|
|
41
|
+
* @param options
|
|
42
|
+
*/
|
|
43
|
+
export function plainObjectToClass(src, options) {
|
|
44
|
+
if (isEmpty(src)) {
|
|
45
|
+
return src;
|
|
46
|
+
}
|
|
47
|
+
const { type, store = JsonEntityStore.from(type), ...next } = options;
|
|
48
|
+
const propertiesMap = getProperties(store, { ...options, withIgnoredProps: true });
|
|
49
|
+
let keys = objectKeys(src);
|
|
50
|
+
const additionalProperties = propertiesMap.size ? !!store.schema.get("additionalProperties") || options.additionalProperties : true;
|
|
51
|
+
src = alterBeforeDeserialize(src, store.schema, options);
|
|
52
|
+
const out = new type(src);
|
|
53
|
+
propertiesMap.forEach((propStore) => {
|
|
54
|
+
const key = options.useAlias
|
|
55
|
+
? propStore.parent.schema.getAliasOf(propStore.propertyName) || propStore.propertyName
|
|
56
|
+
: propStore.propertyName;
|
|
57
|
+
keys = keys.filter((k) => k !== key);
|
|
58
|
+
next.type = propStore.computedType;
|
|
59
|
+
if (alterIgnore(propStore.itemSchema, options)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let value = alterValue(propStore.schema, src[key], { ...options, self: src });
|
|
63
|
+
if (propStore.schema.hasGenerics) {
|
|
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
|
+
}
|
|
75
|
+
value = deserialize(value, {
|
|
76
|
+
...next,
|
|
77
|
+
type: value === src[key] ? next.type : undefined,
|
|
78
|
+
collectionType: propStore.collectionType
|
|
79
|
+
});
|
|
80
|
+
if (value !== undefined) {
|
|
81
|
+
out[propStore.propertyName] = value;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
if (additionalProperties) {
|
|
85
|
+
keys.forEach((key) => {
|
|
86
|
+
out[key] = src[key];
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return alterAfterDeserialize(out, store.schema, options);
|
|
90
|
+
}
|
|
91
|
+
function buildOptions(options) {
|
|
92
|
+
if (options.store instanceof JsonEntityStore) {
|
|
93
|
+
if (options.store.parameter && options.store.parameter.nestedGenerics.length) {
|
|
94
|
+
options.nestedGenerics = options.store.parameter.nestedGenerics;
|
|
95
|
+
}
|
|
96
|
+
options.type = options.store.computedType;
|
|
97
|
+
options.collectionType = options.store.collectionType;
|
|
98
|
+
delete options.store;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
groups: false,
|
|
102
|
+
useAlias: true,
|
|
103
|
+
...options,
|
|
104
|
+
type: options.type ? options.type : undefined,
|
|
105
|
+
types: options.types ? options.types : getJsonMapperTypes()
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Transform given source to class base on the given `options.type`.
|
|
110
|
+
*
|
|
111
|
+
* @param src
|
|
112
|
+
* @param options
|
|
113
|
+
*/
|
|
114
|
+
export function deserialize(src, options = {}) {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
options = buildOptions(options);
|
|
117
|
+
if (!isDeserializable(src, options)) {
|
|
118
|
+
return src;
|
|
119
|
+
}
|
|
120
|
+
if (!options.collectionType && isArray(src)) {
|
|
121
|
+
options.collectionType = Array;
|
|
122
|
+
}
|
|
123
|
+
if (options.collectionType) {
|
|
124
|
+
if (!((_a = options.types) === null || _a === void 0 ? void 0 : _a.has(options.collectionType))) {
|
|
125
|
+
throw new Error(`${nameOf(options.collectionType)} is not supported by JsonMapper.`);
|
|
126
|
+
}
|
|
127
|
+
return transformCollection(src, options);
|
|
128
|
+
}
|
|
129
|
+
if ((_b = options.types) === null || _b === void 0 ? void 0 : _b.has(options.type)) {
|
|
130
|
+
return transformType(src, options);
|
|
131
|
+
}
|
|
132
|
+
// class converter
|
|
133
|
+
return plainObjectToClass(src, options);
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=deserialize.js.map
|
|
@@ -0,0 +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,eA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AI;QACd,GAAG,OAAO;QACV,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS;QAC7C,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,kBAAkB,EAAE;KAC5D,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAU,GAAQ,EAAE,UAAmC,EAAE;;IAClF,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAEhC,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE;QACnC,OAAO,GAAG,CAAC;KACZ;IAED,IAAI,CAAC,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAC3C,OAAO,CAAC,cAAc,GAAG,KAAK,CAAC;KAChC;IAED,IAAI,OAAO,CAAC,cAAc,EAAE;QAC1B,IAAI,CAAC,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"]}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { classOf, isArray, isClassObject, isCollection, isEmpty, isFunction, isNil, objectKeys } from "@tsed/core";
|
|
2
|
+
import { alterIgnore, getPropertiesStores, JsonEntityStore } from "@tsed/schema";
|
|
3
|
+
import "../components";
|
|
4
|
+
import { JsonMapperContext } from "../domain/JsonMapperContext";
|
|
5
|
+
import { getJsonMapperTypes } from "../domain/JsonMapperTypesContainer";
|
|
6
|
+
function alterValue(schema, value, options) {
|
|
7
|
+
return schema.$hooks.alter("onSerialize", value, [options]);
|
|
8
|
+
}
|
|
9
|
+
function getSchemaProperties(storedJson, obj) {
|
|
10
|
+
const stores = Array.from(getPropertiesStores(storedJson).entries());
|
|
11
|
+
if (!stores.length) {
|
|
12
|
+
// fallback to auto discovering field from obj
|
|
13
|
+
objectKeys(obj).forEach((key) => {
|
|
14
|
+
const propStore = JsonEntityStore.from(storedJson.target, key);
|
|
15
|
+
stores.push([key, propStore]);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return stores;
|
|
19
|
+
}
|
|
20
|
+
function getObjectProperties(obj) {
|
|
21
|
+
return Object.entries(obj).filter(([, value]) => !isFunction(value));
|
|
22
|
+
}
|
|
23
|
+
function getType(propStore, value) {
|
|
24
|
+
if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
return propStore.type;
|
|
28
|
+
}
|
|
29
|
+
export function classToPlainObject(obj, options) {
|
|
30
|
+
const { useAlias = true, type, ...props } = options;
|
|
31
|
+
const entity = JsonEntityStore.from(type || obj);
|
|
32
|
+
return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {
|
|
33
|
+
const schema = propStore.schema;
|
|
34
|
+
if (alterIgnore(schema, { useAlias, ...props, self: obj })) {
|
|
35
|
+
return newObj;
|
|
36
|
+
}
|
|
37
|
+
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
38
|
+
value = serialize(value, {
|
|
39
|
+
useAlias,
|
|
40
|
+
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
41
|
+
collectionType: propStore.collectionType,
|
|
42
|
+
...props
|
|
43
|
+
});
|
|
44
|
+
if (value === undefined) {
|
|
45
|
+
return newObj;
|
|
46
|
+
}
|
|
47
|
+
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
48
|
+
return {
|
|
49
|
+
...newObj,
|
|
50
|
+
[key]: value
|
|
51
|
+
};
|
|
52
|
+
}, {});
|
|
53
|
+
}
|
|
54
|
+
function toObject(obj, options) {
|
|
55
|
+
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
56
|
+
...newObj,
|
|
57
|
+
[key]: serialize(value, options)
|
|
58
|
+
}), {});
|
|
59
|
+
}
|
|
60
|
+
function getBestType(type, obj) {
|
|
61
|
+
const dataType = classOf(obj);
|
|
62
|
+
if (dataType && !isClassObject(dataType)) {
|
|
63
|
+
return dataType;
|
|
64
|
+
}
|
|
65
|
+
return type || Object;
|
|
66
|
+
}
|
|
67
|
+
export function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
68
|
+
var _a;
|
|
69
|
+
if (isEmpty(obj)) {
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
const types = options.types ? options.types : getJsonMapperTypes();
|
|
73
|
+
options.groups = groups;
|
|
74
|
+
// FIX custom serialization function from @tsed/mongoose and bson
|
|
75
|
+
if ((typeof obj.toJSON === "function" && obj.$isMongooseModelPrototype) || obj._bsontype) {
|
|
76
|
+
return obj.toJSON(options);
|
|
77
|
+
}
|
|
78
|
+
if (typeof obj.toJSON === "function") {
|
|
79
|
+
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
80
|
+
}
|
|
81
|
+
if (!isCollection(obj)) {
|
|
82
|
+
options.type = type = getBestType(type, obj);
|
|
83
|
+
}
|
|
84
|
+
else if (!options.collectionType) {
|
|
85
|
+
type = classOf(obj);
|
|
86
|
+
options.collectionType = type;
|
|
87
|
+
}
|
|
88
|
+
const context = new JsonMapperContext({
|
|
89
|
+
type,
|
|
90
|
+
options,
|
|
91
|
+
next: (data) => serialize(data, {
|
|
92
|
+
...options,
|
|
93
|
+
collectionType: undefined,
|
|
94
|
+
type: options.type
|
|
95
|
+
})
|
|
96
|
+
});
|
|
97
|
+
if (types.has(type)) {
|
|
98
|
+
const jsonMapper = types.get(type);
|
|
99
|
+
return jsonMapper.serialize(obj, context);
|
|
100
|
+
}
|
|
101
|
+
if (isArray(obj)) {
|
|
102
|
+
// Serialize Array class like
|
|
103
|
+
return (_a = types.get(Array)) === null || _a === void 0 ? void 0 : _a.serialize(obj, context);
|
|
104
|
+
}
|
|
105
|
+
return !isClassObject(type) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=serialize.js.map
|
|
@@ -0,0 +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;AAC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ypes, 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 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 = type = getBestType(type, obj);\n } else if (!options.collectionType) {\n type = classOf(obj);\n options.collectionType = type;\n }\n\n const context = new JsonMapperContext({\n type,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n if (types.has(type)) {\n const jsonMapper = types.get(type)!;\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(type) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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;
|
|
@@ -0,0 +1,33 @@
|
|
|
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;
|
|
@@ -0,0 +1,23 @@
|
|
|
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;
|