-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.html
388 lines (342 loc) · 7.9 KB
/
demo.html
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, shrink-to-fit=no">
<title>JSONSon - fromJSON demo</title>
</head>
<body>
<p>
Check console output.
</p>
<p>
Legend:
</p>
<dl>
<dt>
<code>data</code>
</dt>
<dd>
Raw data
</dd>
</dl>
<dl>
<dt>
<code>json</code>
</dt>
<dd>
JSON stringified raw data
</dd>
</dl>
<dl>
<dt>
<code>parsed</code>
</dt>
<dd>
Parsed JSON and post-processed by JSONSon
</dd>
</dl>
<script type="module" src="./JSONSon.js"></script>
<script>
class FileInfo {
constructor({ path, mtime }) {
this.path = path;
this.mtime = mtime;
}
get name() {
return this.path.split('/').pop();
}
get parentPath() {
return this.path.split('/').slice(0, -1).join('/');
}
static getJSONSonSchema() {
// Use JSON.Son instead of imported JSONSon:
return JSON.Son.mix(
this,
{
mtime: Date,
}
);
}
}
window.FileInfo = FileInfo;
</script>
<script type="module">
// Following class is not exported to window.*
class DirectoryInfo extends FileInfo {
static fromJSON(data) {
return new this({
path: data.path,
mtime: new Date(data.mtime),
});
}
static getJSONSonSchema() {
return JSONSon.mix(
this,
{
childDirs: [this],
childFiles: [FileInfo],
}
);
}
}
import JSONSon from './JSONSon.js';
window.data = window.json = window.schema = null; // declare vars
window.JSONSon = JSONSon;
class TestClass1 {
foo;
ololo;
constructor(foo, ololo) {
this.foo = foo;
this.ololo = ololo;
}
static fromJSON(data) {
return new this(data.foo, data.ololo);
}
}
class TestClass2 extends TestClass1 {
toJSON() {
return [this.foo, this.ololo];
}
static fromJSON(data) {
return new this(data[0], data[1]);
// another way:
return {
foo: data[0],
ololo: data[1],
// extra: data[0] + '-' + data[1],
};
}
}
String.prototype.toJSON = function () {
const res = { _: this.toString() };
let hasProperties = false;
for (const name in this) {
if (!isNaN(name) && 0 <= name && name < this.length) {
// this is an indexed string character, so skip
continue;
}
if (name === 'length' || name === 'toJSON') {
continue;
}
hasProperties = true;
res[name] = this[name];
}
return !hasProperties ? this : res;
};
String.fromJSON = function (data) {
if (typeof data !== 'object') {
return new this(data);
}
const s = data._;
delete data._;
return new this(s);
};
//////////////////////////////////////////////
//////////////////////////////////////////////
//////////////////////////////////////////////
console.log("Explicit schema object:");
data = {
bool: true,
number: 1234,
string: 'ololo',
String: new String('Mememe'),
obj: {
date: new Date(),
dateAsMillis: Date.now(),
bool: false,
},
array: [1, '2', 3],
numArray: [1, 2, 3],
tuple: [true, 4321, 'String within tuple', new Date()],
rawType: { foo: 'bar', date: new Date() },
StringWithProperty: (() => {
const res = new String("String with property");
res.extraProperty = '1234';
return res;
})(),
};
json = JSON.stringify(data);
schema = new JSONSon({
bool: 'boolean',
number: 'number',
string: 'string',
String: String,
obj: {
date: Date,
dateAsMillis: Date, // or number?
bool: 'boolean',
},
array: Array,
numArray: ['number'],
tuple: ['boolean', 'number', 'string', Date],
// rawType: undefined,
StringWithProperty: JSONSon.mix(String, {
extraProperty: 'any', // convert to boolean
}),
});
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("TestClass1:");
data = new TestClass1('bar', 'mememe');
json = JSON.stringify(data);
schema = new JSONSon(TestClass1);
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("TestClass1 with 'extra' property:");
data = new TestClass1('bar', 'mememe');
data.extra = {
date: new Date(),
arr: [1, 2, 3],
};
json = JSON.stringify(data);
schema = new JSONSon(JSONSon.mix(
TestClass1,
{
extra: {
date: Date,
arr: ['string'],
},
}
));
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("Tuple 1:");
data = [new Date(), new TestClass2('bar', 'mememe'), 1, '2', new String('3')];
json = JSON.stringify(data);
schema = new JSONSon([
Date,
TestClass2,
'number', // third and everything after - number
]);
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("Tuple 2:");
data = [1, new Date(), 3, 4, 5, 6, 7, 8, 9];
json = JSON.stringify(data);
schema = new JSONSon([
String,
Date,
Number, // third and everything after - Number
]);
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("Directory and File:");
let directory = new DirectoryInfo({
path: '/home/bars/projects/utils/js-json-son',
mtime: new Date('2022-02-02T23:44:00+0300'),
});
directory.childDirs = [
new DirectoryInfo({
path: '/home/bars/projects/utils/js-json-son/dir-1',
mtime: new Date('2022-01-02T23:31:00+0300'),
}),
new DirectoryInfo({
path: '/home/bars/projects/utils/js-json-son/dir-2',
mtime: new Date('2022-01-02T23:32:00+0300'),
}),
];
directory.childFiles = [
new DirectoryInfo({
path: '/home/bars/projects/utils/js-json-son/JSONSon.js',
mtime: new Date('2022-01-02T23:31:00+0300'),
}),
new DirectoryInfo({
path: '/home/bars/projects/utils/js-json-son/demo.html',
mtime: new Date('2022-01-03T00:00:00+0300'),
}),
];
data = directory;
json = JSON.stringify(data);
const directoryDemoJson = json;
schema = new JSONSon(DirectoryInfo);
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("Nested JSONSon.mix:");
let file = new FileInfo({
path: './README.md',
mtime: new Date(),
});
file.mimeType = 'text/markdown';
data = file;
json = JSON.stringify(data);
schema = new JSONSon(JSONSon.mix(FileInfo, {
mimeType: String,
}));
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
console.log(new Array(80).fill('-').join(''));
///////////////////////
console.log("JSONSon <-> JSON:");
// Moreover, you can JSON.stringify JSONSon itself
// and parse on the other side:
// You should define a way to resolve constructors
// from its names:
JSONSon.resolveConstructor = (name) => {
if (name === 'DirectoryInfo') {
// Since class DirectoryInfo is not exported to window.*,
// we should pass its constructor from current scope
return DirectoryInfo;
}
return window[name];
};
schema = new JSONSon({
str: 'string',
arr: ['number'],
foo: {
bar: 'string',
},
StringObject: String,
BigIntObject: BigInt,
DirectoryInfo: DirectoryInfo,
mix: JSONSon.mix(FileInfo, { mimeType: String }),
enum: JSONSon.enum('f', 'm'),
});
console.log('schema', schema);
debugger;
let schemaJson = JSON.stringify(schema);
console.log('schemaJson', schemaJson);
schema = JSONSon.parse(JSONSon, schemaJson);
console.log('schema parsed', schema);
BigInt.prototype.toJSON = function () {
return this.toString(10);
};
data = {
str: "Here's a string",
arr: [null, true, 2, '3', 4n, 5, new Number(6)],
foo: {
bar: 'baz',
},
StringObject: new String("Here's a string object"),
BigIntObject: Object(BigInt(9007199254740993n)),
DirectoryInfo: directory,
mix: file,
enum: 'm',
};
json = JSON.stringify(data);
schema = JSONSon.parse(JSONSon, schemaJson);
console.log('data', data);
console.log('json', json);
console.log('parsed', schema.parse(json));
</script>
</body>
</html>