makeSerializable.js 640 B

123456789101112131415161718192021222324252627282930
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const { register } = require("./serialization");
  6. class ClassSerializer {
  7. constructor(Constructor) {
  8. this.Constructor = Constructor;
  9. }
  10. serialize(obj, context) {
  11. obj.serialize(context);
  12. }
  13. deserialize(context) {
  14. if (typeof this.Constructor.deserialize === "function") {
  15. return this.Constructor.deserialize(context);
  16. }
  17. const obj = new this.Constructor();
  18. obj.deserialize(context);
  19. return obj;
  20. }
  21. }
  22. module.exports = (Constructor, request, name = null) => {
  23. register(Constructor, request, name, new ClassSerializer(Constructor));
  24. };