-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathindex.ts
More file actions
159 lines (135 loc) · 4.53 KB
/
index.ts
File metadata and controls
159 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Class, JSONValue, SuperJSONResult, SuperJSONValue } from './types.js';
import { ClassRegistry, RegisterOptions } from './class-registry.js';
import { Registry } from './registry.js';
import {
CustomTransfomer,
CustomTransformerRegistry,
} from './custom-transformer-registry.js';
import {
applyReferentialEqualityAnnotations,
applyValueAnnotations,
generateReferentialEqualityAnnotations,
walker,
} from './plainer.js';
import { type AccessDeepContext } from './accessDeep.js';
import { copy } from 'copy-anything';
export default class SuperJSON {
/**
* If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`.
*/
private readonly dedupe: boolean;
/**
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`.
*/
constructor({
dedupe = false,
}: {
dedupe?: boolean;
} = {}) {
this.dedupe = dedupe;
}
serialize(object: SuperJSONValue): SuperJSONResult {
const identities = new Map<any, any[][]>();
const output = walker(object, identities, this, this.dedupe);
const res: SuperJSONResult = {
json: output.transformedValue,
};
if (output.annotations) {
res.meta = {
...res.meta,
values: output.annotations,
};
}
const equalityAnnotations = generateReferentialEqualityAnnotations(
identities,
this.dedupe
);
if (equalityAnnotations) {
res.meta = {
...res.meta,
referentialEqualities: equalityAnnotations,
};
}
if (res.meta) res.meta.v = 1;
return res;
}
deserialize<T = unknown>(payload: SuperJSONResult, options?: { inPlace?: boolean }): T {
const { json, meta } = payload;
let result: T = options?.inPlace ? json : copy(json) as any;
const context: AccessDeepContext = new WeakMap();
if (meta?.values) {
result = applyValueAnnotations(result, meta.values, meta.v ?? 0, this, context);
}
if (meta?.referentialEqualities) {
result = applyReferentialEqualityAnnotations(
result,
meta.referentialEqualities,
meta.v ?? 0,
context
);
}
return result;
}
stringify(object: SuperJSONValue): string {
return JSON.stringify(this.serialize(object));
}
parse<T = unknown>(string: string): T {
return this.deserialize(JSON.parse(string), { inPlace: true });
}
readonly classRegistry = new ClassRegistry();
registerClass(v: Class, options?: RegisterOptions | string) {
this.classRegistry.register(v, options);
}
readonly symbolRegistry = new Registry<Symbol>(s => s.description ?? '');
registerSymbol(v: Symbol, identifier?: string) {
this.symbolRegistry.register(v, identifier);
}
readonly customTransformerRegistry = new CustomTransformerRegistry();
registerCustom<I, O extends JSONValue>(
transformer: Omit<CustomTransfomer<I, O>, 'name'>,
name: string
) {
this.customTransformerRegistry.register({
name,
...transformer,
});
}
readonly allowedErrorProps: string[] = [];
allowErrorProps(...props: string[]) {
this.allowedErrorProps.push(...props);
}
private static defaultInstance = new SuperJSON();
static serialize = SuperJSON.defaultInstance.serialize.bind(
SuperJSON.defaultInstance
);
static deserialize = SuperJSON.defaultInstance.deserialize.bind(
SuperJSON.defaultInstance
);
static stringify = SuperJSON.defaultInstance.stringify.bind(
SuperJSON.defaultInstance
);
static parse = SuperJSON.defaultInstance.parse.bind(
SuperJSON.defaultInstance
);
static registerClass = SuperJSON.defaultInstance.registerClass.bind(
SuperJSON.defaultInstance
);
static registerSymbol = SuperJSON.defaultInstance.registerSymbol.bind(
SuperJSON.defaultInstance
);
static registerCustom = SuperJSON.defaultInstance.registerCustom.bind(
SuperJSON.defaultInstance
);
static allowErrorProps = SuperJSON.defaultInstance.allowErrorProps.bind(
SuperJSON.defaultInstance
);
}
export { SuperJSON, SuperJSONResult, SuperJSONValue };
export const serialize = SuperJSON.serialize;
export const deserialize = SuperJSON.deserialize;
export const stringify = SuperJSON.stringify;
export const parse = SuperJSON.parse;
export const registerClass = SuperJSON.registerClass;
export const registerCustom = SuperJSON.registerCustom;
export const registerSymbol = SuperJSON.registerSymbol;
export const allowErrorProps = SuperJSON.allowErrorProps;