-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONSon.js
291 lines (262 loc) · 6.89 KB
/
JSONSon.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import areEqual from './areEqual.js';
const _globalThis = typeof globalThis === 'object' ? globalThis : this;
export default class JSONSon {
_type;
constructor(type) {
this._type = type;
}
parse(json) {
return this.constructor.parse(this._type, json);
}
make(data) {
return this.constructor.make(this._type, data);
}
static parse(type, json) {
return this.make(type, JSON.parse(json));
}
static make(type, data) {
type = this._resolveType(type);
const typeType = typeof type;
if (type === undefined || type === 'any') {
return data;
}
else if (type === 'string') {
return typeof data === 'undefined' ? '' : '' + data;
}
else if (type === 'number') {
return +data;
}
else if (type === 'boolean') {
return !!(isNaN(data) ? data : +data);
}
else if (type === 'bigint') {
return BigInt(data);
}
else if (type instanceof Array || type === Array) {
const res = type === Array || type.constructor === Array
? new Array(data ? data.length : 0)
: Object.create(type.prototype)
;
if (data === undefined) {
return res;
}
return this._fillArray(res, type, data);
}
else if (typeType === 'function') {
return this._instantiate(type, data);
}
else if (type instanceof JSONSonInternal) {
return type.make(data);
}
else if (typeType === 'object') {
return this._fillObject({}, type, data);
}
else {
throw new Error(`Unsupported type: '${type}'`);
}
}
static _resolveType(type) {
if (typeof type === 'function' && typeof type.getJSONSonSchema === 'function') {
type = type.getJSONSonSchema();
}
if (type instanceof JSONSon) {
type = type._type;
}
return type;
}
static _instantiate(constructor, data, force) {
if (data == null && !force) {
return data;
}
if (constructor === BigInt) {
return Object(BigInt(data));
}
let res = constructor.fromJSON ? constructor.fromJSON(data) : new constructor(data);
if (!res.constructor || res.constructor.name === 'Object') {
const temp = res;
res = Object.create(constructor.prototype);
for (const name in temp) {
res[name] = temp[name];
}
}
return res;
}
static _fillArray(arr, type, data) {
if (!(data instanceof Array)) {
throw new Error("Data should be an array");
}
let leaf = undefined;
for (let i = 0; i < data.length; i++) {
if (type.length > i) {
leaf = type[i];
}
arr[i] = this.make(leaf, data[i]);
}
return arr;
}
static _fillObject(obj, type, data, names) {
if (typeof data !== 'object') {
throw new Error("Data should be an object");
}
if (!names) {
names = Object.keys(data);
}
for (const name of names) {
obj[name] = this.make(type[name], data[name]);
}
return obj;
}
static mix(constructor, propsObject) {
return new JSONSonMix(constructor, propsObject);
}
static enum(...supportedValues) {
return new JSONSonEnum(...supportedValues);
}
toJSON() {
const converter = (data, path) => {
if (typeof data === 'function') {
return {
type: 'constructor',
name: data.name,
};
}
else if (data instanceof Array) {
const res = new Array(data.length);
for (const i in data) {
res[i] = converter(data[i], path.concat([i]));
}
return res;
}
else if (data instanceof JSONSonInternal) {
return {
type: 'internal',
name: data.constructor.name,
value: data,
};
}
else if (typeof data === 'object' && data !== null) {
const value = {};
for (const k in data) {
value[k] = converter(data[k], path.concat([k]));
}
return {
type: 'object',
name: data.constructor.name !== 'Object' ? data.constructor.name : undefined,
value: value,
};
}
else {
return data;
}
};
return converter(this._type, []);
}
static fromJSON(data) {
const converter = (data, path) => {
if (typeof data !== 'object' || data == null) {
return data;
}
else if (data instanceof Array) {
const res = new Array(data.length);
for (const i in data) {
res[i] = converter(data[i], path.concat([i]));
}
return res;
}
else if (data.type === 'constructor') {
const res = this.resolveConstructor(data.name);
if (!res) {
throw new Error(`Unable to resolve constructor '${data.name}'`);
}
return res;
}
else if (data.type === 'internal') {
const constructor = JSONSonInternal.implementations[data.name];
if (!constructor) {
throw new Error(`Unknown type: '${data.name}'. Please check if you are using compatible JSONSon version`);
}
return this._instantiate(constructor, data.value);
}
else if (data.type === 'object') {
const constructor = this.resolveConstructor(data.name || 'Object') || Object;
const res = constructor === Array ? [] : Object.create(constructor.prototype);
for (const k in data.value) {
res[k] = converter(data.value[k], path.concat(['value', k]));
}
return res;
}
const pathStr = ['$'].concat(path).join('.');
throw new Error(`Invalid value at '${pathStr}'`);
};
return new this(converter(data, []));
}
static resolveConstructor(name) {
return _globalThis[name];
}
}
class JSONSonInternal {
static implementations = {};
}
class JSONSonMix extends JSONSonInternal {
underlyingConstructor;
propsObject;
constructor(underlyingConstructor, propsObject) {
super();
this.underlyingConstructor = underlyingConstructor;
this.propsObject = propsObject;
}
make(data) {
const type = JSONSon._resolveType(this.underlyingConstructor);
const res = areEqual(this, type)
? JSONSon._instantiate(this.underlyingConstructor, data)
: JSONSon.make(type, data)
;
if (res == null) {
return res;
}
const names = res.constructor === Object
? undefined
: Object.keys(this.propsObject)
;
JSONSon._fillObject(res, this.propsObject, data, names);
return res;
}
toJSON() {
return {
underlyingConstructor: this.underlyingConstructor.name,
propsObject: new JSONSon(this.propsObject),
};
}
static fromJSON(data) {
const underlyingConstructor = JSONSon.resolveConstructor(data.underlyingConstructor);
if (!underlyingConstructor) {
throw new Error(`Unable to resolve constructor '${data.underlyingConstructor}'`);
}
const propsObject = JSONSon.fromJSON(data.propsObject)._type;
return new this(underlyingConstructor, propsObject);
}
}
JSONSonInternal.implementations['JSONSonMix'] = JSONSonMix;
class JSONSonEnum extends JSONSonInternal {
supportedValues = new Set();
constructor(...supportedValues) {
super();
supportedValues.forEach((v) => this.supportedValues.add(v));
}
make(data) {
if (!this.supportedValues.has(data)) {
throw new Error(`Unknown enum value: ${data}`);
}
return data;
}
toJSON() {
return {
supportedValues: Array.from(this.supportedValues),
};
}
static fromJSON(data) {
return new this(...data.supportedValues);
}
}
JSONSonInternal.implementations['JSONSonEnum'] = JSONSonEnum;
JSON.Son = JSONSon;