提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
4.2 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.allowErrorProps = exports.registerSymbol = exports.registerCustom = exports.registerClass = exports.parse = exports.stringify = exports.deserialize = exports.serialize = exports.SuperJSON = void 0;
  4. const class_registry_js_1 = require("./class-registry.cjs");
  5. const registry_js_1 = require("./registry.cjs");
  6. const custom_transformer_registry_js_1 = require("./custom-transformer-registry.cjs");
  7. const plainer_js_1 = require("./plainer.cjs");
  8. const copy_anything_1 = require("copy-anything");
  9. class SuperJSON {
  10. /**
  11. * @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`.
  12. */
  13. constructor({ dedupe = false, } = {}) {
  14. this.classRegistry = new class_registry_js_1.ClassRegistry();
  15. this.symbolRegistry = new registry_js_1.Registry(s => s.description ?? '');
  16. this.customTransformerRegistry = new custom_transformer_registry_js_1.CustomTransformerRegistry();
  17. this.allowedErrorProps = [];
  18. this.dedupe = dedupe;
  19. }
  20. serialize(object) {
  21. const identities = new Map();
  22. const output = plainer_js_1.walker(object, identities, this, this.dedupe);
  23. const res = {
  24. json: output.transformedValue,
  25. };
  26. if (output.annotations) {
  27. res.meta = {
  28. ...res.meta,
  29. values: output.annotations,
  30. };
  31. }
  32. const equalityAnnotations = plainer_js_1.generateReferentialEqualityAnnotations(identities, this.dedupe);
  33. if (equalityAnnotations) {
  34. res.meta = {
  35. ...res.meta,
  36. referentialEqualities: equalityAnnotations,
  37. };
  38. }
  39. if (res.meta)
  40. res.meta.v = 1;
  41. return res;
  42. }
  43. deserialize(payload, options) {
  44. const { json, meta } = payload;
  45. let result = options?.inPlace ? json : copy_anything_1.copy(json);
  46. if (meta?.values) {
  47. result = plainer_js_1.applyValueAnnotations(result, meta.values, meta.v ?? 0, this);
  48. }
  49. if (meta?.referentialEqualities) {
  50. result = plainer_js_1.applyReferentialEqualityAnnotations(result, meta.referentialEqualities, meta.v ?? 0);
  51. }
  52. return result;
  53. }
  54. stringify(object) {
  55. return JSON.stringify(this.serialize(object));
  56. }
  57. parse(string) {
  58. return this.deserialize(JSON.parse(string), { inPlace: true });
  59. }
  60. registerClass(v, options) {
  61. this.classRegistry.register(v, options);
  62. }
  63. registerSymbol(v, identifier) {
  64. this.symbolRegistry.register(v, identifier);
  65. }
  66. registerCustom(transformer, name) {
  67. this.customTransformerRegistry.register({
  68. name,
  69. ...transformer,
  70. });
  71. }
  72. allowErrorProps(...props) {
  73. this.allowedErrorProps.push(...props);
  74. }
  75. }
  76. exports.default = SuperJSON;
  77. exports.SuperJSON = SuperJSON;
  78. SuperJSON.defaultInstance = new SuperJSON();
  79. SuperJSON.serialize = SuperJSON.defaultInstance.serialize.bind(SuperJSON.defaultInstance);
  80. SuperJSON.deserialize = SuperJSON.defaultInstance.deserialize.bind(SuperJSON.defaultInstance);
  81. SuperJSON.stringify = SuperJSON.defaultInstance.stringify.bind(SuperJSON.defaultInstance);
  82. SuperJSON.parse = SuperJSON.defaultInstance.parse.bind(SuperJSON.defaultInstance);
  83. SuperJSON.registerClass = SuperJSON.defaultInstance.registerClass.bind(SuperJSON.defaultInstance);
  84. SuperJSON.registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(SuperJSON.defaultInstance);
  85. SuperJSON.registerCustom = SuperJSON.defaultInstance.registerCustom.bind(SuperJSON.defaultInstance);
  86. SuperJSON.allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(SuperJSON.defaultInstance);
  87. exports.serialize = SuperJSON.serialize;
  88. exports.deserialize = SuperJSON.deserialize;
  89. exports.stringify = SuperJSON.stringify;
  90. exports.parse = SuperJSON.parse;
  91. exports.registerClass = SuperJSON.registerClass;
  92. exports.registerCustom = SuperJSON.registerCustom;
  93. exports.registerSymbol = SuperJSON.registerSymbol;
  94. exports.allowErrorProps = SuperJSON.allowErrorProps;
  95. //# sourceMappingURL=index.js.map