@tsed/json-mapper 7.35.0 → 7.36.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +3 -3
- package/lib/browser/json-mapper.umd.min.js +1 -1
- package/lib/browser/json-mapper.umd.min.js.map +1 -1
- package/lib/cjs/components/DateMapper.js +1 -1
- package/lib/cjs/components/DateMapper.js.map +1 -1
- package/lib/cjs/components/PrimitiveMapper.js +30 -26
- package/lib/cjs/components/PrimitiveMapper.js.map +1 -1
- package/lib/cjs/components/SymbolMapper.js.map +1 -1
- package/lib/cjs/domain/JsonDeserializer.js +275 -0
- package/lib/cjs/domain/JsonDeserializer.js.map +1 -0
- package/lib/cjs/domain/JsonDeserializerOptions.js +3 -0
- package/lib/cjs/domain/JsonDeserializerOptions.js.map +1 -0
- package/lib/cjs/domain/JsonMapperCompiler.js +167 -0
- package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/cjs/domain/JsonSerializer.js +219 -0
- package/lib/cjs/domain/JsonSerializer.js.map +1 -0
- package/lib/cjs/domain/JsonSerializerOptions.js +3 -0
- package/lib/cjs/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/cjs/domain/Writer.js +141 -0
- package/lib/cjs/domain/Writer.js.map +1 -0
- package/lib/cjs/hooks/alterOnDeserialize.js +8 -0
- package/lib/cjs/hooks/alterOnDeserialize.js.map +1 -0
- package/lib/cjs/hooks/alterOnSerialize.js +8 -0
- package/lib/cjs/hooks/alterOnSerialize.js.map +1 -0
- package/lib/cjs/index.js +9 -4
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/utils/deserialize.js +5 -202
- package/lib/cjs/utils/deserialize.js.map +1 -1
- package/lib/cjs/utils/getObjectProperties.js +9 -0
- package/lib/cjs/utils/getObjectProperties.js.map +1 -0
- package/lib/cjs/utils/serialize.js +5 -143
- package/lib/cjs/utils/serialize.js.map +1 -1
- package/lib/esm/components/DateMapper.js +1 -1
- package/lib/esm/components/DateMapper.js.map +1 -1
- package/lib/esm/components/PrimitiveMapper.js +30 -26
- package/lib/esm/components/PrimitiveMapper.js.map +1 -1
- package/lib/esm/components/SymbolMapper.js.map +1 -1
- package/lib/esm/domain/JsonDeserializer.js +271 -0
- package/lib/esm/domain/JsonDeserializer.js.map +1 -0
- package/lib/esm/domain/JsonDeserializerOptions.js +2 -0
- package/lib/esm/domain/JsonDeserializerOptions.js.map +1 -0
- package/lib/esm/domain/JsonMapperCompiler.js +163 -0
- package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
- package/lib/esm/domain/JsonSerializer.js +215 -0
- package/lib/esm/domain/JsonSerializer.js.map +1 -0
- package/lib/esm/domain/JsonSerializerOptions.js +2 -0
- package/lib/esm/domain/JsonSerializerOptions.js.map +1 -0
- package/lib/esm/domain/Writer.js +137 -0
- package/lib/esm/domain/Writer.js.map +1 -0
- package/lib/esm/hooks/alterOnDeserialize.js +4 -0
- package/lib/esm/hooks/alterOnDeserialize.js.map +1 -0
- package/lib/esm/hooks/alterOnSerialize.js +4 -0
- package/lib/esm/hooks/alterOnSerialize.js.map +1 -0
- package/lib/esm/index.js +9 -4
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils/deserialize.js +4 -200
- package/lib/esm/utils/deserialize.js.map +1 -1
- package/lib/esm/utils/getObjectProperties.js +5 -0
- package/lib/esm/utils/getObjectProperties.js.map +1 -0
- package/lib/esm/utils/serialize.js +4 -141
- package/lib/esm/utils/serialize.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/components/PrimitiveMapper.d.ts +4 -0
- package/lib/types/domain/JsonDeserializer.d.ts +33 -0
- package/lib/types/domain/JsonDeserializerOptions.d.ts +31 -0
- package/lib/types/domain/JsonMapperCompiler.d.ts +58 -0
- package/lib/types/domain/JsonSerializer.d.ts +21 -0
- package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
- package/lib/types/domain/Writer.d.ts +43 -0
- package/lib/types/hooks/alterOnDeserialize.d.ts +2 -0
- package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
- package/lib/types/index.d.ts +9 -4
- package/lib/types/utils/deserialize.d.ts +2 -47
- package/lib/types/utils/getObjectProperties.d.ts +1 -0
- package/lib/types/utils/serialize.d.ts +2 -18
- package/package.json +11 -8
- package/lib/cjs/components/ArrayMapper.js +0 -23
- package/lib/cjs/components/ArrayMapper.js.map +0 -1
- package/lib/cjs/components/MapMapper.js +0 -30
- package/lib/cjs/components/MapMapper.js.map +0 -1
- package/lib/cjs/components/SetMapper.js +0 -30
- package/lib/cjs/components/SetMapper.js.map +0 -1
- package/lib/cjs/domain/JsonMapperContext.js +0 -27
- package/lib/cjs/domain/JsonMapperContext.js.map +0 -1
- package/lib/esm/components/ArrayMapper.js +0 -20
- package/lib/esm/components/ArrayMapper.js.map +0 -1
- package/lib/esm/components/MapMapper.js +0 -27
- package/lib/esm/components/MapMapper.js.map +0 -1
- package/lib/esm/components/SetMapper.js +0 -27
- package/lib/esm/components/SetMapper.js.map +0 -1
- package/lib/esm/domain/JsonMapperContext.js +0 -23
- package/lib/esm/domain/JsonMapperContext.js.map +0 -1
- package/lib/types/components/ArrayMapper.d.ts +0 -10
- package/lib/types/components/MapMapper.d.ts +0 -12
- package/lib/types/components/SetMapper.d.ts +0 -10
- package/lib/types/domain/JsonMapperContext.d.ts +0 -20
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"deserialize.js","sourceRoot":"","sources":["../../../src/utils/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,CAAC;AAClC,OAAO,+BAA+B,CAAC;AACvC,OAAO,4BAA4B,CAAC;AAEpC,OAAO,EAAC,gBAAgB,EAAC,MAAM,4BAA4B,CAAC;AAG5D,MAAM,YAAY,GAAG,IAAI,gBAAgB,EAAE,CAAC;AAE5C,MAAM,UAAU,WAAW,CAAc,KAAU,EAAE,OAAiC;IACpF,OAAO,YAAY,CAAC,GAAG,CAAQ,KAAK,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getObjectProperties.js","sourceRoot":"","sources":["../../../src/utils/getObjectProperties.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAEtC,MAAM,UAAU,mBAAmB,CAAC,GAAQ;IAC1C,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;AACvE,CAAC"}
|
|
@@ -1,146 +1,9 @@
|
|
|
1
|
-
import { classOf, isArray, isClassObject, isCollection, isDate, isEmpty, isFunction, isNil, nameOf, objectKeys } from "@tsed/core";
|
|
2
|
-
import { alterIgnore, getPropertiesStores, JsonEntityStore } from "@tsed/schema";
|
|
3
|
-
import "../components/ArrayMapper.js";
|
|
4
1
|
import "../components/DateMapper.js";
|
|
5
|
-
import "../components/MapMapper.js";
|
|
6
2
|
import "../components/PrimitiveMapper.js";
|
|
7
|
-
import "../components/SetMapper.js";
|
|
8
3
|
import "../components/SymbolMapper.js";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
return
|
|
13
|
-
}
|
|
14
|
-
function getSchemaProperties(storedJson, obj) {
|
|
15
|
-
const stores = Array.from(getPropertiesStores(storedJson).entries());
|
|
16
|
-
if (!stores.length) {
|
|
17
|
-
// fallback to auto discovering field from obj
|
|
18
|
-
objectKeys(obj).forEach((key) => {
|
|
19
|
-
const propStore = JsonEntityStore.from(storedJson.target, key);
|
|
20
|
-
stores.push([key, propStore]);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return stores;
|
|
24
|
-
}
|
|
25
|
-
function getObjectProperties(obj) {
|
|
26
|
-
return Object.entries(obj).filter(([, value]) => !isFunction(value));
|
|
27
|
-
}
|
|
28
|
-
function getType(propStore, value) {
|
|
29
|
-
if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
return propStore.type;
|
|
33
|
-
}
|
|
34
|
-
export function classToPlainObject(obj, options) {
|
|
35
|
-
const { useAlias = true, type, ...props } = options;
|
|
36
|
-
const entity = JsonEntityStore.from(type || obj);
|
|
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);
|
|
42
|
-
const schema = propStore.schema;
|
|
43
|
-
if (alterIgnore(schema, { useAlias, ...props, self: obj })) {
|
|
44
|
-
return newObj;
|
|
45
|
-
}
|
|
46
|
-
let value = alterValue(schema, obj[key], { useAlias, ...props, self: obj });
|
|
47
|
-
value = serialize(value, {
|
|
48
|
-
useAlias,
|
|
49
|
-
self: obj,
|
|
50
|
-
type: value === obj[key] ? getType(propStore, value) : undefined,
|
|
51
|
-
collectionType: propStore.collectionType,
|
|
52
|
-
format: propStore.itemSchema.get("format"),
|
|
53
|
-
...props
|
|
54
|
-
});
|
|
55
|
-
if (value === undefined) {
|
|
56
|
-
return newObj;
|
|
57
|
-
}
|
|
58
|
-
key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;
|
|
59
|
-
return {
|
|
60
|
-
...newObj,
|
|
61
|
-
[key]: value
|
|
62
|
-
};
|
|
63
|
-
}, {});
|
|
64
|
-
if (entity.discriminatorAncestor) {
|
|
65
|
-
const discriminator = entity.discriminatorAncestor.schema.discriminator();
|
|
66
|
-
if (!out[discriminator.propertyName]) {
|
|
67
|
-
out[discriminator.propertyName] = discriminator.getDefaultValue(entity.target);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (additionalProperties) {
|
|
71
|
-
objectKeys(obj).forEach((key) => {
|
|
72
|
-
if (!properties.has(key)) {
|
|
73
|
-
out[key] = obj[key];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return out;
|
|
78
|
-
}
|
|
79
|
-
function toObject(obj, options) {
|
|
80
|
-
return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
|
|
81
|
-
...newObj,
|
|
82
|
-
[key]: serialize(value, options)
|
|
83
|
-
}), {});
|
|
84
|
-
}
|
|
85
|
-
function getBestType(type, obj) {
|
|
86
|
-
const dataType = classOf(obj);
|
|
87
|
-
if (dataType && !isClassObject(dataType)) {
|
|
88
|
-
return dataType;
|
|
89
|
-
}
|
|
90
|
-
return type || Object;
|
|
91
|
-
}
|
|
92
|
-
function hasJsonMethod(obj) {
|
|
93
|
-
return typeof obj.toJSON === "function";
|
|
94
|
-
}
|
|
95
|
-
function isMongooseObject(obj) {
|
|
96
|
-
return (hasJsonMethod(obj) && obj.$isMongooseModelPrototype) || obj._bsontype;
|
|
97
|
-
}
|
|
98
|
-
function isMomentObject(obj) {
|
|
99
|
-
return obj && obj?._isAMomentObject;
|
|
100
|
-
}
|
|
101
|
-
export function serialize(obj, { type, collectionType, groups = false, ...options } = {}) {
|
|
102
|
-
if (isEmpty(obj)) {
|
|
103
|
-
return obj;
|
|
104
|
-
}
|
|
105
|
-
let currentType = type;
|
|
106
|
-
const types = options.types ? options.types : getJsonMapperTypes();
|
|
107
|
-
options.groups = groups;
|
|
108
|
-
// FIX custom serialization function from @tsed/mongoose and bson
|
|
109
|
-
if (isMongooseObject(obj)) {
|
|
110
|
-
return obj.toJSON(options);
|
|
111
|
-
}
|
|
112
|
-
if (hasJsonMethod(obj) && !isMomentObject(obj) && !isDate(obj)) {
|
|
113
|
-
return serialize(obj.toJSON(), { ...options, type: classOf(obj) });
|
|
114
|
-
}
|
|
115
|
-
if (!isCollection(obj)) {
|
|
116
|
-
options.type = currentType = getBestType(currentType, obj);
|
|
117
|
-
}
|
|
118
|
-
else if (!options.collectionType) {
|
|
119
|
-
currentType = classOf(obj);
|
|
120
|
-
options.type = type;
|
|
121
|
-
options.collectionType = currentType;
|
|
122
|
-
}
|
|
123
|
-
const context = new JsonMapperContext({
|
|
124
|
-
type: currentType,
|
|
125
|
-
options,
|
|
126
|
-
next: (data) => serialize(data, {
|
|
127
|
-
...options,
|
|
128
|
-
collectionType: undefined,
|
|
129
|
-
type: options.type
|
|
130
|
-
})
|
|
131
|
-
});
|
|
132
|
-
const mapper = types.get(currentType) || types.get(nameOf(currentType));
|
|
133
|
-
if (mapper) {
|
|
134
|
-
const jsonMapper = mapper;
|
|
135
|
-
return jsonMapper.serialize(obj, context);
|
|
136
|
-
}
|
|
137
|
-
else if (obj?._isAMomentObject) {
|
|
138
|
-
return obj.toJSON();
|
|
139
|
-
}
|
|
140
|
-
if (isArray(obj)) {
|
|
141
|
-
// Serialize Array class like
|
|
142
|
-
return types.get(Array)?.serialize(obj, context);
|
|
143
|
-
}
|
|
144
|
-
return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);
|
|
4
|
+
import { JsonSerializer } from "../domain/JsonSerializer.js";
|
|
5
|
+
const serializer = new JsonSerializer();
|
|
6
|
+
export function serialize(input, options) {
|
|
7
|
+
return serializer.map(input, options);
|
|
145
8
|
}
|
|
146
9
|
//# sourceMappingURL=serialize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,CAAC;AAClC,OAAO,+BAA+B,CAAC;AACvC,OAAO,4BAA4B,CAAC;AAEpC,OAAO,EAAC,cAAc,EAAC,MAAM,0BAA0B,CAAC;AAGxD,MAAM,UAAU,GAAG,IAAI,cAAc,EAAE,CAAC;AAExC,MAAM,UAAU,SAAS,CAAC,KAAU,EAAE,OAA+B;IACnE,OAAO,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC"}
|