@tsed/json-mapper 7.34.2 → 7.35.0-beta.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.
Files changed (50) hide show
  1. package/jest.config.js +3 -3
  2. package/lib/browser/json-mapper.umd.min.js +1 -1
  3. package/lib/browser/json-mapper.umd.min.js.map +1 -1
  4. package/lib/cjs/components/DateMapper.js +1 -1
  5. package/lib/cjs/components/DateMapper.js.map +1 -1
  6. package/lib/cjs/domain/JsonMapperCompiler.js +156 -0
  7. package/lib/cjs/domain/JsonMapperCompiler.js.map +1 -0
  8. package/lib/cjs/domain/JsonSerializer.js +214 -0
  9. package/lib/cjs/domain/JsonSerializer.js.map +1 -0
  10. package/lib/cjs/domain/JsonSerializerOptions.js +3 -0
  11. package/lib/cjs/domain/JsonSerializerOptions.js.map +1 -0
  12. package/lib/cjs/domain/Writer.js +111 -0
  13. package/lib/cjs/domain/Writer.js.map +1 -0
  14. package/lib/cjs/hooks/alterOnSerialize.js +8 -0
  15. package/lib/cjs/hooks/alterOnSerialize.js.map +1 -0
  16. package/lib/cjs/index.js +6 -0
  17. package/lib/cjs/index.js.map +1 -1
  18. package/lib/cjs/utils/getObjectProperties.js +9 -0
  19. package/lib/cjs/utils/getObjectProperties.js.map +1 -0
  20. package/lib/cjs/utils/serialize.js +5 -143
  21. package/lib/cjs/utils/serialize.js.map +1 -1
  22. package/lib/esm/components/DateMapper.js +1 -1
  23. package/lib/esm/components/DateMapper.js.map +1 -1
  24. package/lib/esm/domain/JsonMapperCompiler.js +152 -0
  25. package/lib/esm/domain/JsonMapperCompiler.js.map +1 -0
  26. package/lib/esm/domain/JsonSerializer.js +210 -0
  27. package/lib/esm/domain/JsonSerializer.js.map +1 -0
  28. package/lib/esm/domain/JsonSerializerOptions.js +2 -0
  29. package/lib/esm/domain/JsonSerializerOptions.js.map +1 -0
  30. package/lib/esm/domain/Writer.js +108 -0
  31. package/lib/esm/domain/Writer.js.map +1 -0
  32. package/lib/esm/hooks/alterOnSerialize.js +4 -0
  33. package/lib/esm/hooks/alterOnSerialize.js.map +1 -0
  34. package/lib/esm/index.js +6 -0
  35. package/lib/esm/index.js.map +1 -1
  36. package/lib/esm/utils/getObjectProperties.js +5 -0
  37. package/lib/esm/utils/getObjectProperties.js.map +1 -0
  38. package/lib/esm/utils/serialize.js +4 -141
  39. package/lib/esm/utils/serialize.js.map +1 -1
  40. package/lib/tsconfig.esm.tsbuildinfo +1 -1
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/lib/types/domain/JsonMapperCompiler.d.ts +52 -0
  43. package/lib/types/domain/JsonSerializer.d.ts +20 -0
  44. package/lib/types/domain/JsonSerializerOptions.d.ts +13 -0
  45. package/lib/types/domain/Writer.d.ts +34 -0
  46. package/lib/types/hooks/alterOnSerialize.d.ts +2 -0
  47. package/lib/types/index.d.ts +6 -0
  48. package/lib/types/utils/getObjectProperties.d.ts +1 -0
  49. package/lib/types/utils/serialize.d.ts +2 -18
  50. package/package.json +11 -8
@@ -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 { JsonMapperContext } from "../domain/JsonMapperContext.js";
10
- import { getJsonMapperTypes } from "../domain/JsonMapperTypesContainer.js";
11
- function alterValue(schema, value, options) {
12
- return schema.$hooks.alter("onSerialize", value, [options]);
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,OAA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