json-as 0.2.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +52 -99
- package/asconfig.json +10 -35
- package/assembly/__benches__/as-tral.d.ts +1 -0
- package/assembly/__benches__/benchmark.ts +69 -0
- package/assembly/chars.ts +16 -0
- package/assembly/index.ts +337 -757
- package/assembly/test.ts +35 -192
- package/assembly/tsconfig.json +9 -0
- package/index.ts +1 -0
- package/package.json +26 -37
- package/tests/index.js +3 -3
- package/tests/test.js +13 -6
- package/transform/lib/index.js +47 -150
- package/transform/package.json +30 -2
- package/transform/src/index.old.js +430 -0
- package/transform/src/index.ts +98 -0
- package/transform/tsconfig.json +68 -69
- package/assembly/DynamicObject.ts +0 -29
- package/assembly/bench.ts +0 -199
- package/assembly/unknown.ts +0 -175
- package/bench/bench.js +0 -102
- package/transform/index.ts +0 -223
package/assembly/index.ts
CHANGED
|
@@ -1,757 +1,337 @@
|
|
|
1
|
-
import { StringSink } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
// @ts-ignore
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// @ts-ignore
|
|
168
|
-
else if (
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
else
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
|
|
199
|
-
// @ts-ignore
|
|
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
|
-
function serializeUnknownArray(data: unknown[]): string {
|
|
340
|
-
const result = new StringSink(lbracket)
|
|
341
|
-
const len: u32 = data.length - 1
|
|
342
|
-
for (let i: u32 = 0; i < len; i++) {
|
|
343
|
-
result.write(serializeUnknown(unchecked(data[i])))
|
|
344
|
-
result.writeCodePoint(commaCode)
|
|
345
|
-
}
|
|
346
|
-
result.write(serializeUnknown(unchecked(data[len])))
|
|
347
|
-
result.writeCodePoint(rbracketCode)
|
|
348
|
-
return result.toString()
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function serializeDeepArray<T extends Array<any>>(data: T): string {
|
|
352
|
-
const result = new StringSink(lbracket)
|
|
353
|
-
const len: u32 = data.length - 1
|
|
354
|
-
for (let i: u32 = 0; i < len; i++) {
|
|
355
|
-
result.write(serializeArray<valueof<T>>(unchecked(data[i])))
|
|
356
|
-
result.writeCodePoint(commaCode)
|
|
357
|
-
}
|
|
358
|
-
result.write(serializeArray<valueof<T>>(unchecked(data[len])))
|
|
359
|
-
result.writeCodePoint(rbracketCode)
|
|
360
|
-
return result.toString()
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function serializeObjectArray<T extends Array<any>>(data: T): string {
|
|
364
|
-
const result = new StringSink(lbracket)
|
|
365
|
-
const len: u32 = data.length - 1
|
|
366
|
-
let obj!: valueof<T>
|
|
367
|
-
for (let i: u32 = 0; i < len; i++) {
|
|
368
|
-
obj = unchecked(data[i])
|
|
369
|
-
if (obj.__encoded.length === 0) obj.__encode()
|
|
370
|
-
result.write(obj.__encoded)
|
|
371
|
-
result.writeCodePoint(commaCode)
|
|
372
|
-
}
|
|
373
|
-
obj = unchecked(data[len])
|
|
374
|
-
if (obj.__encoded.length === 0) obj.__encode()
|
|
375
|
-
result.write(obj.__encoded)
|
|
376
|
-
result.writeCodePoint(rbracketCode)
|
|
377
|
-
return result.toString()
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function serializeDynamicObjectArray(data: DynamicObject[]): string {
|
|
381
|
-
const result = new StringSink(lbracket)
|
|
382
|
-
const len: u32 = data.length - 1
|
|
383
|
-
for (let i: u32 = 0; i < len; i++) {
|
|
384
|
-
result.write(serializeDynamicObject(unchecked(data[i])))
|
|
385
|
-
result.writeCodePoint(commaCode)
|
|
386
|
-
}
|
|
387
|
-
result.write(serializeDynamicObject(unchecked(data[len])))
|
|
388
|
-
result.writeCodePoint(rbracketCode)
|
|
389
|
-
return result.toString()
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
export function serializeArray<T extends Array<any>>(data: T): string {
|
|
393
|
-
let type!: valueof<T>
|
|
394
|
-
const len = data.length - 1;
|
|
395
|
-
if (len === -1) return lbracket + rbracket
|
|
396
|
-
let result = new StringSink(lbracket);
|
|
397
|
-
if (isString<valueof<T>>()) {
|
|
398
|
-
for (let i = 0; i < len; i++) {
|
|
399
|
-
result.write(serializeString(unchecked(data[i])))
|
|
400
|
-
result.writeCodePoint(commaCode)
|
|
401
|
-
}
|
|
402
|
-
result.write(serializeString(unchecked(data[len])))
|
|
403
|
-
result.writeCodePoint(rbracketCode)
|
|
404
|
-
return result.toString()
|
|
405
|
-
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
406
|
-
for (let i = 0; i < len; i++) {
|
|
407
|
-
result.write(serializeNumber<valueof<T>>(unchecked(data[i])))
|
|
408
|
-
result.writeCodePoint(commaCode)
|
|
409
|
-
}
|
|
410
|
-
result.write(serializeNumber<valueof<T>>(unchecked(data[len])))
|
|
411
|
-
result.writeCodePoint(rbracketCode)
|
|
412
|
-
return result.toString()
|
|
413
|
-
} else if (isBoolean<valueof<T>>()) {
|
|
414
|
-
for (let i = 0; i < len; i++) {
|
|
415
|
-
result.write(serializeBoolean(unchecked(data[i])))
|
|
416
|
-
result.writeCodePoint(commaCode)
|
|
417
|
-
}
|
|
418
|
-
result.write(serializeBoolean(unchecked(data[len])))
|
|
419
|
-
result.writeCodePoint(rbracketCode)
|
|
420
|
-
return result.toString()
|
|
421
|
-
// @ts-ignore
|
|
422
|
-
} else if (type instanceof unknown) {
|
|
423
|
-
for (let i = 0; i < len; i++) {
|
|
424
|
-
result.write(serializeUnknown(unchecked(data[i])))
|
|
425
|
-
result.writeCodePoint(commaCode)
|
|
426
|
-
}
|
|
427
|
-
result.write(serializeUnknown(unchecked(data[len])))
|
|
428
|
-
result.writeCodePoint(rbracketCode)
|
|
429
|
-
return result.toString()
|
|
430
|
-
} else if (isArray<valueof<T>>()) {
|
|
431
|
-
for (let i = 0; i < len; i++) {
|
|
432
|
-
result.write(serializeArray<valueof<T>>(unchecked(data[i])))
|
|
433
|
-
result.writeCodePoint(commaCode)
|
|
434
|
-
}
|
|
435
|
-
result.write(serializeArray<valueof<T>>(unchecked(data[len])))
|
|
436
|
-
result.writeCodePoint(rbracketCode)
|
|
437
|
-
return result.toString()
|
|
438
|
-
// @ts-ignore
|
|
439
|
-
} else if (type instanceof DynamicObject) {
|
|
440
|
-
for (let i = 0; i < len; i++) {
|
|
441
|
-
result.write(serializeDynamicObject(unchecked(data[i])))
|
|
442
|
-
result.writeCodePoint(commaCode)
|
|
443
|
-
}
|
|
444
|
-
result.write(serializeDynamicObject(unchecked(data[len])))
|
|
445
|
-
result.writeCodePoint(rbracketCode)
|
|
446
|
-
return result.toString()
|
|
447
|
-
} else {
|
|
448
|
-
for (let i = 0; i < len; i++) {
|
|
449
|
-
const elem = unchecked(data[i])
|
|
450
|
-
// @ts-ignore
|
|
451
|
-
if (elem.__encoded.length === 0) elem.__encode()
|
|
452
|
-
}
|
|
453
|
-
// @ts-ignore
|
|
454
|
-
result.write(elem.__encoded)
|
|
455
|
-
result.writeCodePoint(rcbracketCode)
|
|
456
|
-
}
|
|
457
|
-
return result.toString()
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
export function parseUnknown(data: string): unknown {
|
|
461
|
-
const char = data.charCodeAt(0)
|
|
462
|
-
// @ts-ignore
|
|
463
|
-
if (char === quoteCode) return unknown.wrap(parseString(data))
|
|
464
|
-
// @ts-ignore
|
|
465
|
-
else if (char === t_charCode) return unknownTrue
|
|
466
|
-
else if (char === f_charCode) return unknownFalse
|
|
467
|
-
// @ts-ignore
|
|
468
|
-
else if (char === lbracketCode) return unknown.wrap(parseUnknownArray(data))
|
|
469
|
-
// @ts-ignore
|
|
470
|
-
else if (char === nCode) return unknownNull
|
|
471
|
-
// @ts-ignore
|
|
472
|
-
//else if (char === lcbracketCode) return parseObject<T>(data)
|
|
473
|
-
// @ts-ignore
|
|
474
|
-
return parseUnknownNumber(data)
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
export function parseBoolean(data: string): boolean {
|
|
479
|
-
return data.charCodeAt(0) === t_charCode ? true : false
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
export function parseString(data: string): string {
|
|
483
|
-
return data.slice(1, data.length - 1).replaceAll(escapeQuote, quote)
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
export function parseNumber<T>(data: string): T {
|
|
487
|
-
let type: T
|
|
488
|
-
// @ts-ignore
|
|
489
|
-
if (type instanceof f64) return F64.parseFloat(data)
|
|
490
|
-
// @ts-ignore
|
|
491
|
-
else if (type instanceof f32) return F32.parseFloat(data)
|
|
492
|
-
// @ts-ignore
|
|
493
|
-
else if (type instanceof i32) return I32.parseInt(data)
|
|
494
|
-
// @ts-ignore
|
|
495
|
-
else if (type instanceof u32) return U32.parseInt(data)
|
|
496
|
-
// @ts-ignore
|
|
497
|
-
else if (type instanceof u64) return U64.parseInt(data)
|
|
498
|
-
// @ts-ignore
|
|
499
|
-
else if (type instanceof i64) return I64.parseInt(data)
|
|
500
|
-
// @ts-ignore
|
|
501
|
-
else if (type instanceof u8) return U8.parseInt(data)
|
|
502
|
-
// @ts-ignore
|
|
503
|
-
else if (type instanceof u16) return U16.parseInt(data)
|
|
504
|
-
// @ts-ignore
|
|
505
|
-
else if (type instanceof i16) return I16.parseInt(data)
|
|
506
|
-
// @ts-ignore
|
|
507
|
-
return I8.parseInt(data)
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
export function parseUnknownNumber(data: string): unknown {
|
|
511
|
-
// @ts-ignore
|
|
512
|
-
if (data.includes('.')) return unknown.wrap(F64.parseFloat(data))
|
|
513
|
-
// @ts-ignore
|
|
514
|
-
return unknown.wrap(I64.parseInt(data))
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
export function parseArray<T extends Array<unknown>>(data: string): T {
|
|
518
|
-
let type!: valueof<T>
|
|
519
|
-
// @ts-ignore
|
|
520
|
-
if (isString<valueof<T>>()) return parseStringArray(data)
|
|
521
|
-
// @ts-ignore
|
|
522
|
-
else if (isBoolean<valueof<T>>()) return parseBooleanArray(data)
|
|
523
|
-
// @ts-ignore
|
|
524
|
-
else if (isArray<valueof<T>>()) return parseArrayArray<T>(data)
|
|
525
|
-
// @ts-ignore
|
|
526
|
-
else if (type instanceof unknown) return parseUnknownArray(data)
|
|
527
|
-
// @ts-ignore
|
|
528
|
-
return parseNumberArray<valueof<T>>(data)
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
export function parseStringArray(data: string): Array<string> {
|
|
532
|
-
const result = new Array<string>()
|
|
533
|
-
if (data.length === 2) return result
|
|
534
|
-
let lastPos: u32 = 1
|
|
535
|
-
let char: u32 = 0
|
|
536
|
-
let instr: boolean = false
|
|
537
|
-
for (let i: u32 = 1; i < u32(data.length - 1); i++) {
|
|
538
|
-
char = data.charCodeAt(i)
|
|
539
|
-
// This ignores [ and ] or { and } if they are inside a string.
|
|
540
|
-
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode) instr = instr ? false : true
|
|
541
|
-
if (instr === false) {
|
|
542
|
-
// Handles whitespace after a comma
|
|
543
|
-
if (char === WS1code || char === WS2code || char === WS3code || char === WS4code || char === WS5code) lastPos++
|
|
544
|
-
if (char === quoteCode) {
|
|
545
|
-
result.push(parseString(data.slice(lastPos, i + 1)))
|
|
546
|
-
lastPos = i + 2
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return result
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
export function parseBooleanArray(data: string): Array<boolean> {
|
|
554
|
-
const result = new Array<boolean>()
|
|
555
|
-
if (data.length === 2) return result
|
|
556
|
-
let char: u32 = 0
|
|
557
|
-
let instr: boolean = false
|
|
558
|
-
for (let i: u32 = 1; i < u32(data.length - 1); i++) {
|
|
559
|
-
char = data.charCodeAt(i)
|
|
560
|
-
if (instr === false) {
|
|
561
|
-
if (char === t_charCode) {
|
|
562
|
-
result.push(true)
|
|
563
|
-
i += 4
|
|
564
|
-
} else {
|
|
565
|
-
result.push(false)
|
|
566
|
-
i += 5
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
return result
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
export function parseNumberArray<T>(data: string): Array<T> {
|
|
574
|
-
const result = new Array<T>()
|
|
575
|
-
if (data.length === 2) return result
|
|
576
|
-
let lastPos: u32 = 0
|
|
577
|
-
let char: u32 = 0
|
|
578
|
-
let i: u32 = 1
|
|
579
|
-
for (; i < u32(data.length - 1); i++) {
|
|
580
|
-
char = data.charCodeAt(i)
|
|
581
|
-
if (char === commaCode) {
|
|
582
|
-
//console.log('Found number: ' + data.slice(lastPos + 1, i))
|
|
583
|
-
result.push(parseNumber<T>(data.slice(lastPos + 1, i)))
|
|
584
|
-
lastPos = i
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
// console.log('Found number: ' + data.slice(lastPos + 1, i))
|
|
588
|
-
result.push(parseNumber<T>(data.slice(lastPos + 1, i)))
|
|
589
|
-
return result
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
export function parseUnknownArray(data: string): Array<unknown> {
|
|
593
|
-
const result = new Array<unknown>()
|
|
594
|
-
if (data.length === 2) return result
|
|
595
|
-
let lastPos: i32 = 0
|
|
596
|
-
let char: u32 = 0
|
|
597
|
-
let depth: u32 = 0
|
|
598
|
-
let fdepth: u32 = 0
|
|
599
|
-
let instr: boolean = false
|
|
600
|
-
let i: u32 = 1
|
|
601
|
-
for (; i < u32(data.length - 1); i++) {
|
|
602
|
-
char = data.charCodeAt(i)
|
|
603
|
-
// This ignores [ and ] if they are inside a string.
|
|
604
|
-
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode) {
|
|
605
|
-
if (instr === true) {
|
|
606
|
-
instr = false
|
|
607
|
-
} else {
|
|
608
|
-
instr = true
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
if (instr === false) {
|
|
612
|
-
if (char === commaCode && depth === 0 && fdepth === 0) {
|
|
613
|
-
//console.log('Normal chunk: ' + data.slice(lastPos + 1, i))
|
|
614
|
-
result.push(parseUnknown(data.slice(lastPos + 1, i).trim()))
|
|
615
|
-
lastPos = i
|
|
616
|
-
} else if (char === lbracketCode) depth++
|
|
617
|
-
else if (char === rbracketCode) fdepth++
|
|
618
|
-
else if (depth !== 0 && depth === fdepth) {
|
|
619
|
-
//console.log('Deep chunk: ' + data.slice(lastPos + 1, i))
|
|
620
|
-
result.push(unknown.wrap(parseUnknownArray(data.slice(lastPos + 1, i).trim())))
|
|
621
|
-
// Reset the depth
|
|
622
|
-
depth = 0
|
|
623
|
-
fdepth = 0
|
|
624
|
-
// Set new lastPos
|
|
625
|
-
lastPos = i
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
// console.log('Last chunk: ' + data.slice(lastPos + 1, data.length - 1).trim())
|
|
630
|
-
result.push(parseUnknown(data.slice(lastPos + 1, data.length - 1).trim()))
|
|
631
|
-
return result
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
export function parseArrayArray<T extends Array<unknown>>(data: string): T {
|
|
635
|
-
const result = instantiate<T>()
|
|
636
|
-
if (data.length === 2) return result
|
|
637
|
-
let lastPos: i32 = -1
|
|
638
|
-
let char: u32 = 0
|
|
639
|
-
let depth: u32 = 0
|
|
640
|
-
let fdepth: u32 = 0
|
|
641
|
-
let instr: u32 = 0
|
|
642
|
-
for (let i: u32 = 1; i < u32(data.length - 1); i++) {
|
|
643
|
-
char = data.charCodeAt(i)
|
|
644
|
-
// This ignores [ and ] if they are inside a string.
|
|
645
|
-
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode) instr = instr ? 0 : 1
|
|
646
|
-
// This gets the depth of the array.
|
|
647
|
-
if (instr === 0) {
|
|
648
|
-
if (char === lbracketCode) depth++
|
|
649
|
-
if (char === rbracketCode) fdepth++
|
|
650
|
-
// If the depth and found depth are equal, that is an array. Push it.
|
|
651
|
-
if (depth !== 0 && depth === fdepth) {
|
|
652
|
-
result.push(
|
|
653
|
-
// @ts-ignore
|
|
654
|
-
parseArray<valueof<T>>(data.slice(lastPos + 2, i + 1))
|
|
655
|
-
)
|
|
656
|
-
// Reset the depth
|
|
657
|
-
depth = 0
|
|
658
|
-
fdepth = 0
|
|
659
|
-
// Set new lastPos
|
|
660
|
-
lastPos = i
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
// Return the final array
|
|
665
|
-
return result
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
export function parseObject<T>(data: string): T {
|
|
669
|
-
//console.log('Data ' + data)
|
|
670
|
-
const len: u32 = data.length - 1
|
|
671
|
-
let schema: T
|
|
672
|
-
const result = new Map<string, string>()
|
|
673
|
-
let lastPos: u32 = 1
|
|
674
|
-
let key: string = ''
|
|
675
|
-
let instr: u32 = 0
|
|
676
|
-
let char: u32 = 0
|
|
677
|
-
let depth: u32 = 0
|
|
678
|
-
let fdepth: u32 = 0
|
|
679
|
-
for (let i: u32 = 1; i < len; i++) {
|
|
680
|
-
char = data.charCodeAt(i)
|
|
681
|
-
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode) instr = (instr ? 0 : 1)
|
|
682
|
-
else if (instr === 0) {
|
|
683
|
-
if (char === lcbracketCode || char === lbracketCode) depth++
|
|
684
|
-
if (char === rcbracketCode || char === rbracketCode) fdepth++
|
|
685
|
-
}
|
|
686
|
-
if (depth !== 0 && depth === fdepth) {
|
|
687
|
-
//console.log(`Deep: ${prependType(data.slice(lastPos + 1, i + 1))}`)
|
|
688
|
-
result.set(key, data.slice(lastPos + 1, i + 1).trim())
|
|
689
|
-
// Reset the depth
|
|
690
|
-
depth = 0
|
|
691
|
-
fdepth = 0
|
|
692
|
-
// Set new lastPos
|
|
693
|
-
lastPos = i + 1
|
|
694
|
-
}
|
|
695
|
-
if (depth === 0) {
|
|
696
|
-
if (char === colonCode) {
|
|
697
|
-
//console.log(`Key: ${prependType(data.slice(lastPos + 1, i - 1))}`)
|
|
698
|
-
key = data.slice(lastPos + 1, i - 1).trim()
|
|
699
|
-
lastPos = i
|
|
700
|
-
}
|
|
701
|
-
else if (char === commaCode) {
|
|
702
|
-
//console.log(`Value: ${prependType(data.slice(lastPos + 1, i))}`)
|
|
703
|
-
if ((i - lastPos) > 0) result.set(key, data.slice(lastPos + 1, i).trim())
|
|
704
|
-
lastPos = i + 1
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
//console.log(`Trailing: ${prependType(data.slice(lastPos + 1, len))}\n\t\sValid: ${data.slice(lastPos + 1, len).length > 0}`)
|
|
709
|
-
|
|
710
|
-
if ((len - lastPos) > 0) result.set(key, data.slice(lastPos + 1, len).trim())
|
|
711
|
-
// @ts-ignore
|
|
712
|
-
return schema.__decode(result)
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
export function parseDynamicObject(data: string): DynamicObject {
|
|
716
|
-
const len: u32 = data.length - 1
|
|
717
|
-
if (len === 1) return new DynamicObject()
|
|
718
|
-
const result = new Map<string, unknown>()
|
|
719
|
-
let lastPos: u32 = 1
|
|
720
|
-
let key: string = ''
|
|
721
|
-
let instr: u32 = 0
|
|
722
|
-
let char: u32 = 0
|
|
723
|
-
let depth: u32 = 0
|
|
724
|
-
let fdepth: u32 = 0
|
|
725
|
-
for (let i: u32 = 1; i < len; i++) {
|
|
726
|
-
char = data.charCodeAt(i)
|
|
727
|
-
if (char === quoteCode && data.charCodeAt(i - 1) !== fwd_slashCode) instr = (instr ? 0 : 1)
|
|
728
|
-
else if (instr === 0) {
|
|
729
|
-
if (char === lcbracketCode || char === lbracketCode) depth++
|
|
730
|
-
if (char === rcbracketCode || char === rbracketCode) fdepth++
|
|
731
|
-
}
|
|
732
|
-
if (depth !== 0 && depth === fdepth) {
|
|
733
|
-
result.set(key, unknown.wrap(data.slice(lastPos + 1, i + 1).trim()))
|
|
734
|
-
// Reset the depth
|
|
735
|
-
depth = 0
|
|
736
|
-
fdepth = 0
|
|
737
|
-
// Set new lastPos
|
|
738
|
-
lastPos = i + 2
|
|
739
|
-
}
|
|
740
|
-
if (depth === 0) {
|
|
741
|
-
if (char === colonCode) {
|
|
742
|
-
key = data.slice(lastPos + 1, i - 1).trim()
|
|
743
|
-
lastPos = i + 1
|
|
744
|
-
}
|
|
745
|
-
else if (char === commaCode) {
|
|
746
|
-
if ((i - lastPos) > 0) result.set(key, unknown.wrap(data.slice(lastPos + 1, i).trim()))
|
|
747
|
-
lastPos = i + 1
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
|
|
752
|
-
if ((len - lastPos) > 0) result.set(key, unknown.wrap(data.slice(lastPos + 1, len - 1).trim()))
|
|
753
|
-
const o = new DynamicObject()
|
|
754
|
-
// @ts-ignore
|
|
755
|
-
o.__data = result
|
|
756
|
-
return o
|
|
757
|
-
}
|
|
1
|
+
import { StringSink } from "as-string-sink/assembly";
|
|
2
|
+
import { Variant } from "as-variant/assembly";
|
|
3
|
+
import {
|
|
4
|
+
backSlashCode,
|
|
5
|
+
colonCode,
|
|
6
|
+
commaCode,
|
|
7
|
+
leftBraceCode,
|
|
8
|
+
leftBracketCode,
|
|
9
|
+
quoteCode,
|
|
10
|
+
rightBraceCode,
|
|
11
|
+
rightBracketCode
|
|
12
|
+
} from "./chars";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* JSON Encoder/Decoder for AssemblyScript
|
|
16
|
+
*/
|
|
17
|
+
export namespace JSON {
|
|
18
|
+
export type _Variant = Variant;
|
|
19
|
+
/**
|
|
20
|
+
* Stringifies valid JSON data.
|
|
21
|
+
* ```js
|
|
22
|
+
* JSON.stringify<T>(data)
|
|
23
|
+
* ```
|
|
24
|
+
* @param data T
|
|
25
|
+
* @returns string
|
|
26
|
+
*/
|
|
27
|
+
export function stringify<T = Nullable | null>(data: T): string {
|
|
28
|
+
// String
|
|
29
|
+
if (isString(data)) {
|
|
30
|
+
return serializeString(<string>data);
|
|
31
|
+
}
|
|
32
|
+
// Boolean
|
|
33
|
+
else if (isBoolean(data)) {
|
|
34
|
+
return data ? "true" : "false";
|
|
35
|
+
}
|
|
36
|
+
// Null
|
|
37
|
+
else if (isNullable<T>() && data == null) {
|
|
38
|
+
return "null";
|
|
39
|
+
}
|
|
40
|
+
// Integers/Floats
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
return data.toString();
|
|
45
|
+
}
|
|
46
|
+
// Class-Based serialization
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
else if (isDefined(data.__JSON_Serialize)) {
|
|
49
|
+
//@ts-ignore
|
|
50
|
+
return data.__JSON_Serialize();
|
|
51
|
+
}
|
|
52
|
+
// ArrayLike
|
|
53
|
+
else if (isArrayLike(data)) {
|
|
54
|
+
let result = new StringSink("[");
|
|
55
|
+
if (data.length == 0) return "[]";
|
|
56
|
+
for (let i = 0; i < data.length - 1; i++) {
|
|
57
|
+
result.write(stringify(unchecked(data[i])));
|
|
58
|
+
result.write(",");
|
|
59
|
+
}
|
|
60
|
+
result.write(stringify(unchecked(data[data.length - 1])));
|
|
61
|
+
result.write("]");
|
|
62
|
+
return result.toString();
|
|
63
|
+
} else {
|
|
64
|
+
return "null";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parses valid JSON strings into their original format.
|
|
69
|
+
* ```js
|
|
70
|
+
* JSON.parse<T>(data)
|
|
71
|
+
* ```
|
|
72
|
+
* @param data string
|
|
73
|
+
* @returns T
|
|
74
|
+
*/
|
|
75
|
+
export function parse<T = Variant>(data: string): T {
|
|
76
|
+
let type!: T;
|
|
77
|
+
if (isString<T>()) {
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
return parseString(data);
|
|
80
|
+
} else if (isBoolean<T>()) {
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
return parseBoolean<T>(data);
|
|
83
|
+
} else if (isFloat<T>() || isInteger<T>()) {
|
|
84
|
+
return parseNumber<T>(data);
|
|
85
|
+
} else if (isArrayLike<T>()) {
|
|
86
|
+
return parseArray<T>(data);
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
89
|
+
const len: u32 = data.length - 1
|
|
90
|
+
let schema!: T
|
|
91
|
+
const result = new Map<string, string>()
|
|
92
|
+
let lastPos: u32 = 1
|
|
93
|
+
let key: string = ''
|
|
94
|
+
let instr: u32 = 0
|
|
95
|
+
let char: u32 = 0
|
|
96
|
+
let depth: u32 = 0
|
|
97
|
+
let fdepth: u32 = 0
|
|
98
|
+
for (let i: u32 = 1; i < len; i++) {
|
|
99
|
+
char = data.charCodeAt(i)
|
|
100
|
+
if (char === "\"".charCodeAt(0) && data.charCodeAt(i - 1) !== "/".charCodeAt(0)) instr = (instr ? 0 : 1)
|
|
101
|
+
else if (instr === 0) {
|
|
102
|
+
if (char === leftBraceCode || char === leftBracketCode) depth++
|
|
103
|
+
if (char === rightBraceCode || char === rightBracketCode) fdepth++
|
|
104
|
+
}
|
|
105
|
+
if (depth !== 0 && depth === fdepth) {
|
|
106
|
+
result.set(key, data.slice(lastPos + 1, i + 1))
|
|
107
|
+
// Reset the depth
|
|
108
|
+
depth = 0
|
|
109
|
+
fdepth = 0
|
|
110
|
+
// Set new lastPos
|
|
111
|
+
lastPos = i + 1
|
|
112
|
+
}
|
|
113
|
+
if (depth === 0) {
|
|
114
|
+
if (char === colonCode) {
|
|
115
|
+
key = data.slice(lastPos + 1, i - 1)
|
|
116
|
+
lastPos = i
|
|
117
|
+
}
|
|
118
|
+
else if (char === commaCode) {
|
|
119
|
+
if ((i - lastPos) > 0) result.set(key, data.slice(lastPos + 1, i))
|
|
120
|
+
lastPos = i + 1
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if ((len - lastPos) > 0) result.set(key, data.slice(lastPos + 1, len))
|
|
126
|
+
// @ts-ignore
|
|
127
|
+
return schema.__JSON_Deserialize(result)
|
|
128
|
+
} else {
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
@inline
|
|
137
|
+
function serializeString(data: string): string {
|
|
138
|
+
return '"' + data.replaceAll('"', '\\"') + '"';
|
|
139
|
+
}
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
@inline
|
|
142
|
+
function parseString(data: string): string {
|
|
143
|
+
return data.slice(1, data.length - 1).replaceAll('\\"', '"');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
@inline
|
|
148
|
+
function parseBoolean<T extends boolean>(data: string): T {
|
|
149
|
+
if (data.length > 3 && data.startsWith("true")) return <T>true;
|
|
150
|
+
else if (data.length > 4 && data.startsWith("false")) return <T>false;
|
|
151
|
+
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
|
|
152
|
+
}
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
@inline
|
|
155
|
+
function parseNumber<T>(data: string): T {
|
|
156
|
+
let type: T;
|
|
157
|
+
// @ts-ignore
|
|
158
|
+
if (type instanceof f64) return F64.parseFloat(data);
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
else if (type instanceof f32) return F32.parseFloat(data);
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
else if (type instanceof i32) return I32.parseInt(data);
|
|
163
|
+
// @ts-ignore
|
|
164
|
+
else if (type instanceof u32) return U32.parseInt(data);
|
|
165
|
+
// @ts-ignore
|
|
166
|
+
else if (type instanceof u8) return U8.parseInt(data);
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
else if (type instanceof u16) return U16.parseInt(data);
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
else if (type instanceof i16) return I16.parseInt(data);
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
else if (type instanceof i8) return I8.parseInt(data);
|
|
173
|
+
else
|
|
174
|
+
throw new Error(
|
|
175
|
+
`JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// @ts-ignore
|
|
180
|
+
@inline
|
|
181
|
+
export function parseNumberArray<T>(data: string): T {
|
|
182
|
+
const result = instantiate<T>();
|
|
183
|
+
if (data.length == 0) return result;
|
|
184
|
+
let lastPos: u32 = 1;
|
|
185
|
+
let i: u32 = 1;
|
|
186
|
+
let char: u32 = 0;
|
|
187
|
+
for (; i < u32(data.length - 1); i++) {
|
|
188
|
+
char = data.charCodeAt(i);
|
|
189
|
+
if (char == commaCode) {
|
|
190
|
+
// console.log(data.slice(lastPos, i))
|
|
191
|
+
// @ts-ignore
|
|
192
|
+
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i).trim()));
|
|
193
|
+
lastPos = ++i;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//console.log(data.slice(lastPos, data.length - 1))
|
|
197
|
+
// @ts-ignore
|
|
198
|
+
result.push(
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
parseNumber<valueof<T>>(data.slice(lastPos, data.length - 1).trimStart())
|
|
201
|
+
);
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// @ts-ignore
|
|
206
|
+
@inline
|
|
207
|
+
export function parseBooleanArray<T>(data: string): T {
|
|
208
|
+
const result = instantiate<T>();
|
|
209
|
+
if (data.length == 0) return result;
|
|
210
|
+
let lastPos: u32 = 1;
|
|
211
|
+
let i: u32 = 1;
|
|
212
|
+
let char: u32 = 0;
|
|
213
|
+
for (; i < u32(data.length - 1); i++) {
|
|
214
|
+
char = data.charCodeAt(i);
|
|
215
|
+
if (char == commaCode) {
|
|
216
|
+
// @ts-ignore
|
|
217
|
+
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i).trimStart()));
|
|
218
|
+
lastPos = ++i;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// @ts-ignore
|
|
222
|
+
result.push(
|
|
223
|
+
// @ts-ignore
|
|
224
|
+
parseBoolean<valueof<T>>(data.slice(lastPos, data.length - 1).trimStart())
|
|
225
|
+
);
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// @ts-ignore
|
|
230
|
+
@inline
|
|
231
|
+
export function parseArray<T>(data: string): T {
|
|
232
|
+
const result = instantiate<T>();
|
|
233
|
+
data = data.trim();
|
|
234
|
+
let len: u32 = data.length - 1;
|
|
235
|
+
let lastPos: u32 = 1;
|
|
236
|
+
let i: u32 = 1;
|
|
237
|
+
let char: u32 = 0;
|
|
238
|
+
// Is struct such as Object, or Array
|
|
239
|
+
let isStruct: boolean = false;
|
|
240
|
+
let isStr: boolean = false;
|
|
241
|
+
//let offset: u32 = 0;
|
|
242
|
+
// Depth for finding matching brackets
|
|
243
|
+
let inDepth: u32 = 0;
|
|
244
|
+
let outDepth: u32 = 0;
|
|
245
|
+
for (; i < len; i++) {
|
|
246
|
+
char = data.charCodeAt(i);
|
|
247
|
+
if (char == quoteCode && data.charCodeAt(i - 1) != backSlashCode)
|
|
248
|
+
isStr = !isStr;
|
|
249
|
+
if (char == leftBraceCode || char == leftBracketCode) {
|
|
250
|
+
inDepth++;
|
|
251
|
+
isStruct = true;
|
|
252
|
+
} else if (char == rightBraceCode || char == rightBracketCode) {
|
|
253
|
+
outDepth++;
|
|
254
|
+
isStruct = true;
|
|
255
|
+
}
|
|
256
|
+
if (!isStr) {
|
|
257
|
+
if (!isStruct) {
|
|
258
|
+
// This removes whitespace before and after an element
|
|
259
|
+
/*if (offset != 0 && isSpace(char)) {
|
|
260
|
+
lastPos++;
|
|
261
|
+
} else {
|
|
262
|
+
if (isSpace(char)) offset++;
|
|
263
|
+
}*/
|
|
264
|
+
// This checks to see if we are dealing with structures such as Objects and Arrays
|
|
265
|
+
if (char == commaCode) {
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i).trim()));
|
|
268
|
+
//offset = 0;
|
|
269
|
+
lastPos = i + 1;
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
if (inDepth == outDepth) {
|
|
273
|
+
i++;
|
|
274
|
+
//console.log(`Struct-${data.slice(lastPos, i).trim()}-`)
|
|
275
|
+
lastPos = i + 1;
|
|
276
|
+
inDepth = 0;
|
|
277
|
+
outDepth = 0;
|
|
278
|
+
isStruct = false;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/*char = data.charCodeAt(lastPos)
|
|
284
|
+
// Remove preceeding whitespace
|
|
285
|
+
while (isSpace(char)) {
|
|
286
|
+
lastPos++;
|
|
287
|
+
char = data.charCodeAt(lastPos);
|
|
288
|
+
}
|
|
289
|
+
char = data.charCodeAt(--len);
|
|
290
|
+
while (isSpace(char)) {
|
|
291
|
+
len--;
|
|
292
|
+
char = data.charCodeAt(len);
|
|
293
|
+
}*/
|
|
294
|
+
|
|
295
|
+
// @ts-ignore
|
|
296
|
+
// Handle empty arrays
|
|
297
|
+
data = data.slice(lastPos, len).trim();
|
|
298
|
+
// @ts-ignore
|
|
299
|
+
if (data.length != 0) result.push(JSON.parse<valueof<T>>(data));
|
|
300
|
+
//if (data.length != 0) console.log(`Trailing-${data.slice(lastPos, len).trim()}-`)
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function parseObject(data: string): void {
|
|
305
|
+
//const obj = instantiate<T>()
|
|
306
|
+
const result = new Array<string>();
|
|
307
|
+
let lastPos: u32 = 0;
|
|
308
|
+
let char: u32 = 0;
|
|
309
|
+
let i: u32 = 1;
|
|
310
|
+
let key: string = "";
|
|
311
|
+
let isKey: boolean = false;
|
|
312
|
+
for (; i < u32(data.length - 1); i++) {
|
|
313
|
+
char = data.charCodeAt(i);
|
|
314
|
+
if (isKey == false) {
|
|
315
|
+
if (char == colonCode) {
|
|
316
|
+
key = data.slice(lastPos + 2, i - 1);
|
|
317
|
+
//console.log(`Found Key: ${key}`);
|
|
318
|
+
result.push(key);
|
|
319
|
+
lastPos = ++i;
|
|
320
|
+
isKey = true;
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
if (char == commaCode) {
|
|
324
|
+
const val = data.slice(lastPos, i);
|
|
325
|
+
lastPos = i;
|
|
326
|
+
isKey = false;
|
|
327
|
+
//console.log(`Found Val: ${val}`)
|
|
328
|
+
result.push(val);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
result.push(data.slice(lastPos, data.length - 1));
|
|
333
|
+
//console.log(stringify(result))
|
|
334
|
+
//return obj
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
class Nullable { }
|