json-as 0.5.36 → 0.5.38

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.
@@ -1,7 +1,5 @@
1
- import { u128, u128Safe, u256, u256Safe, i128, i128Safe } from "as-bignum/assembly";
2
1
  import { StringSink } from "as-string-sink/assembly";
3
- import { itoa32, itoa64, dtoa, dtoa_buffered } from "util/number";
4
- import { isSpace } from "util/string";
2
+ import { isSpace, CharCode } from "util/string";
5
3
  import {
6
4
  backSlashCode,
7
5
  commaCode,
@@ -21,9 +19,9 @@ import {
21
19
  tCode,
22
20
  trueWord,
23
21
  uCode,
24
- emptyArrayWord
22
+ emptyArrayWord,
25
23
  } from "./chars";
26
- import { parseJSONInt, escapeChar, isBigNum, unsafeCharCodeAt } from "./util";
24
+ import { parseSciInteger, unsafeCharCodeAt } from "./util";
27
25
 
28
26
  /**
29
27
  * JSON Encoder/Decoder for AssemblyScript
@@ -37,7 +35,8 @@ export namespace JSON {
37
35
  * @param data T
38
36
  * @returns string
39
37
  */
40
- export function stringify<T>(data: T): string {
38
+ // @ts-ignore
39
+ @inline export function stringify<T>(data: T): string {
41
40
  // String
42
41
  if (isString<T>() && data != null) {
43
42
  // @ts-ignore
@@ -94,11 +93,10 @@ export namespace JSON {
94
93
  result.write(rightBracketWord);
95
94
  return result.toString();
96
95
  }
97
- } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
98
- // @ts-ignore
99
- return data.toString();
100
96
  } else {
101
- throw new Error(`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`);
97
+ throw new Error(
98
+ `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
99
+ );
102
100
  }
103
101
  }
104
102
  /**
@@ -109,8 +107,9 @@ export namespace JSON {
109
107
  * @param data string
110
108
  * @returns T
111
109
  */
110
+
112
111
  // @ts-ignore
113
- export function parse<T>(data: string): T {
112
+ @inline export function parse<T>(data: string): T {
114
113
  let type: T;
115
114
  if (isString<T>()) {
116
115
  // @ts-ignore
@@ -133,16 +132,15 @@ export namespace JSON {
133
132
  } else if (idof<nonnull<T>>() == idof<Date>()) {
134
133
  // @ts-ignore
135
134
  return Date.fromString(data);
136
- } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
137
- // @ts-ignore
138
- return parseBigNum<T>(data);
139
135
  } else {
140
136
  // @ts-ignore
141
- throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`);
137
+ throw new Error(
138
+ `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
139
+ );
142
140
  }
143
141
  }
144
142
  // @ts-ignore
145
- function parseObjectValue<T>(data: string): T {
143
+ @inline function parseObjectValue<T>(data: string): T {
146
144
  let type: T;
147
145
  if (isString<T>()) {
148
146
  // @ts-ignore
@@ -153,9 +151,9 @@ export namespace JSON {
153
151
  // \\"
154
152
  if (unsafeCharCodeAt(data, i) === backSlashCode) {
155
153
  char = unsafeCharCodeAt(data, ++i);
156
- result += data.slice(last, i - 1)
154
+ result += data.slice(last, i - 1);
157
155
  if (char === 34) {
158
- result += "\"";
156
+ result += '"';
159
157
  last = ++i;
160
158
  } else if (char === 110) {
161
159
  result += "\n";
@@ -177,7 +175,11 @@ export namespace JSON {
177
175
  } else if (char === 116) {
178
176
  result += "\t";
179
177
  last = ++i;
180
- } else if (char === 117 && load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) === 27584753879220272) {
178
+ } else if (
179
+ char === 117 &&
180
+ load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
181
+ 27584753879220272
182
+ ) {
181
183
  result += "\u000b";
182
184
  i += 4;
183
185
  last = ++i;
@@ -206,162 +208,158 @@ export namespace JSON {
206
208
  } else if (idof<nonnull<T>>() == idof<Date>()) {
207
209
  // @ts-ignore
208
210
  return Date.fromString(data);
209
- } else if ((isManaged<T>() || isReference<T>()) && isBigNum<T>()) {
210
- // @ts-ignore
211
- return parseBigNum<T>(data);
212
211
  } else {
213
212
  // @ts-ignore
214
- throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`)
213
+ throw new Error(
214
+ `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
215
+ );
215
216
  }
216
217
  }
217
218
  }
218
219
 
219
-
220
220
  // @ts-ignore
221
- @inline
222
- function serializeString(data: string): string {
221
+ @inline function serializeString(data: string): string {
223
222
  // @ts-ignore
224
- if (data.length === 0) return "\"\"";
225
- // Fast path for Vectors (3)
226
- let char: i32 = 0;
227
- if (data.length === 1) {
228
- char = unsafeCharCodeAt(data, 0);
229
- if (char === 34) {
230
- return "\\\"";
231
- } else if (char === 92) {
232
- return "\\n";
233
- } else if (char <= 13 && char >= 8) {
234
- switch (char) {
235
- case 0x5C: {
236
- return "\\\\";
237
- }
238
- case 0x08: {
239
- return "\\b";
240
- }
241
- case 0x0D: {
242
- return "\\r";
243
- }
244
- case 0x09: {
245
- return "\\t";
246
- }
247
- case 0x0C: {
248
- return "\\f";
249
- }
250
- case 0x0B: {
251
- return "\\u000b";
252
- }
253
- }
254
- } else {
255
- return data;
256
- }
257
- }
223
+ //if (data.length === 0) return "\"\"";
224
+ /*
225
+ let char: i32 = 0;
226
+ if (data.length === 1) {
227
+ char = unsafeCharCodeAt(data, 0);
228
+ if (char === 34) {
229
+ return "\\\"";
230
+ } else if (char === 92) {
231
+ return "\\n";
232
+ } else if (char <= 13 && char >= 8) {
233
+ switch (char) {
234
+ case 0x5C: {
235
+ return "\\\\";
236
+ }
237
+ case 0x08: {
238
+ return "\\b";
239
+ }
240
+ case 0x0D: {
241
+ return "\\r";
242
+ }
243
+ case 0x09: {
244
+ return "\\t";
245
+ }
246
+ case 0x0C: {
247
+ return "\\f";
248
+ }
249
+ case 0x0B: {
250
+ return "\\u000b";
251
+ }
252
+ }
253
+ } else {
254
+ return data;
255
+ }
256
+ }*/
258
257
 
259
- let result = "\"";
258
+ let result = '"';
260
259
 
261
260
  let last: i32 = 0;
262
- let found: boolean = false;
263
261
  // @ts-ignore
264
262
  for (let i = 0; i < data.length; i++) {
265
- char = unsafeCharCodeAt(<string>data, i);
263
+ const char = unsafeCharCodeAt(<string>data, i);
266
264
  if (char === 34 || char === 92) {
267
265
  result += (<string>data).slice(last, i) + "\\";
268
266
  last = i;
269
- found = true;
270
267
  i++;
271
268
  } else if (char <= 13 && char >= 8) {
272
269
  result += (<string>data).slice(last, i);
273
270
  last = ++i;
274
- found = true;
275
271
  switch (char) {
276
- case 0x5C: {
277
- result += "\\\\";
272
+ case 8: {
273
+ result += "\\b";
278
274
  break;
279
275
  }
280
- case 0x08: {
281
- result += "\\b";
276
+ case 9: {
277
+ result += "\\t";
282
278
  break;
283
279
  }
284
- case 0x0D: {
285
- result += "\\r";
280
+ case 10: {
281
+ result += "\\n";
286
282
  break;
287
283
  }
288
- case 0x09: {
289
- result += "\\t";
284
+ case 11: {
285
+ result += "\\x0B"; // \\u000b
290
286
  break;
291
287
  }
292
- case 0x0C: {
288
+ case 12: {
293
289
  result += "\\f";
294
290
  break;
295
291
  }
296
- case 0x0B: {
297
- result += "\\u000b";
292
+ case 13: {
293
+ result += "\\r";
298
294
  break;
299
295
  }
300
296
  }
301
297
  }
302
- }// 8 10 13 9 12
303
- if (!found) return "\"" + data + "\"";
298
+ }
299
+ if (result.length === 1) return '"' + data + '"';
304
300
  else result += (<string>data).slice(last);
305
- return result + "\"";
306
- }
307
- // @ts-ignore
308
- @inline
309
- // @ts-ignore
310
- function parseBigNum<T>(data: string): T {
311
- // @ts-ignore
312
- if (idof<T>() == idof<u128>()) return u128.fromString(data);
313
- // @ts-ignore
314
- if (idof<T>() == idof<u128Safe>()) return u128Safe.fromString(data);
315
- // @ts-ignore
316
- if (idof<T>() == idof<u256>()) return u128Safe.fromString(data);
317
- // @ts-ignore
318
- if (idof<T>() == idof<u256Safe>()) return u256Safe.fromString(data);
319
- // @ts-ignore
320
- if (idof<T>() == idof<i128>()) return i128.fromString(data);
321
- // @ts-ignore
322
- if (idof<T>() == idof<i128Safe>()) return i128Safe.fromString(data);
323
- // @ts-ignore
324
- //if (idof<T>() == idof<i256Safe>()) return data.
301
+ return result + '"';
325
302
  }
326
303
 
327
304
  // @ts-ignore
328
- @inline
329
- function parseString(data: string): string {
305
+ @inline function parseString(data: string): string {
330
306
  let result = "";
331
307
  let last = 1;
332
- let char = 0;
333
308
  for (let i = 1; i < data.length - 1; i++) {
334
309
  // \\"
335
310
  if (unsafeCharCodeAt(data, i) === backSlashCode) {
336
- char = unsafeCharCodeAt(data, ++i);
337
- result += data.slice(last, i - 1)
311
+ const char = unsafeCharCodeAt(data, ++i);
312
+ result += data.slice(last, i - 1);
338
313
  if (char === 34) {
339
- result += "\"";
314
+ result += '"';
340
315
  last = ++i;
341
316
  } else if (char === 110) {
342
317
  result += "\n";
343
318
  last = ++i;
344
319
  // 92 98 114 116 102 117
345
320
  } else if (char >= 92 && char <= 117) {
346
- if (char === 92) {
347
- result += "\\";
348
- last = ++i;
349
- } else if (char === 98) {
350
- result += "\b";
351
- last = ++i;
352
- } else if (char === 102) {
353
- result += "\f";
354
- last = ++i;
355
- } else if (char === 114) {
356
- result += "\r";
357
- last = ++i;
358
- } else if (char === 116) {
359
- result += "\t";
360
- last = ++i;
361
- } else if (char === 117 && load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) === 27584753879220272) {
362
- result += "\u000b";
363
- i += 4;
364
- last = ++i;
321
+ switch (char) {
322
+ case 92: {
323
+ result += "\\";
324
+ last = ++i;
325
+ break;
326
+ }
327
+ case 98: {
328
+ result += "\b";
329
+ last = ++i;
330
+ break;
331
+ }
332
+ case 110: {
333
+ result += "\n";
334
+ last = ++i;
335
+ }
336
+ case 102: {
337
+ result += "\f";
338
+ last = ++i;
339
+ break;
340
+ }
341
+ case 114: {
342
+ result += "\r";
343
+ last = ++i;
344
+ break;
345
+ }
346
+ case 116: {
347
+ result += "\t";
348
+ last = ++i;
349
+ break;
350
+ }
351
+ default: {
352
+ if (
353
+ char === 117 &&
354
+ load<u64>(changetype<usize>(data) + <usize>((i + 1) << 1)) ===
355
+ 27584753879220272
356
+ ) {
357
+ result += "\u000b";
358
+ i += 4;
359
+ last = ++i;
360
+ }
361
+ break;
362
+ }
365
363
  }
366
364
  }
367
365
  }
@@ -371,20 +369,17 @@ function parseString(data: string): string {
371
369
  }
372
370
 
373
371
  // @ts-ignore
374
- @inline
375
- function parseBoolean<T extends boolean>(data: string): T {
372
+ @inline function parseBoolean<T extends boolean>(data: string): T {
376
373
  if (data.length > 3 && data.startsWith("true")) return <T>true;
377
374
  else if (data.length > 4 && data.startsWith("false")) return <T>false;
378
375
  else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
379
376
  }
380
377
 
381
378
  // @ts-ignore
382
- @inline
383
- // @ts-ignore
384
- export function parseNumber<T>(data: string): T {
379
+ @inline export function parseNumber<T>(data: string): T {
385
380
  if (isInteger<T>()) {
386
381
  // @ts-ignore
387
- return parseJSONInt<T>(data);
382
+ return parseSciInteger<T>(data);
388
383
  }
389
384
  // @ts-ignore
390
385
  const type: T = 0;
@@ -395,9 +390,10 @@ export function parseNumber<T>(data: string): T {
395
390
  }
396
391
 
397
392
  // @ts-ignore
398
- @inline
399
- function parseObject<T>(data: string): T {
400
- let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
393
+ @inline function parseObject<T>(data: string): T {
394
+ let schema: nonnull<T> = changetype<nonnull<T>>(
395
+ __new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
396
+ );
401
397
  let key = "";
402
398
  let isKey = false;
403
399
  let depth = 0;
@@ -419,7 +415,10 @@ function parseObject<T>(data: string): T {
419
415
  if (depth === 0) {
420
416
  ++arrayValueIndex;
421
417
  // @ts-ignore
422
- schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, arrayValueIndex));
418
+ schema.__JSON_Set_Key(
419
+ key,
420
+ data.slice(outerLoopIndex, arrayValueIndex)
421
+ );
423
422
  outerLoopIndex = arrayValueIndex;
424
423
  isKey = false;
425
424
  break;
@@ -440,7 +439,10 @@ function parseObject<T>(data: string): T {
440
439
  if (depth === 0) {
441
440
  ++objectValueIndex;
442
441
  // @ts-ignore
443
- schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, objectValueIndex));
442
+ schema.__JSON_Set_Key(
443
+ key,
444
+ data.slice(outerLoopIndex, objectValueIndex)
445
+ );
444
446
  outerLoopIndex = objectValueIndex;
445
447
  isKey = false;
446
448
  break;
@@ -463,7 +465,10 @@ function parseObject<T>(data: string): T {
463
465
  isKey = true;
464
466
  } else {
465
467
  // @ts-ignore
466
- schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, stringValueIndex));
468
+ schema.__JSON_Set_Key(
469
+ key,
470
+ data.slice(outerLoopIndex, stringValueIndex)
471
+ );
467
472
  isKey = false;
468
473
  }
469
474
  outerLoopIndex = ++stringValueIndex;
@@ -499,7 +504,10 @@ function parseObject<T>(data: string): T {
499
504
  char = unsafeCharCodeAt(data, numberValueIndex);
500
505
  if (char === commaCode || char === rightBraceCode || isSpace(char)) {
501
506
  // @ts-ignore
502
- schema.__JSON_Set_Key(key, data.slice(outerLoopIndex - 1, numberValueIndex));
507
+ schema.__JSON_Set_Key(
508
+ key,
509
+ data.slice(outerLoopIndex - 1, numberValueIndex)
510
+ );
503
511
  outerLoopIndex = numberValueIndex;
504
512
  isKey = false;
505
513
  break;
@@ -511,9 +519,7 @@ function parseObject<T>(data: string): T {
511
519
  }
512
520
 
513
521
  // @ts-ignore
514
- @inline
515
- // @ts-ignore
516
- function parseArray<T extends unknown[]>(data: string): T {
522
+ @inline function parseArray<T extends unknown[]>(data: string): T {
517
523
  if (isString<valueof<T>>()) {
518
524
  return <T>parseStringArray(data);
519
525
  } else if (isBoolean<valueof<T>>()) {
@@ -527,7 +533,10 @@ function parseArray<T extends unknown[]>(data: string): T {
527
533
  return parseArrayArray<T>(data);
528
534
  // @ts-ignore
529
535
  } else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
530
- const type = changetype<nonnull<valueof<T>>>(__new(offsetof<nonnull<valueof<T>>>(), idof<nonnull<valueof<T>>>()));
536
+ // We instantiate the required memory for the class and fill it. This is extremely unsafe and uses "a bit of magic".
537
+ const type = changetype<nonnull<valueof<T>>>(
538
+ __new(offsetof<nonnull<valueof<T>>>(), idof<nonnull<valueof<T>>>())
539
+ );
531
540
  // @ts-ignore
532
541
  if (isDefined(type.__JSON_Set_Key)) {
533
542
  // @ts-ignore
@@ -539,8 +548,7 @@ function parseArray<T extends unknown[]>(data: string): T {
539
548
  }
540
549
 
541
550
  // @ts-ignore
542
- @inline
543
- function parseStringArray(data: string): string[] {
551
+ @inline function parseStringArray(data: string): string[] {
544
552
  const result: string[] = [];
545
553
  let lastPos = 0;
546
554
  let instr = false;
@@ -559,23 +567,22 @@ function parseStringArray(data: string): string[] {
559
567
  }
560
568
 
561
569
  // @ts-ignore
562
- @inline
563
- function parseBooleanArray<T extends boolean[]>(data: string): T {
570
+ @inline function parseBooleanArray<T extends boolean[]>(data: string): T {
564
571
  const result = instantiate<T>();
565
572
  let lastPos = 1;
566
573
  let char = 0;
567
574
  for (let i = 1; i < data.length - 1; i++) {
568
575
  char = unsafeCharCodeAt(data, i);
569
576
  /*// if char == "t" && i+3 == "e"
570
- if (char === tCode && data.charCodeAt(i + 3) === eCode) {
571
- //i += 3;
572
- result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
573
- //i++;
574
- } else if (char === fCode && data.charCodeAt(i + 4) === eCode) {
575
- //i += 4;
576
- result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
577
- //i++;
578
- }*/
577
+ if (char === tCode && data.charCodeAt(i + 3) === eCode) {
578
+ //i += 3;
579
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
580
+ //i++;
581
+ } else if (char === fCode && data.charCodeAt(i + 4) === eCode) {
582
+ //i += 4;
583
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
584
+ //i++;
585
+ }*/
579
586
  if (char === tCode || char === fCode) {
580
587
  lastPos = i;
581
588
  } else if (char === eCode) {
@@ -587,8 +594,7 @@ function parseBooleanArray<T extends boolean[]>(data: string): T {
587
594
  }
588
595
 
589
596
  // @ts-ignore
590
- @inline
591
- function parseNumberArray<T extends number[]>(data: string): T {
597
+ @inline function parseNumberArray<T extends number[]>(data: string): T {
592
598
  const result = instantiate<T>();
593
599
  let lastPos = 0;
594
600
  let char = 0;
@@ -613,8 +619,7 @@ function parseNumberArray<T extends number[]>(data: string): T {
613
619
  }
614
620
 
615
621
  // @ts-ignore
616
- @inline
617
- function parseArrayArray<T extends unknown[][]>(data: string): T {
622
+ @inline function parseArrayArray<T extends unknown[][]>(data: string): T {
618
623
  const result = instantiate<T>();
619
624
  let char = 0;
620
625
  let lastPos = 0;
@@ -643,7 +648,7 @@ function parseArrayArray<T extends unknown[][]>(data: string): T {
643
648
  }
644
649
 
645
650
  // @ts-ignore
646
- export function parseObjectArray<T extends unknown[]>(data: string): T {
651
+ @inline export function parseObjectArray<T extends unknown[]>(data: string): T {
647
652
  const result = instantiate<T>();
648
653
  let char = 0;
649
654
  let lastPos: u32 = 1;
@@ -665,4 +670,4 @@ export function parseObjectArray<T extends unknown[]>(data: string): T {
665
670
  }
666
671
  }
667
672
  return result;
668
- }
673
+ }