json-as 0.7.2 → 0.8.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/assembly/__tests__/as-json.spec.ts +298 -2
- package/assembly/src/chars.ts +12 -1
- package/assembly/src/json.ts +133 -156
- package/assembly/src/util.ts +11 -3
- package/assembly/test.ts +18 -85
- package/package.json +1 -1
- package/transform/lib/index.js +94 -76
- package/transform/package.json +1 -1
- package/transform/src/index.ts +115 -85
package/assembly/src/json.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { StringSink } from "as-string-sink/assembly";
|
|
|
2
2
|
import { isSpace } from "util/string";
|
|
3
3
|
import {
|
|
4
4
|
aCode,
|
|
5
|
+
bCode,
|
|
5
6
|
eCode,
|
|
6
7
|
fCode,
|
|
7
8
|
lCode,
|
|
@@ -14,20 +15,24 @@ import {
|
|
|
14
15
|
backSlashCode,
|
|
15
16
|
colonCode,
|
|
16
17
|
commaCode,
|
|
18
|
+
forwardSlashCode,
|
|
17
19
|
leftBraceCode,
|
|
18
20
|
leftBracketCode,
|
|
19
|
-
newLineCode,
|
|
20
21
|
quoteCode,
|
|
21
22
|
rightBraceCode,
|
|
22
23
|
rightBracketCode,
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
backspaceCode,
|
|
26
|
+
carriageReturnCode,
|
|
27
|
+
tabCode,
|
|
28
|
+
formFeedCode,
|
|
29
|
+
newLineCode,
|
|
30
|
+
|
|
25
31
|
commaWord,
|
|
26
32
|
quoteWord,
|
|
27
33
|
|
|
28
34
|
leftBraceWord,
|
|
29
35
|
leftBracketWord,
|
|
30
|
-
rightBraceWord,
|
|
31
36
|
rightBracketWord,
|
|
32
37
|
emptyArrayWord,
|
|
33
38
|
|
|
@@ -35,7 +40,7 @@ import {
|
|
|
35
40
|
falseWord,
|
|
36
41
|
nullWord,
|
|
37
42
|
} from "./chars";
|
|
38
|
-
import { snip_fast, unsafeCharCodeAt } from "./util";
|
|
43
|
+
import { snip_fast, unsafeCharCodeAt, containsCodePoint } from "./util";
|
|
39
44
|
import { Virtual } from "as-virtual/assembly";
|
|
40
45
|
|
|
41
46
|
/**
|
|
@@ -68,7 +73,7 @@ export namespace JSON {
|
|
|
68
73
|
// @ts-ignore: Hidden function
|
|
69
74
|
return data.__JSON_Serialize();
|
|
70
75
|
} else if (data instanceof Date) {
|
|
71
|
-
return "
|
|
76
|
+
return `"${data.toISOString()}"`;
|
|
72
77
|
} else if (isArrayLike<T>()) {
|
|
73
78
|
// @ts-ignore
|
|
74
79
|
if (data.length == 0) {
|
|
@@ -100,11 +105,11 @@ export namespace JSON {
|
|
|
100
105
|
for (let i = 0; i < data.length - 1; i++) {
|
|
101
106
|
// @ts-ignore
|
|
102
107
|
result.write(JSON.stringify(unchecked(data[i])));
|
|
103
|
-
result.
|
|
108
|
+
result.writeCodePoint(commaCode);
|
|
104
109
|
}
|
|
105
110
|
// @ts-ignore
|
|
106
111
|
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
107
|
-
result.
|
|
112
|
+
result.writeCodePoint(rightBracketCode);
|
|
108
113
|
return result.toString();
|
|
109
114
|
}
|
|
110
115
|
} else if (data instanceof Map) {
|
|
@@ -112,14 +117,14 @@ export namespace JSON {
|
|
|
112
117
|
let keys = data.keys();
|
|
113
118
|
let values = data.values();
|
|
114
119
|
for (let i = 0; i < data.size; i++) {
|
|
115
|
-
result.write(serializeString(keys[i].toString()));
|
|
116
|
-
result.
|
|
117
|
-
result.write(JSON.stringify(values[i]));
|
|
120
|
+
result.write(serializeString(unchecked(keys[i]).toString()));
|
|
121
|
+
result.writeCodePoint(colonCode);
|
|
122
|
+
result.write(JSON.stringify(unchecked(values[i])));
|
|
118
123
|
if (i < data.size - 1) {
|
|
119
|
-
result.
|
|
124
|
+
result.writeCodePoint(commaCode);
|
|
120
125
|
}
|
|
121
126
|
}
|
|
122
|
-
result.
|
|
127
|
+
result.writeCodePoint(rightBraceCode);
|
|
123
128
|
return result.toString();
|
|
124
129
|
} else {
|
|
125
130
|
throw new Error(
|
|
@@ -194,11 +199,11 @@ export namespace JSON {
|
|
|
194
199
|
for (let i = 0; i < data.length - 1; i++) {
|
|
195
200
|
// @ts-ignore
|
|
196
201
|
result.write(JSON.stringify(unchecked(data[i])));
|
|
197
|
-
result.
|
|
202
|
+
result.writeCodePoint(commaCode);
|
|
198
203
|
}
|
|
199
204
|
// @ts-ignore
|
|
200
205
|
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
201
|
-
result.
|
|
206
|
+
result.writeCodePoint(rightBracketCode);
|
|
202
207
|
out = result.toString();
|
|
203
208
|
return;
|
|
204
209
|
}
|
|
@@ -254,51 +259,8 @@ export namespace JSON {
|
|
|
254
259
|
@global @inline function __parseObjectValue<T>(data: string, initializeDefaultValues: boolean): T {
|
|
255
260
|
let type: T;
|
|
256
261
|
if (isString<T>()) {
|
|
257
|
-
let result = "";
|
|
258
|
-
let last = 0;
|
|
259
|
-
for (let i = 0; i < data.length; i++) {
|
|
260
|
-
// \\"
|
|
261
|
-
if (unsafeCharCodeAt(data, i) === backSlashCode) {
|
|
262
|
-
const char = unsafeCharCodeAt(data, ++i);
|
|
263
|
-
result += data.slice(last, i - 1);
|
|
264
|
-
if (char === 34) {
|
|
265
|
-
result += '"';
|
|
266
|
-
last = ++i;
|
|
267
|
-
} else if (char === 110) {
|
|
268
|
-
result += "\n";
|
|
269
|
-
last = ++i;
|
|
270
|
-
// 92 98 114 116 102 117
|
|
271
|
-
} else if (char >= 92 && char <= 117) {
|
|
272
|
-
if (char === 92) {
|
|
273
|
-
result += "\\";
|
|
274
|
-
last = ++i;
|
|
275
|
-
} else if (char === 98) {
|
|
276
|
-
result += "\b";
|
|
277
|
-
last = ++i;
|
|
278
|
-
} else if (char === 102) {
|
|
279
|
-
result += "\f";
|
|
280
|
-
last = ++i;
|
|
281
|
-
} else if (char === 114) {
|
|
282
|
-
result += "\r";
|
|
283
|
-
last = ++i;
|
|
284
|
-
} else if (char === 116) {
|
|
285
|
-
result += "\t";
|
|
286
|
-
last = ++i;
|
|
287
|
-
} else if (
|
|
288
|
-
char === 117 &&
|
|
289
|
-
load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
|
|
290
|
-
27584753879220272
|
|
291
|
-
) {
|
|
292
|
-
result += "\u000b";
|
|
293
|
-
i += 4;
|
|
294
|
-
last = ++i;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
result += data.slice(last);
|
|
300
262
|
// @ts-ignore
|
|
301
|
-
return
|
|
263
|
+
return data;
|
|
302
264
|
} else if (isBoolean<T>()) {
|
|
303
265
|
// @ts-ignore
|
|
304
266
|
return parseBoolean<T>(data);
|
|
@@ -327,118 +289,133 @@ export namespace JSON {
|
|
|
327
289
|
|
|
328
290
|
// @ts-ignore: Decorator
|
|
329
291
|
@inline function serializeString(data: string): string {
|
|
330
|
-
|
|
292
|
+
if (data.length === 0) {
|
|
293
|
+
return quoteWord + quoteWord;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let result = new StringSink(quoteWord);
|
|
331
297
|
|
|
332
298
|
let last: i32 = 0;
|
|
333
299
|
for (let i = 0; i < data.length; i++) {
|
|
334
300
|
const char = unsafeCharCodeAt(<string>data, i);
|
|
335
|
-
if (char ===
|
|
301
|
+
if (char === quoteCode || char === backSlashCode) {
|
|
336
302
|
result.write(<string>data, last, i);
|
|
337
303
|
result.writeCodePoint(backSlashCode);
|
|
338
304
|
last = i;
|
|
339
|
-
} else if (char
|
|
305
|
+
} else if (char < 16) {
|
|
340
306
|
result.write(<string>data, last, i);
|
|
341
307
|
last = i + 1;
|
|
342
308
|
switch (char) {
|
|
343
|
-
case
|
|
309
|
+
case backspaceCode: {
|
|
344
310
|
result.write("\\b");
|
|
345
311
|
break;
|
|
346
312
|
}
|
|
347
|
-
case
|
|
313
|
+
case tabCode: {
|
|
348
314
|
result.write("\\t");
|
|
349
315
|
break;
|
|
350
316
|
}
|
|
351
|
-
case
|
|
317
|
+
case newLineCode: {
|
|
352
318
|
result.write("\\n");
|
|
353
319
|
break;
|
|
354
320
|
}
|
|
355
|
-
case
|
|
356
|
-
result.write("\\x0B"); // \\u000b
|
|
357
|
-
break;
|
|
358
|
-
}
|
|
359
|
-
case 12: {
|
|
321
|
+
case formFeedCode: {
|
|
360
322
|
result.write("\\f");
|
|
361
323
|
break;
|
|
362
324
|
}
|
|
363
|
-
case
|
|
325
|
+
case carriageReturnCode: {
|
|
364
326
|
result.write("\\r");
|
|
365
327
|
break;
|
|
366
328
|
}
|
|
329
|
+
default: {
|
|
330
|
+
// all chars 0-31 must be encoded as a four digit unicode escape sequence
|
|
331
|
+
// \u0000 to \u000f handled here
|
|
332
|
+
result.write("\\u000");
|
|
333
|
+
result.write(char.toString(16));
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
367
336
|
}
|
|
337
|
+
} else if (char < 32) {
|
|
338
|
+
result.write(<string>data, last, i);
|
|
339
|
+
last = i + 1;
|
|
340
|
+
// all chars 0-31 must be encoded as a four digit unicode escape sequence
|
|
341
|
+
// \u0010 to \u001f handled here
|
|
342
|
+
result.write("\\u00");
|
|
343
|
+
result.write(char.toString(16));
|
|
368
344
|
}
|
|
369
345
|
}
|
|
370
|
-
if (result.length === 1) {
|
|
371
|
-
return quoteWord + data + quoteWord;
|
|
372
|
-
}
|
|
373
346
|
result.write(<string>data, last);
|
|
374
|
-
result.
|
|
347
|
+
result.writeCodePoint(quoteCode);
|
|
375
348
|
return result.toString();
|
|
376
349
|
}
|
|
377
350
|
|
|
378
351
|
// @ts-ignore: Decorator
|
|
379
|
-
@inline function parseString(data: string): string {
|
|
380
|
-
|
|
381
|
-
let
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (unsafeCharCodeAt(data, i)
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
352
|
+
@inline function parseString(data: string, start: i32 = 0, end: i32 = 0): string {
|
|
353
|
+
end = end || data.length - 1;
|
|
354
|
+
let result = StringSink.withCapacity(end - start - 1);
|
|
355
|
+
let last = start + 1;
|
|
356
|
+
for (let i = last; i < end; i++) {
|
|
357
|
+
if (unsafeCharCodeAt(data, i) !== backSlashCode) {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const char = unsafeCharCodeAt(data, ++i);
|
|
361
|
+
result.write(data, last, i - 1);
|
|
362
|
+
switch (char) {
|
|
363
|
+
case quoteCode: {
|
|
388
364
|
result.writeCodePoint(quoteCode);
|
|
389
365
|
last = i + 1;
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case backSlashCode: {
|
|
369
|
+
result.writeCodePoint(backSlashCode);
|
|
370
|
+
last = i + 1;
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
case forwardSlashCode: {
|
|
374
|
+
result.writeCodePoint(forwardSlashCode);
|
|
375
|
+
last = i + 1;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case bCode: {
|
|
379
|
+
result.writeCodePoint(backspaceCode);
|
|
380
|
+
last = i + 1;
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case fCode: {
|
|
384
|
+
result.writeCodePoint(formFeedCode);
|
|
385
|
+
last = i + 1;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
case nCode: {
|
|
389
|
+
result.writeCodePoint(newLineCode);
|
|
390
|
+
last = i + 1;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
case rCode: {
|
|
394
|
+
result.writeCodePoint(carriageReturnCode);
|
|
395
|
+
last = i + 1;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
case tCode: {
|
|
399
|
+
result.writeCodePoint(tabCode);
|
|
400
|
+
last = i + 1;
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
case uCode: {
|
|
404
|
+
const code = u16.parse(data.slice(i + 1, i + 5), 16);
|
|
405
|
+
result.writeCodePoint(code);
|
|
406
|
+
i += 4;
|
|
407
|
+
last = i + 1;
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
default: {
|
|
411
|
+
throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
|
|
435
412
|
}
|
|
436
413
|
}
|
|
437
414
|
}
|
|
438
|
-
if (
|
|
439
|
-
result.write(data, last,
|
|
415
|
+
if (end > last) {
|
|
416
|
+
result.write(data, last, end);
|
|
440
417
|
}
|
|
441
|
-
return result.toString()
|
|
418
|
+
return result.toString()
|
|
442
419
|
}
|
|
443
420
|
|
|
444
421
|
// @ts-ignore: Decorator
|
|
@@ -491,7 +468,7 @@ export namespace JSON {
|
|
|
491
468
|
if (depth === 0) {
|
|
492
469
|
++arrayValueIndex;
|
|
493
470
|
// @ts-ignore
|
|
494
|
-
schema.__JSON_Set_Key
|
|
471
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex, arrayValueIndex, initializeDefaultValues);
|
|
495
472
|
outerLoopIndex = arrayValueIndex;
|
|
496
473
|
isKey = false;
|
|
497
474
|
break;
|
|
@@ -512,7 +489,7 @@ export namespace JSON {
|
|
|
512
489
|
if (depth === 0) {
|
|
513
490
|
++objectValueIndex;
|
|
514
491
|
// @ts-ignore
|
|
515
|
-
schema.__JSON_Set_Key
|
|
492
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues);
|
|
516
493
|
outerLoopIndex = objectValueIndex;
|
|
517
494
|
isKey = false;
|
|
518
495
|
break;
|
|
@@ -530,15 +507,19 @@ export namespace JSON {
|
|
|
530
507
|
if (char === backSlashCode && !escaping) {
|
|
531
508
|
escaping = true;
|
|
532
509
|
} else {
|
|
533
|
-
if (
|
|
534
|
-
char === quoteCode && !escaping
|
|
535
|
-
) {
|
|
510
|
+
if (char === quoteCode && !escaping) {
|
|
536
511
|
if (isKey === false) {
|
|
537
|
-
key
|
|
512
|
+
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
513
|
+
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
|
|
514
|
+
key.reinst(parseString(data, outerLoopIndex - 1, stringValueIndex));
|
|
515
|
+
} else {
|
|
516
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
517
|
+
}
|
|
538
518
|
isKey = true;
|
|
539
519
|
} else {
|
|
520
|
+
const value = parseString(data, outerLoopIndex - 1, stringValueIndex);
|
|
540
521
|
// @ts-ignore
|
|
541
|
-
schema.__JSON_Set_Key
|
|
522
|
+
schema.__JSON_Set_Key(key, value, 0, value.length, initializeDefaultValues);
|
|
542
523
|
isKey = false;
|
|
543
524
|
}
|
|
544
525
|
outerLoopIndex = ++stringValueIndex;
|
|
@@ -554,7 +535,7 @@ export namespace JSON {
|
|
|
554
535
|
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode
|
|
555
536
|
) {
|
|
556
537
|
// @ts-ignore
|
|
557
|
-
schema.__JSON_Set_Key
|
|
538
|
+
schema.__JSON_Set_Key(key, nullWord, 0, 4, initializeDefaultValues);
|
|
558
539
|
isKey = false;
|
|
559
540
|
} else if (
|
|
560
541
|
char === tCode &&
|
|
@@ -563,7 +544,7 @@ export namespace JSON {
|
|
|
563
544
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
564
545
|
) {
|
|
565
546
|
// @ts-ignore
|
|
566
|
-
schema.__JSON_Set_Key
|
|
547
|
+
schema.__JSON_Set_Key(key, trueWord, 0, 4, initializeDefaultValues);
|
|
567
548
|
isKey = false;
|
|
568
549
|
} else if (
|
|
569
550
|
char === fCode &&
|
|
@@ -573,7 +554,7 @@ export namespace JSON {
|
|
|
573
554
|
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
574
555
|
) {
|
|
575
556
|
// @ts-ignore
|
|
576
|
-
schema.__JSON_Set_Key
|
|
557
|
+
schema.__JSON_Set_Key(key, falseWord, 0, 5, initializeDefaultValues);
|
|
577
558
|
isKey = false;
|
|
578
559
|
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
579
560
|
let numberValueIndex = ++outerLoopIndex;
|
|
@@ -581,7 +562,7 @@ export namespace JSON {
|
|
|
581
562
|
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
582
563
|
if (char === commaCode || char === rightBraceCode || isSpace(char)) {
|
|
583
564
|
// @ts-ignore
|
|
584
|
-
schema.__JSON_Set_Key
|
|
565
|
+
schema.__JSON_Set_Key(key, data, outerLoopIndex - 1, numberValueIndex, initializeDefaultValues);
|
|
585
566
|
outerLoopIndex = numberValueIndex;
|
|
586
567
|
isKey = false;
|
|
587
568
|
break;
|
|
@@ -664,11 +645,17 @@ export namespace JSON {
|
|
|
664
645
|
char === quoteCode && !escaping
|
|
665
646
|
) {
|
|
666
647
|
if (isKey === false) {
|
|
667
|
-
key
|
|
648
|
+
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
649
|
+
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) {
|
|
650
|
+
key.reinst(parseString(data, outerLoopIndex - 1, stringValueIndex));
|
|
651
|
+
} else {
|
|
652
|
+
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
653
|
+
}
|
|
668
654
|
isKey = true;
|
|
669
655
|
} else {
|
|
670
656
|
if (isString<valueof<T>>()) {
|
|
671
|
-
|
|
657
|
+
const value = parseString(data, outerLoopIndex - 1, stringValueIndex);
|
|
658
|
+
map.set(parseMapKey<indexof<T>>(key), value);
|
|
672
659
|
}
|
|
673
660
|
isKey = false;
|
|
674
661
|
}
|
|
@@ -788,7 +775,7 @@ export namespace JSON {
|
|
|
788
775
|
lastPos = i;
|
|
789
776
|
} else {
|
|
790
777
|
instr = false;
|
|
791
|
-
result.push(parseString(data
|
|
778
|
+
result.push(parseString(data, lastPos, i));
|
|
792
779
|
}
|
|
793
780
|
}
|
|
794
781
|
escaping = false;
|
|
@@ -803,16 +790,6 @@ export namespace JSON {
|
|
|
803
790
|
let lastPos = 1;
|
|
804
791
|
for (let i = 1; i < data.length - 1; i++) {
|
|
805
792
|
const char = unsafeCharCodeAt(data, i);
|
|
806
|
-
/*// if char == "t" && i+3 == "e"
|
|
807
|
-
if (char === tCode && data.charCodeAt(i + 3) === eCode) {
|
|
808
|
-
//i += 3;
|
|
809
|
-
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
|
|
810
|
-
//i++;
|
|
811
|
-
} else if (char === fCode && data.charCodeAt(i + 4) === eCode) {
|
|
812
|
-
//i += 4;
|
|
813
|
-
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
|
|
814
|
-
//i++;
|
|
815
|
-
}*/
|
|
816
793
|
if (char === tCode || char === fCode) {
|
|
817
794
|
lastPos = i;
|
|
818
795
|
} else if (char === eCode) {
|
|
@@ -830,7 +807,7 @@ export namespace JSON {
|
|
|
830
807
|
let i = 1;
|
|
831
808
|
for (; i < data.length - 1; i++) {
|
|
832
809
|
const char = unsafeCharCodeAt(data, i);
|
|
833
|
-
if (
|
|
810
|
+
if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
|
|
834
811
|
lastPos = i;
|
|
835
812
|
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
|
|
836
813
|
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
|
package/assembly/src/util.ts
CHANGED
|
@@ -12,11 +12,11 @@ import { backSlashCode, quoteCode } from "./chars";
|
|
|
12
12
|
const result = new StringSink();
|
|
13
13
|
let instr = false;
|
|
14
14
|
for (let i = 0; i < data.length; i++) {
|
|
15
|
-
const char = data
|
|
15
|
+
const char = unsafeCharCodeAt(data, i);
|
|
16
16
|
if (instr === false && char === quoteCode) instr = true;
|
|
17
17
|
else if (
|
|
18
18
|
instr === true && char === quoteCode
|
|
19
|
-
&& data
|
|
19
|
+
&& unsafeCharCodeAt(data, i - 1) !== backSlashCode
|
|
20
20
|
) instr = false;
|
|
21
21
|
|
|
22
22
|
if (instr === false) {
|
|
@@ -347,4 +347,12 @@ import { backSlashCode, quoteCode } from "./chars";
|
|
|
347
347
|
return load<u16>(changetype<usize>(p1_data) + p1_start) == load<u16>(changetype<usize>(p2_data) + p2_start)
|
|
348
348
|
}
|
|
349
349
|
return memory.compare(changetype<usize>(p1_data) + p1_start, changetype<usize>(p2_data) + p2_start, p1_len) === 0;
|
|
350
|
-
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// @ts-ignore
|
|
353
|
+
@inline export function containsCodePoint(str: string, code: u32, start: i32, end: i32): bool {
|
|
354
|
+
for (let i = start; i <= end; i++) {
|
|
355
|
+
if (unsafeCharCodeAt(str, i) == code) return true;
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
package/assembly/test.ts
CHANGED
|
@@ -1,88 +1,21 @@
|
|
|
1
1
|
import { JSON } from "./src/json";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
pos: Vec3 | null;
|
|
18
|
-
isVerified: boolean;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const vec = new Vec3();
|
|
22
|
-
|
|
23
|
-
const player: Player = {
|
|
24
|
-
firstName: "Emmet",
|
|
25
|
-
lastName: "West",
|
|
26
|
-
lastActive: new Date(0),
|
|
27
|
-
age: 23,
|
|
28
|
-
pos: {
|
|
29
|
-
x: 3.4,
|
|
30
|
-
y: 1.2,
|
|
31
|
-
z: 8.3,
|
|
32
|
-
},
|
|
33
|
-
isVerified: true,
|
|
34
|
-
x: 1,
|
|
35
|
-
y: 3,
|
|
36
|
-
z: 3
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let out = "";
|
|
40
|
-
|
|
41
|
-
JSON.stringifyTo(vec, out);
|
|
42
|
-
|
|
43
|
-
console.log("Original: " + out);
|
|
44
|
-
//console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
|
|
45
|
-
console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));
|
|
46
|
-
|
|
47
|
-
console.log("Original: " + JSON.stringify(player));
|
|
48
|
-
//console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
|
|
49
|
-
console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"first name":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));
|
|
50
|
-
|
|
51
|
-
@serializable
|
|
52
|
-
class Wrapper<T> {
|
|
53
|
-
data!: T;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
@serializable
|
|
57
|
-
class Foo {
|
|
58
|
-
@alias("ur mom")
|
|
59
|
-
foo!: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
@serializable
|
|
63
|
-
class Bar {
|
|
64
|
-
bar!: string;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const foo: Wrapper<Foo> = {
|
|
68
|
-
data: new Foo()
|
|
2
|
+
@json
|
|
3
|
+
class Person {
|
|
4
|
+
private prng: i32 = 23;
|
|
5
|
+
public country: string = '';
|
|
6
|
+
|
|
7
|
+
constructor(id: u32) {
|
|
8
|
+
this.prng = 321;
|
|
9
|
+
const seed = id.toString();
|
|
10
|
+
this.country = this.getCountry();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// temp method, returns hard-coded string for now
|
|
14
|
+
private getCountry(): string {
|
|
15
|
+
return "USA";
|
|
16
|
+
}
|
|
69
17
|
}
|
|
70
18
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
console.log(
|
|
74
|
-
/*
|
|
75
|
-
// 9,325,755
|
|
76
|
-
bench("Stringify Object (Vec3)", () => {
|
|
77
|
-
blackbox<string>(vec.__JSON_Serialize());
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// 17,747,531 -> 55,517,015
|
|
81
|
-
bench("New Parse Object (Vec3)", () => {
|
|
82
|
-
blackbox<Vec3>(vec.__JSON_Deserialize(blackbox<string>('{"x":0,"y":0,"z":0}')));
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// 17,747,531
|
|
86
|
-
bench("Old Parse Object (Vec3)", () => {
|
|
87
|
-
blackbox<Vec3>(JSON.parse<Vec3>(blackbox<string>('{"x":3.4,"y":1.2,"z":8.3}')));
|
|
88
|
-
});*/
|
|
19
|
+
const person = new Person(1);
|
|
20
|
+
let result = JSON.stringify<Person>(person);
|
|
21
|
+
console.log(result);
|