ClassTransformer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClassTransformer = void 0;
  4. const TransformOperationExecutor_1 = require("./TransformOperationExecutor");
  5. const enums_1 = require("./enums");
  6. const default_options_constant_1 = require("./constants/default-options.constant");
  7. class ClassTransformer {
  8. instanceToPlain(object, options) {
  9. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_PLAIN, {
  10. ...default_options_constant_1.defaultOptions,
  11. ...options,
  12. });
  13. return executor.transform(undefined, object, undefined, undefined, undefined, undefined);
  14. }
  15. classToPlainFromExist(object, plainObject, options) {
  16. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_PLAIN, {
  17. ...default_options_constant_1.defaultOptions,
  18. ...options,
  19. });
  20. return executor.transform(plainObject, object, undefined, undefined, undefined, undefined);
  21. }
  22. plainToInstance(cls, plain, options) {
  23. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.PLAIN_TO_CLASS, {
  24. ...default_options_constant_1.defaultOptions,
  25. ...options,
  26. });
  27. return executor.transform(undefined, plain, cls, undefined, undefined, undefined);
  28. }
  29. plainToClassFromExist(clsObject, plain, options) {
  30. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.PLAIN_TO_CLASS, {
  31. ...default_options_constant_1.defaultOptions,
  32. ...options,
  33. });
  34. return executor.transform(clsObject, plain, undefined, undefined, undefined, undefined);
  35. }
  36. instanceToInstance(object, options) {
  37. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_CLASS, {
  38. ...default_options_constant_1.defaultOptions,
  39. ...options,
  40. });
  41. return executor.transform(undefined, object, undefined, undefined, undefined, undefined);
  42. }
  43. classToClassFromExist(object, fromObject, options) {
  44. const executor = new TransformOperationExecutor_1.TransformOperationExecutor(enums_1.TransformationType.CLASS_TO_CLASS, {
  45. ...default_options_constant_1.defaultOptions,
  46. ...options,
  47. });
  48. return executor.transform(fromObject, object, undefined, undefined, undefined, undefined);
  49. }
  50. serialize(object, options) {
  51. return JSON.stringify(this.instanceToPlain(object, options));
  52. }
  53. /**
  54. * Deserializes given JSON string to a object of the given class.
  55. */
  56. deserialize(cls, json, options) {
  57. const jsonObject = JSON.parse(json);
  58. return this.plainToInstance(cls, jsonObject, options);
  59. }
  60. /**
  61. * Deserializes given JSON string to an array of objects of the given class.
  62. */
  63. deserializeArray(cls, json, options) {
  64. const jsonObject = JSON.parse(json);
  65. return this.plainToInstance(cls, jsonObject, options);
  66. }
  67. }
  68. exports.ClassTransformer = ClassTransformer;
  69. //# sourceMappingURL=ClassTransformer.js.map