json-as 0.5.52 → 0.5.56
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/.github/workflows/nodejs.yml +25 -0
- package/LICENSE +1 -1
- package/README.md +0 -23
- package/asconfig.json +2 -2
- package/assembly/__benches__/as-json.ts +10 -64
- package/assembly/__tests__/as-json.spec.ts +4 -13
- package/assembly/src/chars.ts +52 -26
- package/assembly/src/json.ts +130 -204
- package/assembly/src/util.ts +175 -148
- package/assembly/test.ts +51 -72
- package/bench/benchmark.ts +206 -0
- package/bench/tsconfig.json +99 -0
- package/bench-results/INTEGER-PARSING.md +52 -0
- package/package.json +16 -10
- package/transform/lib/index.js +26 -11
- package/transform/package.json +1 -1
- package/transform/src/index.ts +44 -22
package/assembly/src/json.ts
CHANGED
|
@@ -20,8 +20,10 @@ import {
|
|
|
20
20
|
trueWord,
|
|
21
21
|
uCode,
|
|
22
22
|
emptyArrayWord,
|
|
23
|
+
falseWord,
|
|
23
24
|
} from "./chars";
|
|
24
25
|
import { snip_fast, unsafeCharCodeAt } from "./util";
|
|
26
|
+
import { Virtual } from "as-virtual/assembly";
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
29
|
* JSON Encoder/Decoder for AssemblyScript
|
|
@@ -35,14 +37,11 @@ export namespace JSON {
|
|
|
35
37
|
* @param data T
|
|
36
38
|
* @returns string
|
|
37
39
|
*/
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
@inline export function stringify<
|
|
40
|
-
T
|
|
41
|
-
>(data: T): string {
|
|
40
|
+
// @ts-ignore: Decorator
|
|
41
|
+
@inline export function stringify<T>(data: T): string {
|
|
42
42
|
// String
|
|
43
43
|
if (isString<T>() && data != null) {
|
|
44
|
-
|
|
45
|
-
return serializeString(data);
|
|
44
|
+
return serializeString(data as string);
|
|
46
45
|
} else if (isBoolean<T>()) {
|
|
47
46
|
return data ? "true" : "false";
|
|
48
47
|
} else if (isNullable<T>() && data == null) {
|
|
@@ -51,9 +50,9 @@ export namespace JSON {
|
|
|
51
50
|
} else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
|
|
52
51
|
// @ts-ignore
|
|
53
52
|
return data.toString();
|
|
54
|
-
// @ts-ignore
|
|
53
|
+
// @ts-ignore: Hidden function
|
|
55
54
|
} else if (isDefined(data.__JSON_Serialize)) {
|
|
56
|
-
// @ts-ignore
|
|
55
|
+
// @ts-ignore: Hidden function
|
|
57
56
|
return data.__JSON_Serialize();
|
|
58
57
|
} else if (data instanceof Date) {
|
|
59
58
|
return data.toISOString();
|
|
@@ -110,10 +109,8 @@ export namespace JSON {
|
|
|
110
109
|
* @returns T
|
|
111
110
|
*/
|
|
112
111
|
|
|
113
|
-
// @ts-ignore
|
|
114
|
-
@inline export function parse<
|
|
115
|
-
T
|
|
116
|
-
>(data: string): T {
|
|
112
|
+
// @ts-ignore: Decorator
|
|
113
|
+
@inline export function parse<T>(data: string): T {
|
|
117
114
|
let type: T;
|
|
118
115
|
if (isString<T>()) {
|
|
119
116
|
// @ts-ignore
|
|
@@ -143,176 +140,136 @@ export namespace JSON {
|
|
|
143
140
|
);
|
|
144
141
|
}
|
|
145
142
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// @ts-ignore: Decorator
|
|
146
|
+
@global @inline function __parseObjectValue<T>(data: string): T {
|
|
147
|
+
let type: T;
|
|
148
|
+
if (isString<T>()) {
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
let result = "";
|
|
151
|
+
let last = 0;
|
|
152
|
+
for (let i = 0; i < data.length; i++) {
|
|
153
|
+
// \\"
|
|
154
|
+
if (unsafeCharCodeAt(data, i) === backSlashCode) {
|
|
155
|
+
const char = unsafeCharCodeAt(data, ++i);
|
|
156
|
+
result += data.slice(last, i - 1);
|
|
157
|
+
if (char === 34) {
|
|
158
|
+
result += '"';
|
|
159
|
+
last = ++i;
|
|
160
|
+
} else if (char === 110) {
|
|
161
|
+
result += "\n";
|
|
162
|
+
last = ++i;
|
|
163
|
+
// 92 98 114 116 102 117
|
|
164
|
+
} else if (char >= 92 && char <= 117) {
|
|
165
|
+
if (char === 92) {
|
|
166
|
+
result += "\\";
|
|
163
167
|
last = ++i;
|
|
164
|
-
} else if (char ===
|
|
165
|
-
result += "\
|
|
168
|
+
} else if (char === 98) {
|
|
169
|
+
result += "\b";
|
|
170
|
+
last = ++i;
|
|
171
|
+
} else if (char === 102) {
|
|
172
|
+
result += "\f";
|
|
173
|
+
last = ++i;
|
|
174
|
+
} else if (char === 114) {
|
|
175
|
+
result += "\r";
|
|
176
|
+
last = ++i;
|
|
177
|
+
} else if (char === 116) {
|
|
178
|
+
result += "\t";
|
|
179
|
+
last = ++i;
|
|
180
|
+
} else if (
|
|
181
|
+
char === 117 &&
|
|
182
|
+
load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
|
|
183
|
+
27584753879220272
|
|
184
|
+
) {
|
|
185
|
+
result += "\u000b";
|
|
186
|
+
i += 4;
|
|
166
187
|
last = ++i;
|
|
167
|
-
// 92 98 114 116 102 117
|
|
168
|
-
} else if (char >= 92 && char <= 117) {
|
|
169
|
-
if (char === 92) {
|
|
170
|
-
result += "\\";
|
|
171
|
-
last = ++i;
|
|
172
|
-
} else if (char === 98) {
|
|
173
|
-
result += "\b";
|
|
174
|
-
last = ++i;
|
|
175
|
-
} else if (char === 102) {
|
|
176
|
-
result += "\f";
|
|
177
|
-
last = ++i;
|
|
178
|
-
} else if (char === 114) {
|
|
179
|
-
result += "\r";
|
|
180
|
-
last = ++i;
|
|
181
|
-
} else if (char === 116) {
|
|
182
|
-
result += "\t";
|
|
183
|
-
last = ++i;
|
|
184
|
-
} else if (
|
|
185
|
-
char === 117 &&
|
|
186
|
-
load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
|
|
187
|
-
27584753879220272
|
|
188
|
-
) {
|
|
189
|
-
result += "\u000b";
|
|
190
|
-
i += 4;
|
|
191
|
-
last = ++i;
|
|
192
|
-
}
|
|
193
188
|
}
|
|
194
189
|
}
|
|
195
190
|
}
|
|
196
|
-
result += data.slice(last);
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
return result;
|
|
199
|
-
} else if (isBoolean<T>()) {
|
|
200
|
-
// @ts-ignore
|
|
201
|
-
return parseBoolean<T>(data);
|
|
202
|
-
} else if (isFloat<T>() || isInteger<T>()) {
|
|
203
|
-
return parseNumber<T>(data);
|
|
204
|
-
} else if (isArrayLike<T>()) {
|
|
205
|
-
// @ts-ignore
|
|
206
|
-
return parseArray<T>(data);
|
|
207
|
-
// @ts-ignore
|
|
208
|
-
} else if (isNullable<T>() && data == "null") {
|
|
209
|
-
// @ts-ignore
|
|
210
|
-
return null;
|
|
211
|
-
// @ts-ignore
|
|
212
|
-
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
213
|
-
return parseObject<T>(data.trimStart());
|
|
214
|
-
} else if (idof<nonnull<T>>() == idof<Date>()) {
|
|
215
|
-
// @ts-ignore
|
|
216
|
-
return Date.fromString(data);
|
|
217
|
-
} else {
|
|
218
|
-
// @ts-ignore
|
|
219
|
-
throw new Error(
|
|
220
|
-
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
221
|
-
);
|
|
222
191
|
}
|
|
192
|
+
result += data.slice(last);
|
|
193
|
+
// @ts-ignore
|
|
194
|
+
return result;
|
|
195
|
+
} else if (isBoolean<T>()) {
|
|
196
|
+
// @ts-ignore
|
|
197
|
+
return parseBoolean<T>(data);
|
|
198
|
+
} else if (isFloat<T>() || isInteger<T>()) {
|
|
199
|
+
return parseNumber<T>(data);
|
|
200
|
+
} else if (isArrayLike<T>()) {
|
|
201
|
+
// @ts-ignore
|
|
202
|
+
return parseArray<T>(data);
|
|
203
|
+
// @ts-ignore
|
|
204
|
+
} else if (isNullable<T>() && data == "null") {
|
|
205
|
+
// @ts-ignore
|
|
206
|
+
return null;
|
|
207
|
+
// @ts-ignore
|
|
208
|
+
} else if (isDefined(type.__JSON_Set_Key)) {
|
|
209
|
+
return parseObject<T>(data.trimStart());
|
|
210
|
+
} else if (idof<nonnull<T>>() == idof<Date>()) {
|
|
211
|
+
// @ts-ignore
|
|
212
|
+
return Date.fromString(data);
|
|
213
|
+
} else {
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
throw new Error(
|
|
216
|
+
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
|
|
217
|
+
);
|
|
223
218
|
}
|
|
224
219
|
}
|
|
225
220
|
|
|
226
|
-
// @ts-ignore
|
|
227
|
-
@inline function serializeString(
|
|
228
|
-
|
|
229
|
-
): string {
|
|
230
|
-
// @ts-ignore
|
|
231
|
-
//if (data.length === 0) return "\"\"";
|
|
232
|
-
/*
|
|
233
|
-
let char: i32 = 0;
|
|
234
|
-
if (data.length === 1) {
|
|
235
|
-
char = unsafeCharCodeAt(data, 0);
|
|
236
|
-
if (char === 34) {
|
|
237
|
-
return "\\\"";
|
|
238
|
-
} else if (char === 92) {
|
|
239
|
-
return "\\n";
|
|
240
|
-
} else if (char <= 13 && char >= 8) {
|
|
241
|
-
switch (char) {
|
|
242
|
-
case 0x5C: {
|
|
243
|
-
return "\\\\";
|
|
244
|
-
}
|
|
245
|
-
case 0x08: {
|
|
246
|
-
return "\\b";
|
|
247
|
-
}
|
|
248
|
-
case 0x0D: {
|
|
249
|
-
return "\\r";
|
|
250
|
-
}
|
|
251
|
-
case 0x09: {
|
|
252
|
-
return "\\t";
|
|
253
|
-
}
|
|
254
|
-
case 0x0C: {
|
|
255
|
-
return "\\f";
|
|
256
|
-
}
|
|
257
|
-
case 0x0B: {
|
|
258
|
-
return "\\u000b";
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
} else {
|
|
262
|
-
return data;
|
|
263
|
-
}
|
|
264
|
-
}*/
|
|
265
|
-
|
|
266
|
-
let result = '"';
|
|
221
|
+
// @ts-ignore: Decorator
|
|
222
|
+
@inline export function serializeString(data: string): string {
|
|
223
|
+
let result = new StringSink('"');
|
|
267
224
|
|
|
268
225
|
let last: i32 = 0;
|
|
269
226
|
// @ts-ignore
|
|
270
227
|
for (let i = 0; i < data.length; i++) {
|
|
271
228
|
const char = unsafeCharCodeAt(<string>data, i);
|
|
272
229
|
if (char === 34 || char === 92) {
|
|
273
|
-
result
|
|
230
|
+
result.write(<string>data, last, i);
|
|
231
|
+
result.writeCodePoint(backSlashCode);
|
|
274
232
|
last = i;
|
|
275
233
|
//i++;
|
|
276
234
|
} else if (char <= 13 && char >= 8) {
|
|
277
|
-
result
|
|
235
|
+
result.write(<string>data, last, i);
|
|
278
236
|
last = i + 1;
|
|
279
237
|
switch (char) {
|
|
280
238
|
case 8: {
|
|
281
|
-
result
|
|
239
|
+
result.write("\\b");
|
|
282
240
|
break;
|
|
283
241
|
}
|
|
284
242
|
case 9: {
|
|
285
|
-
result
|
|
243
|
+
result.write("\\t");
|
|
286
244
|
break;
|
|
287
245
|
}
|
|
288
246
|
case 10: {
|
|
289
|
-
result
|
|
247
|
+
result.write("\\n");
|
|
290
248
|
break;
|
|
291
249
|
}
|
|
292
250
|
case 11: {
|
|
293
|
-
result
|
|
251
|
+
result.write("\\x0B"); // \\u000b
|
|
294
252
|
break;
|
|
295
253
|
}
|
|
296
254
|
case 12: {
|
|
297
|
-
result
|
|
255
|
+
result.write("\\f");
|
|
298
256
|
break;
|
|
299
257
|
}
|
|
300
258
|
case 13: {
|
|
301
|
-
result
|
|
259
|
+
result.write("\\r");
|
|
302
260
|
break;
|
|
303
261
|
}
|
|
304
262
|
}
|
|
305
263
|
}
|
|
306
264
|
}
|
|
307
265
|
if (result.length === 1) return '"' + data + '"';
|
|
308
|
-
else result
|
|
309
|
-
|
|
266
|
+
else result.write(<string>data, last);
|
|
267
|
+
result.write("\"");
|
|
268
|
+
return result.toString();
|
|
310
269
|
}
|
|
311
270
|
|
|
312
|
-
// @ts-ignore
|
|
313
|
-
@inline function parseString(
|
|
314
|
-
data: string
|
|
315
|
-
): string {
|
|
271
|
+
// @ts-ignore: Decorator
|
|
272
|
+
@inline function parseString(data: string): string {
|
|
316
273
|
let result = "";
|
|
317
274
|
let last = 1;
|
|
318
275
|
for (let i = 1; i < data.length - 1; i++) {
|
|
@@ -375,16 +332,14 @@ export namespace JSON {
|
|
|
375
332
|
return result;
|
|
376
333
|
}
|
|
377
334
|
|
|
378
|
-
// @ts-ignore
|
|
379
|
-
@inline function parseBoolean<
|
|
380
|
-
T extends boolean
|
|
381
|
-
>(data: string): T {
|
|
335
|
+
// @ts-ignore: Decorator
|
|
336
|
+
@inline function parseBoolean<T extends boolean>(data: string): T {
|
|
382
337
|
if (data.length > 3 && data.startsWith("true")) return <T>true;
|
|
383
338
|
else if (data.length > 4 && data.startsWith("false")) return <T>false;
|
|
384
339
|
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
|
|
385
340
|
}
|
|
386
341
|
|
|
387
|
-
// @ts-ignore
|
|
342
|
+
// @ts-ignore: Decorator
|
|
388
343
|
@inline export function parseNumber<T>(data: string): T {
|
|
389
344
|
if (isInteger<T>()) {
|
|
390
345
|
// @ts-ignore
|
|
@@ -398,25 +353,24 @@ export namespace JSON {
|
|
|
398
353
|
else if (type instanceof f32) return f32.parse(data);
|
|
399
354
|
}
|
|
400
355
|
|
|
401
|
-
// @ts-ignore
|
|
356
|
+
// @ts-ignore: Decorator
|
|
402
357
|
@inline function parseObject<T>(data: string): T {
|
|
403
358
|
let schema: nonnull<T> = changetype<nonnull<T>>(
|
|
404
359
|
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
405
360
|
);
|
|
406
|
-
let key =
|
|
361
|
+
let key = Virtual.createEmpty<string>();
|
|
407
362
|
let isKey = false;
|
|
408
363
|
let depth = 0;
|
|
409
|
-
let char = 0;
|
|
410
364
|
let outerLoopIndex = 1;
|
|
411
365
|
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
412
|
-
char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
366
|
+
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
413
367
|
if (char === leftBracketCode) {
|
|
414
368
|
for (
|
|
415
369
|
let arrayValueIndex = outerLoopIndex;
|
|
416
370
|
arrayValueIndex < data.length - 1;
|
|
417
371
|
arrayValueIndex++
|
|
418
372
|
) {
|
|
419
|
-
char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
373
|
+
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
420
374
|
if (char === leftBracketCode) {
|
|
421
375
|
depth++;
|
|
422
376
|
} else if (char === rightBracketCode) {
|
|
@@ -424,10 +378,7 @@ export namespace JSON {
|
|
|
424
378
|
if (depth === 0) {
|
|
425
379
|
++arrayValueIndex;
|
|
426
380
|
// @ts-ignore
|
|
427
|
-
schema.__JSON_Set_Key(
|
|
428
|
-
key,
|
|
429
|
-
data.slice(outerLoopIndex, arrayValueIndex)
|
|
430
|
-
);
|
|
381
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, arrayValueIndex);
|
|
431
382
|
outerLoopIndex = arrayValueIndex;
|
|
432
383
|
isKey = false;
|
|
433
384
|
break;
|
|
@@ -440,7 +391,7 @@ export namespace JSON {
|
|
|
440
391
|
objectValueIndex < data.length - 1;
|
|
441
392
|
objectValueIndex++
|
|
442
393
|
) {
|
|
443
|
-
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
394
|
+
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
444
395
|
if (char === leftBraceCode) {
|
|
445
396
|
depth++;
|
|
446
397
|
} else if (char === rightBraceCode) {
|
|
@@ -448,10 +399,7 @@ export namespace JSON {
|
|
|
448
399
|
if (depth === 0) {
|
|
449
400
|
++objectValueIndex;
|
|
450
401
|
// @ts-ignore
|
|
451
|
-
schema.__JSON_Set_Key(
|
|
452
|
-
key,
|
|
453
|
-
data.slice(outerLoopIndex, objectValueIndex)
|
|
454
|
-
);
|
|
402
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, objectValueIndex);
|
|
455
403
|
outerLoopIndex = objectValueIndex;
|
|
456
404
|
isKey = false;
|
|
457
405
|
break;
|
|
@@ -464,20 +412,17 @@ export namespace JSON {
|
|
|
464
412
|
stringValueIndex < data.length - 1;
|
|
465
413
|
stringValueIndex++
|
|
466
414
|
) {
|
|
467
|
-
char = unsafeCharCodeAt(data, stringValueIndex);
|
|
415
|
+
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
468
416
|
if (
|
|
469
417
|
char === quoteCode &&
|
|
470
418
|
unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
|
|
471
419
|
) {
|
|
472
420
|
if (isKey === false) {
|
|
473
|
-
key
|
|
421
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
474
422
|
isKey = true;
|
|
475
423
|
} else {
|
|
476
424
|
// @ts-ignore
|
|
477
|
-
schema.__JSON_Set_Key(
|
|
478
|
-
key,
|
|
479
|
-
data.slice(outerLoopIndex, stringValueIndex)
|
|
480
|
-
);
|
|
425
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex, stringValueIndex);
|
|
481
426
|
isKey = false;
|
|
482
427
|
}
|
|
483
428
|
outerLoopIndex = ++stringValueIndex;
|
|
@@ -486,7 +431,7 @@ export namespace JSON {
|
|
|
486
431
|
}
|
|
487
432
|
} else if (char == nCode) {
|
|
488
433
|
// @ts-ignore
|
|
489
|
-
schema.__JSON_Set_Key(key, nullWord);
|
|
434
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, nullWord, 0, 4);
|
|
490
435
|
isKey = false;
|
|
491
436
|
} else if (
|
|
492
437
|
char === tCode &&
|
|
@@ -495,7 +440,7 @@ export namespace JSON {
|
|
|
495
440
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
496
441
|
) {
|
|
497
442
|
// @ts-ignore
|
|
498
|
-
schema.__JSON_Set_Key(key, trueWord);
|
|
443
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, trueWord, 0, 4);
|
|
499
444
|
isKey = false;
|
|
500
445
|
} else if (
|
|
501
446
|
char === fCode &&
|
|
@@ -505,18 +450,15 @@ export namespace JSON {
|
|
|
505
450
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
506
451
|
) {
|
|
507
452
|
// @ts-ignore
|
|
508
|
-
schema.__JSON_Set_Key(key,
|
|
453
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, falseWord, 0, 5);
|
|
509
454
|
isKey = false;
|
|
510
455
|
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
511
456
|
let numberValueIndex = ++outerLoopIndex;
|
|
512
457
|
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
513
|
-
char = unsafeCharCodeAt(data, numberValueIndex);
|
|
458
|
+
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
514
459
|
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
515
460
|
// @ts-ignore
|
|
516
|
-
schema.__JSON_Set_Key(
|
|
517
|
-
key,
|
|
518
|
-
data.slice(outerLoopIndex - 1, numberValueIndex)
|
|
519
|
-
);
|
|
461
|
+
schema.__JSON_Set_Key<Virtual<string>>(key, data, outerLoopIndex - 1, numberValueIndex);
|
|
520
462
|
outerLoopIndex = numberValueIndex;
|
|
521
463
|
isKey = false;
|
|
522
464
|
break;
|
|
@@ -527,10 +469,8 @@ export namespace JSON {
|
|
|
527
469
|
return schema;
|
|
528
470
|
}
|
|
529
471
|
|
|
530
|
-
// @ts-ignore
|
|
531
|
-
@inline function parseArray<
|
|
532
|
-
T extends unknown[]
|
|
533
|
-
>(data: string): T {
|
|
472
|
+
// @ts-ignore: Decorator
|
|
473
|
+
@inline function parseArray<T extends unknown[]>(data: string): T {
|
|
534
474
|
if (isString<valueof<T>>()) {
|
|
535
475
|
return <T>parseStringArray(data);
|
|
536
476
|
} else if (isBoolean<valueof<T>>()) {
|
|
@@ -558,10 +498,8 @@ export namespace JSON {
|
|
|
558
498
|
return unreachable();
|
|
559
499
|
}
|
|
560
500
|
|
|
561
|
-
// @ts-ignore
|
|
562
|
-
@inline function parseStringArray(
|
|
563
|
-
data: string
|
|
564
|
-
): string[] {
|
|
501
|
+
// @ts-ignore: Decorator
|
|
502
|
+
@inline function parseStringArray(data: string): string[] {
|
|
565
503
|
const result: string[] = [];
|
|
566
504
|
let lastPos = 0;
|
|
567
505
|
let instr = false;
|
|
@@ -579,15 +517,12 @@ export namespace JSON {
|
|
|
579
517
|
return result;
|
|
580
518
|
}
|
|
581
519
|
|
|
582
|
-
// @ts-ignore
|
|
583
|
-
@inline function parseBooleanArray<
|
|
584
|
-
T extends boolean[]
|
|
585
|
-
>(data: string): T {
|
|
520
|
+
// @ts-ignore: Decorator
|
|
521
|
+
@inline function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
586
522
|
const result = instantiate<T>();
|
|
587
523
|
let lastPos = 1;
|
|
588
|
-
let char = 0;
|
|
589
524
|
for (let i = 1; i < data.length - 1; i++) {
|
|
590
|
-
char = unsafeCharCodeAt(data, i);
|
|
525
|
+
const char = unsafeCharCodeAt(data, i);
|
|
591
526
|
/*// if char == "t" && i+3 == "e"
|
|
592
527
|
if (char === tCode && data.charCodeAt(i + 3) === eCode) {
|
|
593
528
|
//i += 3;
|
|
@@ -608,16 +543,13 @@ export namespace JSON {
|
|
|
608
543
|
return result;
|
|
609
544
|
}
|
|
610
545
|
|
|
611
|
-
// @ts-ignore
|
|
612
|
-
@inline function parseNumberArray<
|
|
613
|
-
T extends number[]
|
|
614
|
-
>(data: string): T {
|
|
546
|
+
// @ts-ignore: Decorator
|
|
547
|
+
@inline function parseNumberArray<T extends number[]>(data: string): T {
|
|
615
548
|
const result = instantiate<T>();
|
|
616
549
|
let lastPos = 0;
|
|
617
|
-
let char = 0;
|
|
618
550
|
let i = 1;
|
|
619
551
|
for (; i < data.length - 1; i++) {
|
|
620
|
-
char = unsafeCharCodeAt(data, i);
|
|
552
|
+
const char = unsafeCharCodeAt(data, i);
|
|
621
553
|
if ((lastPos === 0 && char >= 48 && char <= 57) || char === 45) {
|
|
622
554
|
lastPos = i;
|
|
623
555
|
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
|
|
@@ -626,7 +558,7 @@ export namespace JSON {
|
|
|
626
558
|
}
|
|
627
559
|
}
|
|
628
560
|
for (; i > lastPos - 1; i--) {
|
|
629
|
-
char = unsafeCharCodeAt(data, i);
|
|
561
|
+
const char = unsafeCharCodeAt(data, i);
|
|
630
562
|
if (char !== rightBracketCode) {
|
|
631
563
|
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
|
|
632
564
|
break;
|
|
@@ -635,12 +567,9 @@ export namespace JSON {
|
|
|
635
567
|
return result;
|
|
636
568
|
}
|
|
637
569
|
|
|
638
|
-
// @ts-ignore
|
|
639
|
-
@inline function parseArrayArray<
|
|
640
|
-
T extends unknown[][]
|
|
641
|
-
>(data: string): T {
|
|
570
|
+
// @ts-ignore: Decorator
|
|
571
|
+
@inline function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
642
572
|
const result = instantiate<T>();
|
|
643
|
-
let char = 0;
|
|
644
573
|
let lastPos = 0;
|
|
645
574
|
let depth = 0;
|
|
646
575
|
let i = 1;
|
|
@@ -648,7 +577,7 @@ export namespace JSON {
|
|
|
648
577
|
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
|
|
649
578
|
//i++;
|
|
650
579
|
for (; i < data.length - 1; i++) {
|
|
651
|
-
char = unsafeCharCodeAt(data, i);
|
|
580
|
+
const char = unsafeCharCodeAt(data, i);
|
|
652
581
|
if (char === leftBracketCode) {
|
|
653
582
|
if (depth === 0) {
|
|
654
583
|
lastPos = i;
|
|
@@ -666,16 +595,13 @@ export namespace JSON {
|
|
|
666
595
|
return result;
|
|
667
596
|
}
|
|
668
597
|
|
|
669
|
-
// @ts-ignore
|
|
670
|
-
@inline export function parseObjectArray<
|
|
671
|
-
T extends unknown[]
|
|
672
|
-
>(data: string): T {
|
|
598
|
+
// @ts-ignore: Decorator
|
|
599
|
+
@inline export function parseObjectArray<T extends unknown[]>(data: string): T {
|
|
673
600
|
const result = instantiate<T>();
|
|
674
|
-
let char = 0;
|
|
675
601
|
let lastPos: u32 = 1;
|
|
676
602
|
let depth: u32 = 0;
|
|
677
603
|
for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
|
|
678
|
-
char = unsafeCharCodeAt(data, pos);
|
|
604
|
+
const char = unsafeCharCodeAt(data, pos);
|
|
679
605
|
if (char === leftBraceCode) {
|
|
680
606
|
if (depth === 0) {
|
|
681
607
|
lastPos = pos;
|