index.js 3.7 KB

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