json-as 0.9.29 → 1.0.0-alpha.2

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.
Files changed (110) hide show
  1. package/.github/workflows/nodejs.yml +0 -3
  2. package/.gitmodules +0 -0
  3. package/.prettierrc.json +3 -2
  4. package/CHANGELOG +24 -0
  5. package/LICENSE +1 -1
  6. package/README.md +23 -7
  7. package/as-test.config.json +1 -1
  8. package/asconfig.json +2 -2
  9. package/assembly/__benches__/misc.bench.ts +0 -34
  10. package/assembly/__tests__/bool.spec.ts +1 -1
  11. package/assembly/__tests__/simd/string.spec.ts +32 -0
  12. package/assembly/custom/memory.ts +25 -0
  13. package/assembly/custom/util.ts +14 -92
  14. package/assembly/deserialize/simd/string.ts +103 -0
  15. package/assembly/deserialize/simple/arbitrary.ts +17 -0
  16. package/assembly/deserialize/simple/array/arbitrary.ts +113 -0
  17. package/assembly/deserialize/simple/array/array.ts +18 -0
  18. package/assembly/deserialize/simple/array/bool.ts +17 -0
  19. package/assembly/deserialize/simple/array/float.ts +28 -0
  20. package/assembly/deserialize/simple/array/integer.ts +27 -0
  21. package/assembly/deserialize/simple/array/map.ts +18 -0
  22. package/assembly/deserialize/simple/array/object.ts +18 -0
  23. package/assembly/deserialize/simple/array/string.ts +22 -0
  24. package/assembly/deserialize/simple/array.ts +48 -0
  25. package/assembly/deserialize/simple/bool.ts +9 -0
  26. package/assembly/deserialize/simple/date.ts +11 -0
  27. package/assembly/deserialize/simple/float.ts +10 -0
  28. package/assembly/deserialize/simple/integer.ts +5 -0
  29. package/assembly/deserialize/simple/map.ts +154 -0
  30. package/assembly/deserialize/simple/object.ts +158 -0
  31. package/assembly/deserialize/simple/string.ts +48 -0
  32. package/assembly/globals/tables.ts +417 -0
  33. package/assembly/index.d.ts +9 -13
  34. package/assembly/index.ts +261 -146
  35. package/assembly/serialize/simd/string.ts +176 -0
  36. package/assembly/serialize/simple/arbitrary.ts +36 -0
  37. package/assembly/serialize/simple/array.ts +32 -0
  38. package/assembly/serialize/simple/bool.ts +19 -0
  39. package/assembly/serialize/simple/date.ts +13 -0
  40. package/assembly/serialize/simple/float.ts +7 -0
  41. package/assembly/serialize/simple/integer.ts +7 -0
  42. package/assembly/serialize/simple/map.ts +43 -0
  43. package/assembly/serialize/simple/object.ts +7 -0
  44. package/assembly/serialize/simple/string.ts +48 -0
  45. package/assembly/test.ts +36 -27
  46. package/assembly/tsconfig.json +2 -91
  47. package/assembly/types.ts +0 -0
  48. package/assembly/util/atoi.ts +35 -0
  49. package/assembly/util/bytes.ts +12 -0
  50. package/assembly/util/concat.ts +9 -0
  51. package/assembly/util/getArrayDepth.ts +17 -0
  52. package/assembly/util/index.ts +5 -0
  53. package/assembly/util/isSpace.ts +4 -0
  54. package/assembly/util/nextPowerOf2.ts +4 -0
  55. package/assembly/util/ptrToStr.ts +7 -0
  56. package/assembly/util/snp.ts +69 -0
  57. package/bench.js +5 -5
  58. package/modules/as-bs/LICENSE +21 -0
  59. package/modules/as-bs/README.md +95 -0
  60. package/modules/as-bs/assembly/index.ts +166 -0
  61. package/modules/as-bs/assembly/tsconfig.json +97 -0
  62. package/modules/as-bs/index.ts +1 -0
  63. package/modules/as-bs/package.json +32 -0
  64. package/package.json +41 -48
  65. package/transform/lib/builder.js +1275 -0
  66. package/transform/lib/builder.js.map +1 -0
  67. package/transform/lib/index.js +548 -443
  68. package/transform/lib/index.js.map +1 -1
  69. package/transform/lib/linker.js +16 -0
  70. package/transform/lib/linker.js.map +1 -0
  71. package/transform/lib/types.js +26 -0
  72. package/transform/lib/types.js.map +1 -0
  73. package/transform/lib/util.js +47 -0
  74. package/transform/lib/util.js.map +1 -0
  75. package/transform/lib/visitor.js +510 -430
  76. package/transform/lib/visitor.js.map +1 -0
  77. package/transform/package.json +1 -33
  78. package/transform/src/builder.ts +1371 -0
  79. package/transform/src/index.ts +574 -340
  80. package/transform/src/linker.ts +21 -0
  81. package/transform/src/types.ts +28 -0
  82. package/transform/src/util.ts +56 -0
  83. package/transform/src/visitor.ts +531 -0
  84. package/transform/tsconfig.json +3 -1
  85. package/assembly/__benches__/as-tral.d.ts +0 -1
  86. package/assembly/__tests__/date.spec.ts +0 -12
  87. package/assembly/custom/bs.ts +0 -202
  88. package/assembly/deserialize/array/array.ts +0 -31
  89. package/assembly/deserialize/array/bool.ts +0 -19
  90. package/assembly/deserialize/array/float.ts +0 -24
  91. package/assembly/deserialize/array/integer.ts +0 -24
  92. package/assembly/deserialize/array/map.ts +0 -27
  93. package/assembly/deserialize/array/object.ts +0 -27
  94. package/assembly/deserialize/array/string.ts +0 -29
  95. package/assembly/deserialize/array.ts +0 -46
  96. package/assembly/deserialize/bool.ts +0 -34
  97. package/assembly/deserialize/date.ts +0 -19
  98. package/assembly/deserialize/float.ts +0 -21
  99. package/assembly/deserialize/integer.ts +0 -16
  100. package/assembly/deserialize/map.ts +0 -139
  101. package/assembly/deserialize/object.ts +0 -211
  102. package/assembly/deserialize/string.ts +0 -149
  103. package/assembly/serialize/array.ts +0 -44
  104. package/assembly/serialize/bool.ts +0 -10
  105. package/assembly/serialize/date.ts +0 -4
  106. package/assembly/serialize/float.ts +0 -4
  107. package/assembly/serialize/integer.ts +0 -5
  108. package/assembly/serialize/map.ts +0 -24
  109. package/assembly/serialize/object.ts +0 -13
  110. package/assembly/serialize/string.ts +0 -284
