@tsed/json-mapper 6.100.2 → 6.102.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.
@@ -32,7 +32,11 @@ function getType(propStore, value) {
32
32
  function classToPlainObject(obj, options) {
33
33
  const { useAlias = true, type, ...props } = options;
34
34
  const entity = schema_1.JsonEntityStore.from(type || obj);
35
- return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {
35
+ const additionalProperties = !!entity.schema.get("additionalProperties");
36
+ const schemaProperties = getSchemaProperties(entity, obj);
37
+ const properties = new Set();
38
+ const out = schemaProperties.reduce((newObj, [key, propStore]) => {
39
+ properties.add(key);
36
40
  const schema = propStore.schema;
37
41
  if ((0, schema_1.alterIgnore)(schema, { useAlias, ...props, self: obj })) {
38
42
  return newObj;
@@ -53,6 +57,14 @@ function classToPlainObject(obj, options) {
53
57
  [key]: value
54
58
  };
55
59
  }, {});
60
+ if (additionalProperties) {
61
+ (0, core_1.objectKeys)(obj).forEach((key) => {
62
+ if (!properties.has(key)) {
63
+ out[key] = obj[key];
64
+ }
65
+ });
66
+ }
67
+ return out;
56
68
  }
57
69
  exports.classToPlainObject = classToPlainObject;
58
70
  function toObject(obj, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":";;;AAAA,qCAYoB;AACpB,yCAA4G;AAC5G,yBAAuB;AACvB,mEAA8D;AAC9D,iFAAsE;AAgBtE,SAAS,UAAU,CAAC,MAAkB,EAAE,KAAU,EAAE,OAAwB;IAC1E,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,mBAAmB,CAAC,UAA2B,EAAE,GAAQ;IAChE,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,IAAA,4BAAmB,EAAC,UAAU,CAAC,CA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import {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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 (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
1
+ {"version":3,"file":"serialize.js","sourceRoot":"","sources":["../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,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,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,IAAA,aAAM,EAAC,WAAW,CAAC,CAAC,CAAC;IACxE,IAAI,MAAM,EAAE;QACV,MAAM,UAAU,GAAG,MAAO,CAAC;QAE3B,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;SAAM,IAAI,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,gBAAgB,EAAE;QAChC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;KACrB;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;AArDD,8BAqDC","sourcesContent":["import {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n value = serialize(value, {\n useAlias,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if ((typeof obj.toJSON === \"function\" && obj.$isMongooseModelPrototype) || obj._bsontype) {\n return obj.toJSON(options);\n }\n\n if (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
@@ -29,7 +29,11 @@ function getType(propStore, value) {
29
29
  export function classToPlainObject(obj, options) {
30
30
  const { useAlias = true, type, ...props } = options;
31
31
  const entity = JsonEntityStore.from(type || obj);
32
- return getSchemaProperties(entity, obj).reduce((newObj, [key, propStore]) => {
32
+ const additionalProperties = !!entity.schema.get("additionalProperties");
33
+ const schemaProperties = getSchemaProperties(entity, obj);
34
+ const properties = new Set();
35
+ const out = schemaProperties.reduce((newObj, [key, propStore]) => {
36
+ properties.add(key);
33
37
  const schema = propStore.schema;
34
38
  if (alterIgnore(schema, { useAlias, ...props, self: obj })) {
35
39
  return newObj;
@@ -50,6 +54,14 @@ export function classToPlainObject(obj, options) {
50
54
  [key]: value
51
55
  };
52
56
  }, {});
57
+ if (additionalProperties) {
58
+ objectKeys(obj).forEach((key) => {
59
+ if (!properties.has(key)) {
60
+ out[key] = obj[key];
61
+ }
62
+ });
63
+ }
64
+ return out;
53
65
  }
54
66
  function toObject(obj, options) {
55
67
  return getObjectProperties(obj).reduce((newObj, [key, value]) => ({
@@ -1 +1 @@
1
- {"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,OAAO,EACP,aAAa,EACb,YAAY,EACZ,OAAO,EACP,UAAU,EACV,KAAK,EAEL,MAAM,EACN,UAAU,EAEX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,WAAW,EAAE,mBAAmB,EAAE,eAAe,EAA8B,MAAM,cAAc,CAAC;AAC5G,OAAO,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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C,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,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;IACxE,IAAI,MAAM,EAAE;QACV,MAAM,UAAU,GAAG,MAAO,CAAC;QAE3B,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;SAAM,IAAI,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,gBAAgB,EAAE;QAChC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;KACrB;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 {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\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 (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
1
+ {"version":3,"file":"serialize.js","sourceRoot":"","sources":["../../../src/utils/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,OAAO,EACP,aAAa,EACb,YAAY,EACZ,OAAO,EACP,UAAU,EACV,KAAK,EAE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AAO,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,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;IACxE,IAAI,MAAM,EAAE;QACV,MAAM,UAAU,GAAG,MAAO,CAAC;QAE3B,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;KAC3C;SAAM,IAAI,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,gBAAgB,EAAE;QAChC,OAAO,GAAG,CAAC,MAAM,EAAE,CAAC;KACrB;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 {\n classOf,\n isArray,\n isClassObject,\n isCollection,\n isEmpty,\n isFunction,\n isNil,\n MetadataTypes,\n nameOf,\n objectKeys,\n Type\n} from \"@tsed/core\";\nimport {alterIgnore, getPropertiesStores, JsonEntityStore, JsonHookContext, JsonSchema} from \"@tsed/schema\";\nimport \"../components\";\nimport {JsonMapperContext} from \"../domain/JsonMapperContext\";\nimport {getJsonMapperTypes} from \"../domain/JsonMapperTypesContainer\";\nimport {JsonMapperMethods} from \"../interfaces/JsonMapperMethods\";\n\nexport interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T, C> {\n /**\n * Types used to map complex types (Symbol, Array, Set, Map)\n */\n types?: Map<Type<any>, JsonMapperMethods>;\n /**\n * useAlias mapping\n */\n useAlias?: boolean;\n\n [key: string]: any;\n}\n\nfunction alterValue(schema: JsonSchema, value: any, options: JsonHookContext) {\n return schema.$hooks.alter(\"onSerialize\", value, [options]);\n}\n\nfunction getSchemaProperties(storedJson: JsonEntityStore, obj: any) {\n const stores = Array.from(getPropertiesStores(storedJson).entries());\n\n if (!stores.length) {\n // fallback to auto discovering field from obj\n objectKeys(obj).forEach((key) => {\n const propStore = JsonEntityStore.from(storedJson.target, key);\n stores.push([key, propStore]);\n });\n }\n\n return stores;\n}\n\nfunction getObjectProperties(obj: any): [string, any][] {\n return Object.entries(obj).filter(([, value]) => !isFunction(value));\n}\n\nfunction getType(propStore: JsonEntityStore, value: any) {\n if (isNil(value) || propStore.type === Object || isCollection(propStore.type)) {\n return undefined;\n }\n\n return propStore.type;\n}\n\nexport function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>) {\n const {useAlias = true, type, ...props} = options;\n\n const entity = JsonEntityStore.from(type || obj);\n\n const additionalProperties = !!entity.schema.get(\"additionalProperties\");\n const schemaProperties = getSchemaProperties(entity, obj);\n const properties = new Set<string>();\n const out: any = schemaProperties.reduce((newObj, [key, propStore]) => {\n properties.add(key as string);\n\n const schema = propStore.schema;\n\n if (alterIgnore(schema, {useAlias, ...props, self: obj})) {\n return newObj;\n }\n\n let value = alterValue(schema, obj[key], {useAlias, ...props, self: obj});\n value = serialize(value, {\n useAlias,\n type: value === obj[key] ? getType(propStore, value) : undefined,\n collectionType: propStore.collectionType,\n ...props\n });\n\n if (value === undefined) {\n return newObj;\n }\n\n key = useAlias ? propStore.parent.schema.getAliasOf(key) || key : key;\n\n return {\n ...newObj,\n [key]: value\n };\n }, {});\n\n if (additionalProperties) {\n objectKeys(obj).forEach((key: any) => {\n if (!properties.has(key)) {\n out[key] = obj[key];\n }\n });\n }\n return out;\n}\n\nfunction toObject(obj: any, options: JsonSerializerOptions): any {\n return getObjectProperties(obj).reduce(\n (newObj, [key, value]) => ({\n ...newObj,\n [key]: serialize(value, options)\n }),\n {}\n );\n}\n\nfunction getBestType(type: Type<any>, obj: any) {\n const dataType = classOf(obj);\n\n if (dataType && !isClassObject(dataType)) {\n return dataType;\n }\n\n return type || Object;\n}\n\nexport function serialize(obj: any, {type, collectionType, groups = false, ...options}: JsonSerializerOptions = {}): any {\n if (isEmpty(obj)) {\n return obj;\n }\n\n let currentType = type;\n\n const types = options.types ? options.types : getJsonMapperTypes();\n options.groups = groups;\n\n // FIX custom serialization function from @tsed/mongoose and bson\n if ((typeof obj.toJSON === \"function\" && obj.$isMongooseModelPrototype) || obj._bsontype) {\n return obj.toJSON(options);\n }\n\n if (!(obj && obj?._isAMomentObject) && typeof obj.toJSON === \"function\") {\n return serialize(obj.toJSON(), {...options, type: classOf(obj)});\n }\n\n if (!isCollection(obj)) {\n options.type = currentType = getBestType(currentType, obj);\n } else if (!options.collectionType) {\n currentType = classOf(obj);\n options.type = type;\n options.collectionType = currentType;\n }\n\n const context = new JsonMapperContext({\n type: currentType,\n options,\n next: (data) =>\n serialize(data, {\n ...options,\n collectionType: undefined,\n type: options.type\n })\n });\n\n const mapper = types.get(currentType) || types.get(nameOf(currentType));\n if (mapper) {\n const jsonMapper = mapper!;\n\n return jsonMapper.serialize(obj, context);\n } else if (obj?._isAMomentObject) {\n return obj.toJSON();\n }\n\n if (isArray(obj)) {\n // Serialize Array class like\n return types.get(Array)?.serialize(obj, context);\n }\n\n return !isClassObject(currentType) ? classToPlainObject(obj, options) : toObject(obj, options);\n}\n"]}
@@ -12,5 +12,5 @@ export interface JsonSerializerOptions<T = any, C = any> extends MetadataTypes<T
12
12
  useAlias?: boolean;
13
13
  [key: string]: any;
14
14
  }
15
- export declare function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>): {};
15
+ export declare function classToPlainObject(obj: any, options: JsonSerializerOptions<any, any>): any;
16
16
  export declare function serialize(obj: any, { type, collectionType, groups, ...options }?: JsonSerializerOptions): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/json-mapper",
3
- "version": "6.100.2",
3
+ "version": "6.102.1",
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.100.2",
37
- "@tsed/exceptions": "6.100.2",
38
- "@tsed/schema": "6.100.2"
36
+ "@tsed/core": "6.102.1",
37
+ "@tsed/exceptions": "6.102.1",
38
+ "@tsed/schema": "6.102.1"
39
39
  },
40
40
  "peerDependencies": {
41
- "@tsed/core": "^6.100.2",
42
- "@tsed/exceptions": "^6.100.2",
43
- "@tsed/schema": "^6.100.2"
41
+ "@tsed/core": "^6.102.1",
42
+ "@tsed/exceptions": "^6.102.1",
43
+ "@tsed/schema": "^6.102.1"
44
44
  },
45
45
  "repository": "https://github.com/tsedio/tsed",
46
46
  "bugs": {
package/readme.md CHANGED
@@ -49,6 +49,7 @@ npm install --save @tsed/schema @tsed/json-mapper
49
49
  ```
50
50
 
51
51
  ## Contributors
52
+
52
53
  Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
53
54
 
54
55
  <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
@@ -8,16 +8,5 @@
8
8
  "declaration": true,
9
9
  "declarationDir": "./lib/types"
10
10
  },
11
- "exclude": [
12
- "node_modules",
13
- "test",
14
- "lib",
15
- "benchmark",
16
- "coverage",
17
- "spec",
18
- "**/*.benchmark.ts",
19
- "**/*.spec.ts",
20
- "keys",
21
- "jest.config.js"
22
- ]
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
23
12
  }