@@ -1,202 +0,0 @@
1
- import { dtoa_buffered, itoa_buffered } from "util/number";
2
- import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
3
- // @ts-ignore
4
- @inline const MAX_LEN: usize = 65536;
5
- const STORE: usize[] = [];
6
- let STORE_LEN: usize = 0;
7
- const CACHE = memory.data(i32(MAX_LEN));
8
- // Configurable amount of referenceable strings
9
- let POINTER = changetype<usize>(CACHE);
10
- // @ts-ignore
11
- @inline const MAX_CACHE = CACHE + MAX_LEN;
12
-
13
- export namespace bs {
14
- // @ts-ignore
15
- @inline export function write_int<T extends number>(num: T): void {
16
- POINTER += itoa_buffered(POINTER, num) << 1;
17
- if (MAX_CACHE <= POINTER) bs.shrink();
18
- }
19
-
20
- // @ts-ignore
21
- @inline export function write_int_u<T extends number>(num: T): void {
22
- POINTER += itoa_buffered(POINTER, num) << 1;
23
- }
24
-
25
- // @ts-ignore
26
- @inline export function write_fl<T extends number>(num: T): void {
27
- POINTER += dtoa_buffered(POINTER, num) << 1;
28
- if (MAX_CACHE <= POINTER) bs.shrink();
29
- }
30
-
31
- // @ts-ignore
32
- @inline export function write_fl_u<T extends number>(num: T): void {
33
- POINTER += dtoa_buffered(POINTER, num) << 1;
34
- }
35
-
36
- // @ts-ignore
37
- @inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
38
- memory.copy(POINTER, buf, bytes);
39
- POINTER += bytes;
40
- if (MAX_CACHE <= POINTER) bs.shrink();
41
- }
42
-
43
- // @ts-ignore
44
- @inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void {
45
- memory.copy(POINTER, buf, bytes);
46
- POINTER += bytes;
47
- }
48
-
49
- // @ts-ignore
50
- @inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
51
- memory.copy(POINTER, changetype<usize>(str), bytes);
52
- POINTER += bytes;
53
- if (MAX_CACHE <= POINTER) bs.shrink();
54
- }
55
-
56
- // @ts-ignore
57
- @inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void {
58
- memory.copy(POINTER, changetype<usize>(str), bytes);
59
- POINTER += bytes;
60
- }
61
-
62
- // @ts-ignore
63
- @inline export function write_s_se(str: string, start: usize, end: usize): void {
64
- const bytes = end - start;
65
- memory.copy(POINTER, changetype<usize>(str) + start, bytes);
66
- POINTER += bytes;
67
- if (MAX_CACHE <= POINTER) bs.shrink();
68
- }
69
-
70
- // @ts-ignore
71
- @inline export function write_s_se_u(str: string, start: usize, end: usize): void {
72
- const bytes = end - start;
73
- memory.copy(POINTER, changetype<usize>(str) + start, bytes);
74
- POINTER += bytes;
75
- }
76
-
77
- // @ts-ignore
78
- @inline export function write_8(char: i32): void {
79
- store<u8>(POINTER, char);
80
- POINTER += 2;
81
- if (MAX_CACHE <= POINTER) bs.shrink();
82
- }
83
-
84
- // @ts-ignore
85
- @inline export function write_8_u(char: i32): void {
86
- store<u8>(POINTER, char);
87
- //POINTER += 2;
88
- }
89
-
90
- // @ts-ignore
91
- @inline export function write_16(char: i32): void {
92
- store<u16>(POINTER, char);
93
- POINTER += 2;
94
- if (MAX_CACHE <= POINTER) bs.shrink();
95
- }
96
-
97
- // @ts-ignore
98
- @inline export function write_16_u(char: i32): void {
99
- store<u16>(POINTER, char);
100
- //POINTER += 2;
101
- }
102
-
103
- // @ts-ignore
104
- @inline export function write_32(chars: i32): void {
105
- store<u32>(POINTER, chars);
106
- POINTER += 4;
107
- if (MAX_CACHE <= POINTER) bs.shrink();
108
- }
109
-
110
- // @ts-ignore
111
- @inline export function write_32_u(chars: i32): void {
112
- store<u32>(POINTER, chars);
113
- //POINTER += 4;
114
- }
115
-
116
- // @ts-ignore
117
- @inline export function write_64(chars: i64): void {
118
- store<u64>(POINTER, chars);
119
- POINTER += 8;
120
- if (MAX_CACHE <= POINTER) bs.shrink();
121
- }
122
-
123
- // @ts-ignore
124
- @inline export function write_64_u(chars: i64): void {
125
- store<u64>(POINTER, chars);
126
- POINTER += 8;
127
- }
128
-
129
- // @ts-ignore
130
- @inline export function write_128(chars: v128): void {
131
- store<v128>(POINTER, chars);
132
- POINTER += 16;
133
- if (MAX_CACHE <= POINTER) bs.shrink();
134
- }
135
-
136
- // @ts-ignore
137
- @inline export function write_128_n(chars: v128, n: usize): void {
138
- store<v128>(POINTER, chars);
139
- POINTER += n;
140
- if (MAX_CACHE <= POINTER) bs.shrink();
141
- }
142
-
143
- // @ts-ignore
144
- @inline export function write_128_u(chars: v128): void {
145
- store<v128>(POINTER, chars);
146
- //POINTER += 16;
147
- //if (MAX_CACHE <= POINTER) bs.shrink();
148
- }
149
-
150
- // @ts-ignore
151
- @inline export function shrink(): void {
152
- const len = POINTER - CACHE;
153
- STORE_LEN += len;
154
- const out = __new(len, idof<ArrayBuffer>());
155
- memory.copy(out, CACHE, len);
156
- bs.reset();
157
- STORE.push(out);
158
- }
159
-
160
- // @ts-ignore
161
- @inline export function out<T>(): T {
162
- const len = POINTER - CACHE;
163
- let out = __new(len + STORE_LEN, idof<T>());
164
-
165
- memory.copy(out, CACHE, len);
166
- if (STORE_LEN) {
167
- out += len;
168
- for (let i = 0; i < STORE.length; i++) {
169
- const ptr = changetype<usize>(unchecked(STORE[i]));
170
- const storeLen = changetype<OBJECT>(ptr - TOTAL_OVERHEAD).rtSize;
171
- memory.copy(out, ptr, storeLen);
172
- //__unpin(ptr);
173
- out += storeLen;
174
- }
175
- STORE_LEN = 0;
176
- }
177
- bs.reset();
178
-
179
- return changetype<T>(out);
180
- }
181
-
182
- // @ts-ignore
183
- @inline export function out_u<T>(): T {
184
- const len = POINTER - CACHE;
185
- const out = __new(len + STORE_LEN, idof<T>());
186
-
187
- memory.copy(out, CACHE, len);
188
- bs.reset();
189
-
190
- return changetype<T>(out);
191
- }
192
-
193
- // @ts-ignore
194
- @inline export function _out(out: usize): void {
195
- memory.copy(out, CACHE, POINTER - CACHE);
196
- }
197
-
198
- // @ts-ignore
199
- @inline export function reset(): void {
200
- POINTER = CACHE;
201
- }
202
- }
@@ -1,31 +0,0 @@
1
- import { BRACKET_LEFT, BRACKET_RIGHT } from "../../custom/chars";
2
- import { JSON } from "../..";
3
- import { unsafeCharCodeAt } from "../../custom/util";
4
-
5
- // @ts-ignore: Decorator valid here
6
- @inline export function deserializeArrayArray<T extends unknown[][]>(data: string): T {
7
- const result = instantiate<T>();
8
- let lastPos = 0;
9
- let depth = 0;
10
- let i = 1;
11
- // Find start of bracket
12
- //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
13
- //i++;
14
- for (; i < data.length - 1; i++) {
15
- const char = unsafeCharCodeAt(data, i);
16
- if (char === BRACKET_LEFT) {
17
- if (depth === 0) {
18
- lastPos = i;
19
- }
20
- // Shifting is 6% faster than incrementing
21
- depth++;
22
- } else if (char === BRACKET_RIGHT) {
23
- depth--;
24
- if (depth === 0) {
25
- i++;
26
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
27
- }
28
- }
29
- }
30
- return result;
31
- }
@@ -1,19 +0,0 @@
1
- import { CHAR_E, CHAR_F, CHAR_T } from "../../custom/chars";
2
- import { unsafeCharCodeAt } from "../../custom/util";
3
- import { deserializeBoolean } from "../bool";
4
-
5
- // @ts-ignore: Decorator valid here
6
- @inline export function deserializeBooleanArray<T extends boolean[]>(data: string): T {
7
- const result = instantiate<T>();
8
- let lastPos = 1;
9
- for (let i = 1; i < data.length - 1; i++) {
10
- const char = unsafeCharCodeAt(data, i);
11
- if (char === CHAR_T || char === CHAR_F) {
12
- lastPos = i;
13
- } else if (char === CHAR_E) {
14
- i++;
15
- result.push(deserializeBoolean(data.slice(lastPos, i)));
16
- }
17
- }
18
- return result;
19
- }
@@ -1,24 +0,0 @@
1
- import { isSpace } from "util/string";
2
- import { unsafeCharCodeAt } from "../../custom/util";
3
- import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
4
- import { deserializeFloat } from "../float";
5
-
6
- // @ts-ignore: Decorator valid here
7
- @inline export function deserializeFloatArray<T extends number[]>(data: string): T {
8
- const result = instantiate<T>();
9
- let lastPos = 0;
10
- let i = 1;
11
- let awaitingParse = false;
12
- for (; i < data.length; i++) {
13
- const char = unsafeCharCodeAt(data, i);
14
- if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
15
- awaitingParse = true;
16
- lastPos = i;
17
- } else if (awaitingParse && (isSpace(char) || char == COMMA || char == BRACKET_RIGHT) && lastPos > 0) {
18
- awaitingParse = false;
19
- result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i)));
20
- lastPos = 0;
21
- }
22
- }
23
- return result;
24
- }
@@ -1,24 +0,0 @@
1
- import { isSpace } from "util/string";
2
- import { unsafeCharCodeAt } from "../../custom/util";
3
- import { COMMA, BRACKET_RIGHT } from "../../custom/chars";
4
- import { deserializeInteger } from "../integer";
5
-
6
- // @ts-ignore: Decorator valid here
7
- @inline export function deserializeIntegerArray<T extends number[]>(data: string): T {
8
- const result = instantiate<T>();
9
- let lastPos = 0;
10
- let i = 1;
11
- let awaitingParse = false;
12
- for (; i < data.length; i++) {
13
- const char = unsafeCharCodeAt(data, i);
14
- if (lastPos === 0 && ((char >= 48 && char <= 57) || char === 45)) {
15
- awaitingParse = true;
16
- lastPos = i;
17
- } else if (awaitingParse && (isSpace(char) || char == COMMA || char == BRACKET_RIGHT) && lastPos > 0) {
18
- awaitingParse = false;
19
- result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i)));
20
- lastPos = 0;
21
- }
22
- }
23
- return result;
24
- }
@@ -1,27 +0,0 @@
1
- import { BRACE_LEFT, BRACE_RIGHT } from "../../custom/chars";
2
- import { JSON } from "../..";
3
- import { unsafeCharCodeAt } from "../../custom/util";
4
-
5
- // @ts-ignore: Decorator valid here
6
- @inline export function deserializeMapArray<T extends unknown[]>(data: string): T {
7
- const result = instantiate<T>();
8
- let lastPos: u32 = 1;
9
- let depth: u32 = 0;
10
- for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
11
- const char = unsafeCharCodeAt(data, pos);
12
- if (char === BRACE_LEFT) {
13
- if (depth === 0) {
14
- lastPos = pos;
15
- }
16
- depth++;
17
- } else if (char === BRACE_RIGHT) {
18
- depth--;
19
- if (depth === 0) {
20
- pos++;
21
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
22
- //lastPos = pos + 2;
23
- }
24
- }
25
- }
26
- return result;
27
- }
@@ -1,27 +0,0 @@
1
- import { BRACE_LEFT, BRACE_RIGHT } from "../../custom/chars";
2
- import { JSON } from "../..";
3
- import { unsafeCharCodeAt } from "../../custom/util";
4
-
5
- // @ts-ignore: Decorator valid here
6
- @inline export function deserializeObjectArray<T extends unknown[]>(data: string): T {
7
- const result = instantiate<T>();
8
- let lastPos: u32 = 1;
9
- let depth: u32 = 0;
10
- for (let pos: u32 = 0; pos < <u32>data.length; pos++) {
11
- const char = unsafeCharCodeAt(data, pos);
12
- if (char === BRACE_LEFT) {
13
- if (depth === 0) {
14
- lastPos = pos;
15
- }
16
- depth++;
17
- } else if (char === BRACE_RIGHT) {
18
- depth--;
19
- if (depth === 0) {
20
- pos++;
21
- result.push(JSON.parse<valueof<T>>(data.slice(lastPos, pos)));
22
- //lastPos = pos + 2;
23
- }
24
- }
25
- }
26
- return result;
27
- }
@@ -1,29 +0,0 @@
1
- import { BACK_SLASH, QUOTE } from "../../custom/chars";
2
- import { unsafeCharCodeAt } from "../../custom/util";
3
- import { deserializeString } from "../string";
4
-
5
- // @ts-ignore: Decorator valid here
6
- @inline export function deserializeStringArray(data: string): string[] {
7
- const result: string[] = [];
8
- let lastPos = 0;
9
- let instr = false;
10
- let escaping = false;
11
- for (let i = 1; i < data.length - 1; i++) {
12
- const char = unsafeCharCodeAt(data, i);
13
- if (char === BACK_SLASH && !escaping) {
14
- escaping = true;
15
- } else {
16
- if (char === QUOTE && !escaping) {
17
- if (instr === false) {
18
- instr = true;
19
- lastPos = i;
20
- } else {
21
- instr = false;
22
- result.push(deserializeString(data, lastPos, i));
23
- }
24
- }
25
- escaping = false;
26
- }
27
- }
28
- return result;
29
- }
@@ -1,46 +0,0 @@
1
- import { BRACKET_LEFT } from "../custom/chars";
2
- import { isMap } from "../custom/util";
3
- import { deserializeArrayArray } from "./array/array";
4
- import { deserializeBooleanArray } from "./array/bool";
5
- import { deserializeFloatArray } from "./array/float";
6
- import { deserializeIntegerArray } from "./array/integer";
7
- import { deserializeMapArray } from "./array/map";
8
- import { deserializeObjectArray } from "./array/object";
9
- import { deserializeStringArray } from "./array/string";
10
-
11
- // @ts-ignore: Decorator valid here
12
- export function deserializeArray<T extends unknown[]>(data: string): T {
13
- if (isString<valueof<T>>()) {
14
- return <T>deserializeStringArray(data);
15
- } else if (isBoolean<valueof<T>>()) {
16
- // @ts-ignore
17
- return deserializeBooleanArray<T>(data);
18
- } else if (isInteger<valueof<T>>()) {
19
- // @ts-ignore
20
- return deserializeIntegerArray<T>(data);
21
- } else if (isFloat<valueof<T>>()) {
22
- // @ts-ignore
23
- return deserializeFloatArray<T>(data);
24
- } else if (isArrayLike<valueof<T>>()) {
25
- // @ts-ignore
26
- return deserializeArrayArray<T>(data);
27
- } else if (isMap<valueof<T>>()) {
28
- return deserializeMapArray<T>(data);
29
- } else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
30
- const type = changetype<nonnull<valueof<T>>>(0);
31
- // @ts-ignore
32
- if (isDefined(type.__DESERIALIZE)) {
33
- return deserializeObjectArray<T>(data);
34
- }
35
- throw new Error("Could not parse array of type " + nameof<T>() + "! Make sure to add the @json decorator over classes!");
36
- } else {
37
- throw new Error("Could not parse array of type " + nameof<T>() + "!");
38
- }
39
- }
40
-
41
- // @ts-ignore: Decorator valid here
42
- export function deserializeArray_Safe<T extends unknown[]>(data: string): T {
43
- const firstChar = load<u8>(changetype<usize>(data));
44
- if (firstChar != BRACKET_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + ' but got "' + data.slice(0, 100) + '" instead!');
45
- return deserializeArray<T>(data);
46
- }
@@ -1,34 +0,0 @@
1
- import { CHAR_F, CHAR_T } from "../custom/chars";
2
- import { unsafeCharCodeAt } from "../custom/util";
3
-
4
- /**
5
- * Deserialize a string to type boolean
6
- * @param data data to parse
7
- * @returns boolean
8
- */
9
- // @ts-ignore: Decorator valid here
10
- @inline export function deserializeBoolean(data: string, start: i32 = 0, end: i32 = 0): boolean {
11
- if (!end) end = data.length;
12
- const len = end - start;
13
- const ptr = changetype<usize>(data) + <usize>(start << 1);
14
- const firstChar = unsafeCharCodeAt(data, start);
15
- if (len === 4 && firstChar === CHAR_T && load<u64>(ptr) === 28429475166421108) return true;
16
- else if (len === 5 && firstChar === CHAR_F && load<u64>(ptr, 2) === 28429466576093281) return false;
17
- return false; //ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
18
- }
19
-
20
- /**
21
- * Deserialize a string to type boolean (safely)
22
- * @param data data to parse
23
- * @returns boolean
24
- */
25
- // @ts-ignore: Decorator valid here
26
- @inline export function deserializeBoolean_Safe(data: string, start: i32 = 0, end: i32 = 0): boolean {
27
- if (!end) end = data.length;
28
- const len = end - start;
29
- const ptr = changetype<usize>(data) + <usize>(start << 1);
30
- const firstChar = unsafeCharCodeAt(data, start);
31
- if (len === 4 && firstChar === CHAR_T && load<u64>(ptr) === 28429475166421108) return true;
32
- else if (len === 5 && firstChar === CHAR_F && load<u64>(ptr, 2) === 28429466576093281) return false;
33
- throw new Error('Mismatched Types! Expected boolean but got "' + data.slice(0, 100) + '" instead!');
34
- }
@@ -1,19 +0,0 @@
1
- import { QUOTE } from "../custom/chars";
2
-
3
- // @ts-ignore: Decorator valid here
4
- @inline export function deserializeDate(dateTimeString: string): Date {
5
- // Use AssemblyScript's date parser
6
- const d = Date.fromString(dateTimeString.slice(1, -1));
7
-
8
- // Return a new object instead of the one that the parser returned.
9
- // This may seem redundant, but addresses the issue when Date
10
- // is globally aliased to wasi_Date (or some other superclass).
11
- return new Date(d.getTime());
12
- }
13
-
14
- // @ts-ignore: Decorator valid here
15
- @inline export function deserializeDate_Safe(dateTimeString: string): Date {
16
- const firstChar = load<u8>(changetype<usize>(dateTimeString));
17
- if (firstChar != QUOTE) throw new Error('Mismatched Types! Expected Date but got "' + dateTimeString.slice(0, 100) + '" instead!');
18
- return deserializeDate(dateTimeString);
19
- }
@@ -1,21 +0,0 @@
1
- // @ts-ignore: Decorator valid here
2
- @inline export function deserializeFloat<T>(data: string): T {
3
- // @ts-ignore
4
- const type: T = 0;
5
- // @ts-ignore
6
- if (type instanceof f64) return f64.parse(data);
7
- // @ts-ignore
8
- return f32.parse(data);
9
- }
10
-
11
- // @ts-ignore: Decorator valid here
12
- @inline export function deserializeFloat_Safe<T>(data: string): T {
13
- const firstChar = load<u8>(changetype<usize>(data));
14
- if ((firstChar < 48 || firstChar > 57) && firstChar != 45) throw new Error('Mismatched Types! Expected float but got "' + data.slice(0, 100) + '" instead!');
15
- // @ts-ignore
16
- const type: T = 0;
17
- // @ts-ignore
18
- if (type instanceof f64) return f64.parse(data);
19
- // @ts-ignore
20
- return f32.parse(data);
21
- }
@@ -1,16 +0,0 @@
1
- import { snip_fast } from "../custom/util";
2
-
3
- // @ts-ignore: Decorator valid here
4
- @inline export function deserializeInteger<T>(data: string): T {
5
- // @ts-ignore
6
- return snip_fast<T>(data);
7
- }
8
-
9
- // @ts-ignore: Decorator valid here
10
- @inline export function deserializeInteger_Safe<T>(data: string): T {
11
- const firstChar = load<u8>(changetype<usize>(data));
12
- console.log(firstChar.toString());
13
- if ((firstChar < 48 || firstChar > 57) && firstChar != 45) throw new Error("Mismatched Types! Expected " + nameof<T>() + ' but got "' + data.slice(0, 100) + '" instead!');
14
- // @ts-ignore
15
- return snip_fast<T>(data);
16
- }
@@ -1,139 +0,0 @@
1
- import { Virtual } from "as-virtual/assembly";
2
- import { containsCodePoint, unsafeCharCodeAt } from "../custom/util";
3
- import { CHAR_A, BACK_SLASH, COLON, COMMA, CHAR_E, CHAR_F, CHAR_L, BRACE_LEFT, BRACKET_LEFT, CHAR_N, QUOTE, CHAR_R, BRACE_RIGHT, BRACKET_RIGHT, CHAR_S, CHAR_T, CHAR_U } from "../custom/chars";
4
- import { deserializeBoolean } from "./bool";
5
- import { JSON } from "..";
6
- import { deserializeString } from "./string";
7
- import { isSpace } from "util/string";
8
- import { deserializeInteger } from "./integer";
9
- import { deserializeFloat } from "./float";
10
-
11
- // @ts-ignore: Decorator valid here
12
- @inline export function deserializeMap<T extends Map>(data: string): T {
13
- const map: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
14
-
15
- const key = Virtual.createEmpty<string>();
16
- let isKey = false;
17
- let depth = 0;
18
- let outerLoopIndex = 1;
19
- for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
20
- const char = unsafeCharCodeAt(data, outerLoopIndex);
21
- if (char === BRACKET_LEFT) {
22
- for (let arrayValueIndex = outerLoopIndex; arrayValueIndex < data.length - 1; arrayValueIndex++) {
23
- const char = unsafeCharCodeAt(data, arrayValueIndex);
24
- if (char === BRACKET_LEFT) {
25
- depth++;
26
- } else if (char === BRACKET_RIGHT) {
27
- depth--;
28
- if (depth === 0) {
29
- ++arrayValueIndex;
30
- map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, arrayValueIndex)));
31
- outerLoopIndex = arrayValueIndex;
32
- isKey = false;
33
- break;
34
- }
35
- }
36
- }
37
- } else if (char === BRACE_LEFT) {
38
- for (let objectValueIndex = outerLoopIndex; objectValueIndex < data.length - 1; objectValueIndex++) {
39
- const char = unsafeCharCodeAt(data, objectValueIndex);
40
- if (char === BRACE_LEFT) {
41
- depth++;
42
- } else if (char === BRACE_RIGHT) {
43
- depth--;
44
- if (depth === 0) {
45
- ++objectValueIndex;
46
- map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
47
- outerLoopIndex = objectValueIndex;
48
- isKey = false;
49
- break;
50
- }
51
- }
52
- }
53
- } else if (char === QUOTE) {
54
- let escaping = false;
55
- for (let stringValueIndex = ++outerLoopIndex; stringValueIndex < data.length - 1; stringValueIndex++) {
56
- const char = unsafeCharCodeAt(data, stringValueIndex);
57
- if (char === BACK_SLASH && !escaping) {
58
- escaping = true;
59
- } else {
60
- if (char === QUOTE && !escaping) {
61
- if (isKey === false) {
62
- // perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
63
- if (containsCodePoint(data, BACK_SLASH, outerLoopIndex, stringValueIndex)) {
64
- key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
65
- } else {
66
- key.reinst(data, outerLoopIndex, stringValueIndex);
67
- }
68
- isKey = true;
69
- } else {
70
- if (isString<valueof<T>>()) {
71
- const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
72
- map.set(deserializeMapKey<indexof<T>>(key), value);
73
- }
74
- isKey = false;
75
- }
76
- outerLoopIndex = ++stringValueIndex;
77
- break;
78
- }
79
- escaping = false;
80
- }
81
- }
82
- } else if (char == CHAR_N && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L) {
83
- if (isNullable<valueof<T>>()) {
84
- map.set(deserializeMapKey<indexof<T>>(key), null);
85
- }
86
- isKey = false;
87
- } else if (char === CHAR_T && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_R && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E) {
88
- if (isBoolean<valueof<T>>()) {
89
- map.set(deserializeMapKey<indexof<T>>(key), true);
90
- }
91
- isKey = false;
92
- } else if (char === CHAR_F && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_A && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_S && unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E) {
93
- if (isBoolean<valueof<T>>()) {
94
- map.set(deserializeMapKey<indexof<T>>(key), false);
95
- }
96
- isKey = false;
97
- } else if ((char >= 48 && char <= 57) || char === 45) {
98
- let numberValueIndex = ++outerLoopIndex;
99
- for (; numberValueIndex < data.length; numberValueIndex++) {
100
- const char = unsafeCharCodeAt(data, numberValueIndex);
101
- if (char === COLON || char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
102
- if (isInteger<valueof<T>>()) {
103
- map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
104
- } else if (isFloat<valueof<T>>()) {
105
- map.set(deserializeMapKey<indexof<T>>(key), deserializeFloat<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
106
- }
107
- outerLoopIndex = numberValueIndex;
108
- isKey = false;
109
- break;
110
- }
111
- }
112
- }
113
- }
114
-
115
- return map;
116
- }
117
-
118
- //@ts-ignore: Decorator
119
- function deserializeMapKey<T>(key: Virtual<string>): T {
120
- const k = key.copyOut();
121
- if (isString<T>()) {
122
- return k as T;
123
- } else if (isBoolean<T>()) {
124
- return deserializeBoolean(k) as T;
125
- } else if (isInteger<T>()) {
126
- return deserializeInteger<T>(k);
127
- } else if (isFloat<T>()) {
128
- return deserializeFloat<T>(k);
129
- }
130
-
131
- throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
132
- }
133
-
134
- // @ts-ignore: Decorator valid here
135
- @inline export function deserializeMap_Safe<T extends Map>(data: string): T {
136
- const firstChar = load<u8>(changetype<usize>(data));
137
- if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + ' but got "' + data.slice(0, 100) + '" instead!');
138
- return deserializeMap<T>(data);
139
- }