json-as 0.9.28 → 1.0.0-alpha.1
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 +0 -3
- package/.prettierrc.json +4 -1
- package/CHANGELOG +13 -0
- package/LICENSE +1 -1
- package/README.md +22 -7
- package/as-test.config.json +1 -1
- package/asconfig.json +2 -2
- package/assembly/__benches__/misc.bench.ts +17 -31
- package/assembly/__tests__/bool.spec.ts +1 -1
- package/assembly/__tests__/simd/string.spec.ts +32 -0
- package/assembly/__tests__/types.ts +17 -0
- package/assembly/custom/chars.ts +2 -2
- package/assembly/custom/memory.ts +25 -0
- package/assembly/custom/types.ts +1 -0
- package/assembly/custom/util.ts +59 -140
- package/assembly/deserialize/simd/string.ts +103 -0
- package/assembly/deserialize/simple/arbitrary.ts +17 -0
- package/assembly/deserialize/simple/array/arbitrary.ts +113 -0
- package/assembly/deserialize/simple/array/array.ts +18 -0
- package/assembly/deserialize/simple/array/bool.ts +17 -0
- package/assembly/deserialize/simple/array/float.ts +28 -0
- package/assembly/deserialize/simple/array/integer.ts +27 -0
- package/assembly/deserialize/simple/array/map.ts +18 -0
- package/assembly/deserialize/simple/array/object.ts +18 -0
- package/assembly/deserialize/simple/array/string.ts +22 -0
- package/assembly/deserialize/simple/array.ts +48 -0
- package/assembly/deserialize/simple/bool.ts +9 -0
- package/assembly/deserialize/simple/date.ts +11 -0
- package/assembly/deserialize/simple/float.ts +10 -0
- package/assembly/deserialize/simple/integer.ts +5 -0
- package/assembly/deserialize/simple/map.ts +154 -0
- package/assembly/deserialize/simple/object.ts +159 -0
- package/assembly/deserialize/simple/string.ts +48 -0
- package/assembly/globals/tables.ts +417 -0
- package/assembly/index.d.ts +9 -13
- package/assembly/index.ts +282 -158
- package/assembly/serialize/simd/string.ts +176 -0
- package/assembly/serialize/simple/arbitrary.ts +36 -0
- package/assembly/serialize/simple/array.ts +32 -0
- package/assembly/serialize/simple/bool.ts +19 -0
- package/assembly/serialize/simple/date.ts +13 -0
- package/assembly/serialize/simple/float.ts +7 -0
- package/assembly/serialize/simple/integer.ts +7 -0
- package/assembly/serialize/simple/map.ts +43 -0
- package/assembly/serialize/simple/object.ts +7 -0
- package/assembly/serialize/simple/string.ts +48 -0
- package/assembly/test.ts +43 -28
- package/assembly/tsconfig.json +2 -91
- package/assembly/types.ts +0 -0
- package/assembly/util/atoi.ts +35 -0
- package/assembly/util/bytes.ts +12 -0
- package/assembly/util/concat.ts +9 -0
- package/assembly/util/getArrayDepth.ts +17 -0
- package/assembly/util/index.ts +5 -0
- package/assembly/util/isSpace.ts +4 -0
- package/assembly/util/nextPowerOf2.ts +4 -0
- package/assembly/util/ptrToStr.ts +7 -0
- package/assembly/util/snp.ts +69 -0
- package/bench.js +5 -5
- package/modules/bs/index.ts +167 -0
- package/modules/tsconfig.json +8 -0
- package/package.json +42 -47
- package/transform/lib/builder.js +1353 -0
- package/transform/lib/builder.js.map +1 -0
- package/transform/lib/index.js +497 -395
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/index.old.js +404 -0
- package/transform/lib/index.old.js.map +1 -0
- package/transform/lib/linker.js +18 -0
- package/transform/lib/linker.js.map +1 -0
- package/transform/lib/types.js +25 -0
- package/transform/lib/types.js.map +1 -0
- package/transform/lib/util.js +47 -0
- package/transform/lib/util.js.map +1 -0
- package/transform/lib/visitor.js +529 -446
- package/transform/lib/visitor.js.map +1 -0
- package/transform/package.json +1 -34
- package/transform/src/builder.ts +1371 -0
- package/transform/src/index.ts +571 -481
- package/transform/src/linker.ts +21 -0
- package/transform/src/types.ts +27 -0
- package/transform/src/util.ts +56 -0
- package/transform/src/visitor.ts +531 -0
- package/transform/tsconfig.json +3 -1
- package/assembly/__benches__/as-tral.d.ts +0 -1
- package/assembly/custom/bs.ts +0 -211
- package/assembly/deserialize/array/array.ts +0 -31
- package/assembly/deserialize/array/bool.ts +0 -19
- package/assembly/deserialize/array/float.ts +0 -24
- package/assembly/deserialize/array/integer.ts +0 -24
- package/assembly/deserialize/array/map.ts +0 -27
- package/assembly/deserialize/array/object.ts +0 -27
- package/assembly/deserialize/array/string.ts +0 -29
- package/assembly/deserialize/array.ts +0 -46
- package/assembly/deserialize/bool.ts +0 -34
- package/assembly/deserialize/date.ts +0 -19
- package/assembly/deserialize/float.ts +0 -21
- package/assembly/deserialize/integer.ts +0 -16
- package/assembly/deserialize/map.ts +0 -189
- package/assembly/deserialize/object.ts +0 -271
- package/assembly/deserialize/string.ts +0 -164
- package/assembly/serialize/array.ts +0 -51
- package/assembly/serialize/bool.ts +0 -10
- package/assembly/serialize/date.ts +0 -4
- package/assembly/serialize/float.ts +0 -4
- package/assembly/serialize/integer.ts +0 -5
- package/assembly/serialize/map.ts +0 -24
- package/assembly/serialize/object.ts +0 -13
- package/assembly/serialize/string.ts +0 -287
- package/logs/test.log.json +0 -1049
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import { Virtual } from "as-virtual/assembly";
|
|
2
|
-
import { containsCodePoint, unsafeCharCodeAt } from "../custom/util";
|
|
3
|
-
import {
|
|
4
|
-
CHAR_A,
|
|
5
|
-
BACK_SLASH,
|
|
6
|
-
COLON,
|
|
7
|
-
COMMA,
|
|
8
|
-
CHAR_E,
|
|
9
|
-
CHAR_F,
|
|
10
|
-
CHAR_L,
|
|
11
|
-
BRACE_LEFT,
|
|
12
|
-
BRACKET_LEFT,
|
|
13
|
-
CHAR_N,
|
|
14
|
-
QUOTE,
|
|
15
|
-
CHAR_R,
|
|
16
|
-
BRACE_RIGHT,
|
|
17
|
-
BRACKET_RIGHT,
|
|
18
|
-
CHAR_S,
|
|
19
|
-
CHAR_T,
|
|
20
|
-
CHAR_U
|
|
21
|
-
} from "../custom/chars";
|
|
22
|
-
import { deserializeBoolean } from "./bool";
|
|
23
|
-
import { JSON } from "..";
|
|
24
|
-
import { deserializeString } from "./string";
|
|
25
|
-
import { isSpace } from "util/string";
|
|
26
|
-
import { deserializeInteger } from "./integer";
|
|
27
|
-
import { deserializeFloat } from "./float";
|
|
28
|
-
|
|
29
|
-
// @ts-ignore: Decorator valid here
|
|
30
|
-
@inline export function deserializeMap<T extends Map>(data: string): T {
|
|
31
|
-
|
|
32
|
-
const map: nonnull<T> = changetype<nonnull<T>>(
|
|
33
|
-
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const key = Virtual.createEmpty<string>();
|
|
37
|
-
let isKey = false;
|
|
38
|
-
let depth = 0;
|
|
39
|
-
let outerLoopIndex = 1;
|
|
40
|
-
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
41
|
-
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
42
|
-
if (char === BRACKET_LEFT) {
|
|
43
|
-
for (
|
|
44
|
-
let arrayValueIndex = outerLoopIndex;
|
|
45
|
-
arrayValueIndex < data.length - 1;
|
|
46
|
-
arrayValueIndex++
|
|
47
|
-
) {
|
|
48
|
-
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
49
|
-
if (char === BRACKET_LEFT) {
|
|
50
|
-
depth++;
|
|
51
|
-
} else if (char === BRACKET_RIGHT) {
|
|
52
|
-
depth--;
|
|
53
|
-
if (depth === 0) {
|
|
54
|
-
++arrayValueIndex;
|
|
55
|
-
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, arrayValueIndex)));
|
|
56
|
-
outerLoopIndex = arrayValueIndex;
|
|
57
|
-
isKey = false;
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
} else if (char === BRACE_LEFT) {
|
|
63
|
-
for (
|
|
64
|
-
let objectValueIndex = outerLoopIndex;
|
|
65
|
-
objectValueIndex < data.length - 1;
|
|
66
|
-
objectValueIndex++
|
|
67
|
-
) {
|
|
68
|
-
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
69
|
-
if (char === BRACE_LEFT) {
|
|
70
|
-
depth++;
|
|
71
|
-
} else if (char === BRACE_RIGHT) {
|
|
72
|
-
depth--;
|
|
73
|
-
if (depth === 0) {
|
|
74
|
-
++objectValueIndex;
|
|
75
|
-
map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
|
|
76
|
-
outerLoopIndex = objectValueIndex;
|
|
77
|
-
isKey = false;
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
} else if (char === QUOTE) {
|
|
83
|
-
let escaping = false;
|
|
84
|
-
for (
|
|
85
|
-
let stringValueIndex = ++outerLoopIndex;
|
|
86
|
-
stringValueIndex < data.length - 1;
|
|
87
|
-
stringValueIndex++
|
|
88
|
-
) {
|
|
89
|
-
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
90
|
-
if (char === BACK_SLASH && !escaping) {
|
|
91
|
-
escaping = true;
|
|
92
|
-
} else {
|
|
93
|
-
if (
|
|
94
|
-
char === QUOTE && !escaping
|
|
95
|
-
) {
|
|
96
|
-
if (isKey === false) {
|
|
97
|
-
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences
|
|
98
|
-
if (containsCodePoint(data, BACK_SLASH, outerLoopIndex, stringValueIndex)) {
|
|
99
|
-
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex));
|
|
100
|
-
} else {
|
|
101
|
-
key.reinst(data, outerLoopIndex, stringValueIndex);
|
|
102
|
-
}
|
|
103
|
-
isKey = true;
|
|
104
|
-
} else {
|
|
105
|
-
if (isString<valueof<T>>()) {
|
|
106
|
-
const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex);
|
|
107
|
-
map.set(deserializeMapKey<indexof<T>>(key), value);
|
|
108
|
-
}
|
|
109
|
-
isKey = false;
|
|
110
|
-
}
|
|
111
|
-
outerLoopIndex = ++stringValueIndex;
|
|
112
|
-
break;
|
|
113
|
-
}
|
|
114
|
-
escaping = false;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
} else if (
|
|
118
|
-
char == CHAR_N &&
|
|
119
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
|
|
120
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
|
|
121
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L) {
|
|
122
|
-
if (isNullable<valueof<T>>()) {
|
|
123
|
-
map.set(deserializeMapKey<indexof<T>>(key), null);
|
|
124
|
-
}
|
|
125
|
-
isKey = false;
|
|
126
|
-
} else if (
|
|
127
|
-
char === CHAR_T &&
|
|
128
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_R &&
|
|
129
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_U &&
|
|
130
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
|
|
131
|
-
) {
|
|
132
|
-
if (isBoolean<valueof<T>>()) {
|
|
133
|
-
map.set(deserializeMapKey<indexof<T>>(key), true);
|
|
134
|
-
}
|
|
135
|
-
isKey = false;
|
|
136
|
-
} else if (
|
|
137
|
-
char === CHAR_F &&
|
|
138
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_A &&
|
|
139
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_L &&
|
|
140
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_S &&
|
|
141
|
-
unsafeCharCodeAt(data, ++outerLoopIndex) === CHAR_E
|
|
142
|
-
) {
|
|
143
|
-
if (isBoolean<valueof<T>>()) {
|
|
144
|
-
map.set(deserializeMapKey<indexof<T>>(key), false);
|
|
145
|
-
}
|
|
146
|
-
isKey = false;
|
|
147
|
-
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
148
|
-
let numberValueIndex = ++outerLoopIndex;
|
|
149
|
-
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
150
|
-
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
151
|
-
if (char === COLON || char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
|
|
152
|
-
if (isInteger<valueof<T>>()) {
|
|
153
|
-
map.set(deserializeMapKey<indexof<T>>(key), deserializeInteger<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
|
|
154
|
-
} else if (isFloat<valueof<T>>()) {
|
|
155
|
-
map.set(deserializeMapKey<indexof<T>>(key), deserializeFloat<valueof<T>>(data.slice(outerLoopIndex - 1, numberValueIndex)));
|
|
156
|
-
}
|
|
157
|
-
outerLoopIndex = numberValueIndex;
|
|
158
|
-
isKey = false;
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return map;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
//@ts-ignore: Decorator
|
|
169
|
-
function deserializeMapKey<T>(key: Virtual<string>): T {
|
|
170
|
-
const k = key.copyOut();
|
|
171
|
-
if (isString<T>()) {
|
|
172
|
-
return k as T;
|
|
173
|
-
} else if (isBoolean<T>()) {
|
|
174
|
-
return deserializeBoolean(k) as T;
|
|
175
|
-
} else if (isInteger<T>()) {
|
|
176
|
-
return deserializeInteger<T>(k);
|
|
177
|
-
} else if (isFloat<T>()) {
|
|
178
|
-
return deserializeFloat<T>(k);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// @ts-ignore: Decorator valid here
|
|
185
|
-
@inline export function deserializeMap_Safe<T extends Map>(data: string): T {
|
|
186
|
-
const firstChar = load<u8>(changetype<usize>(data));
|
|
187
|
-
if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + " but got \"" + data.slice(0, 100) + "\" instead!");
|
|
188
|
-
return deserializeMap<T>(data);
|
|
189
|
-
}
|
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import { unsafeCharCodeAt } from "../custom/util";
|
|
2
|
-
import { CHAR_A, BACK_SLASH, 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";
|
|
3
|
-
import { isSpace } from "util/string";
|
|
4
|
-
|
|
5
|
-
// @ts-ignore: Decorator valid here
|
|
6
|
-
@inline export function deserializeObject<T>(data: string): T {
|
|
7
|
-
const schema: nonnull<T> = changetype<nonnull<T>>(
|
|
8
|
-
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
schema.__INITIALIZE();
|
|
13
|
-
|
|
14
|
-
let key_start: i32 = 0;
|
|
15
|
-
let key_end: i32 = 0;
|
|
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 (
|
|
23
|
-
let arrayValueIndex = outerLoopIndex;
|
|
24
|
-
arrayValueIndex < data.length - 1;
|
|
25
|
-
arrayValueIndex++
|
|
26
|
-
) {
|
|
27
|
-
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
28
|
-
if (char === BRACKET_LEFT) {
|
|
29
|
-
depth++;
|
|
30
|
-
} else if (char === BRACKET_RIGHT) {
|
|
31
|
-
depth--;
|
|
32
|
-
if (depth === 0) {
|
|
33
|
-
++arrayValueIndex;
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, arrayValueIndex);
|
|
36
|
-
outerLoopIndex = arrayValueIndex;
|
|
37
|
-
isKey = false;
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
} else if (char === BRACE_LEFT) {
|
|
43
|
-
for (
|
|
44
|
-
let objectValueIndex = outerLoopIndex;
|
|
45
|
-
objectValueIndex < data.length - 1;
|
|
46
|
-
objectValueIndex++
|
|
47
|
-
) {
|
|
48
|
-
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
49
|
-
if (char === BRACE_LEFT) {
|
|
50
|
-
depth++;
|
|
51
|
-
} else if (char === BRACE_RIGHT) {
|
|
52
|
-
depth--;
|
|
53
|
-
if (depth === 0) {
|
|
54
|
-
++objectValueIndex;
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, objectValueIndex);
|
|
57
|
-
outerLoopIndex = objectValueIndex;
|
|
58
|
-
isKey = false;
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
} else if (char === QUOTE) {
|
|
64
|
-
let escaping = false;
|
|
65
|
-
for (
|
|
66
|
-
let stringValueIndex = ++outerLoopIndex;
|
|
67
|
-
stringValueIndex < data.length - 1;
|
|
68
|
-
stringValueIndex++
|
|
69
|
-
) {
|
|
70
|
-
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
71
|
-
if (char === BACK_SLASH && !escaping) {
|
|
72
|
-
escaping = true;
|
|
73
|
-
} else {
|
|
74
|
-
if (char === QUOTE && !escaping) {
|
|
75
|
-
if (isKey === false) {
|
|
76
|
-
key_start = outerLoopIndex;
|
|
77
|
-
key_end = stringValueIndex;
|
|
78
|
-
isKey = true;
|
|
79
|
-
} else {
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex + 1);
|
|
82
|
-
isKey = false;
|
|
83
|
-
}
|
|
84
|
-
outerLoopIndex = ++stringValueIndex;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
escaping = false;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
} else if (
|
|
91
|
-
char == CHAR_N &&
|
|
92
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U &&
|
|
93
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
94
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L
|
|
95
|
-
) {
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
98
|
-
outerLoopIndex += 3;
|
|
99
|
-
isKey = false;
|
|
100
|
-
} else if (
|
|
101
|
-
char === CHAR_T &&
|
|
102
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R &&
|
|
103
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U &&
|
|
104
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E
|
|
105
|
-
) {
|
|
106
|
-
// @ts-ignore
|
|
107
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
108
|
-
outerLoopIndex += 3;
|
|
109
|
-
isKey = false;
|
|
110
|
-
} else if (
|
|
111
|
-
char === CHAR_F &&
|
|
112
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A &&
|
|
113
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
114
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S &&
|
|
115
|
-
unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E
|
|
116
|
-
) {
|
|
117
|
-
// @ts-ignore
|
|
118
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
|
|
119
|
-
outerLoopIndex += 4;
|
|
120
|
-
isKey = false;
|
|
121
|
-
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
122
|
-
let numberValueIndex = ++outerLoopIndex;
|
|
123
|
-
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
124
|
-
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
125
|
-
if (char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
|
|
126
|
-
// @ts-ignore
|
|
127
|
-
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, numberValueIndex);
|
|
128
|
-
outerLoopIndex = numberValueIndex;
|
|
129
|
-
isKey = false;
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return schema;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// @ts-ignore: Decorator valid here
|
|
139
|
-
@inline export function deserializeObject_Safe<T>(data: string): T {
|
|
140
|
-
const firstChar = load<u8>(changetype<usize>(data));
|
|
141
|
-
if (firstChar != BRACE_LEFT) throw new Error("Mismatched Types! Expected " + nameof<T>() + " but got \"" + data.slice(0, 100) + "\" instead!");
|
|
142
|
-
const schema: nonnull<T> = changetype<nonnull<T>>(
|
|
143
|
-
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>())
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
// @ts-ignore
|
|
147
|
-
schema.__INITIALIZE();
|
|
148
|
-
|
|
149
|
-
let key_start: i32 = 0;
|
|
150
|
-
let key_end: i32 = 0;
|
|
151
|
-
let isKey = false;
|
|
152
|
-
let depth = 0;
|
|
153
|
-
let outerLoopIndex = 1;
|
|
154
|
-
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
|
|
155
|
-
const char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
156
|
-
if (char === BRACKET_LEFT) {
|
|
157
|
-
for (
|
|
158
|
-
let arrayValueIndex = outerLoopIndex;
|
|
159
|
-
arrayValueIndex < data.length - 1;
|
|
160
|
-
arrayValueIndex++
|
|
161
|
-
) {
|
|
162
|
-
const char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
163
|
-
if (char === BRACKET_LEFT) {
|
|
164
|
-
depth++;
|
|
165
|
-
} else if (char === BRACKET_RIGHT) {
|
|
166
|
-
depth--;
|
|
167
|
-
if (depth === 0) {
|
|
168
|
-
++arrayValueIndex;
|
|
169
|
-
// @ts-ignore
|
|
170
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, arrayValueIndex);
|
|
171
|
-
outerLoopIndex = arrayValueIndex;
|
|
172
|
-
isKey = false;
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
} else if (char === BRACE_LEFT) {
|
|
178
|
-
for (
|
|
179
|
-
let objectValueIndex = outerLoopIndex;
|
|
180
|
-
objectValueIndex < data.length - 1;
|
|
181
|
-
objectValueIndex++
|
|
182
|
-
) {
|
|
183
|
-
const char = unsafeCharCodeAt(data, objectValueIndex);
|
|
184
|
-
if (char === BRACE_LEFT) {
|
|
185
|
-
depth++;
|
|
186
|
-
} else if (char === BRACE_RIGHT) {
|
|
187
|
-
depth--;
|
|
188
|
-
if (depth === 0) {
|
|
189
|
-
++objectValueIndex;
|
|
190
|
-
// @ts-ignore
|
|
191
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, objectValueIndex);
|
|
192
|
-
outerLoopIndex = objectValueIndex;
|
|
193
|
-
isKey = false;
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
} else if (char === QUOTE) {
|
|
199
|
-
let escaping = false;
|
|
200
|
-
for (
|
|
201
|
-
let stringValueIndex = ++outerLoopIndex;
|
|
202
|
-
stringValueIndex < data.length - 1;
|
|
203
|
-
stringValueIndex++
|
|
204
|
-
) {
|
|
205
|
-
const char = unsafeCharCodeAt(data, stringValueIndex);
|
|
206
|
-
if (char === BACK_SLASH && !escaping) {
|
|
207
|
-
escaping = true;
|
|
208
|
-
} else {
|
|
209
|
-
if (char === QUOTE && !escaping) {
|
|
210
|
-
if (isKey === false) {
|
|
211
|
-
key_start = outerLoopIndex;
|
|
212
|
-
key_end = stringValueIndex;
|
|
213
|
-
isKey = true;
|
|
214
|
-
} else {
|
|
215
|
-
// @ts-ignore
|
|
216
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex + 1);
|
|
217
|
-
isKey = false;
|
|
218
|
-
}
|
|
219
|
-
outerLoopIndex = ++stringValueIndex;
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
escaping = false;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
} else if (
|
|
226
|
-
char == CHAR_N &&
|
|
227
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_U &&
|
|
228
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
229
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_L
|
|
230
|
-
) {
|
|
231
|
-
// @ts-ignore
|
|
232
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
233
|
-
outerLoopIndex += 3;
|
|
234
|
-
isKey = false;
|
|
235
|
-
} else if (
|
|
236
|
-
char === CHAR_T &&
|
|
237
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_R &&
|
|
238
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_U &&
|
|
239
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_E
|
|
240
|
-
) {
|
|
241
|
-
// @ts-ignore
|
|
242
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4);
|
|
243
|
-
outerLoopIndex += 3;
|
|
244
|
-
isKey = false;
|
|
245
|
-
} else if (
|
|
246
|
-
char === CHAR_F &&
|
|
247
|
-
unsafeCharCodeAt(data, outerLoopIndex + 1) === CHAR_A &&
|
|
248
|
-
unsafeCharCodeAt(data, outerLoopIndex + 2) === CHAR_L &&
|
|
249
|
-
unsafeCharCodeAt(data, outerLoopIndex + 3) === CHAR_S &&
|
|
250
|
-
unsafeCharCodeAt(data, outerLoopIndex + 4) === CHAR_E
|
|
251
|
-
) {
|
|
252
|
-
// @ts-ignore
|
|
253
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5);
|
|
254
|
-
outerLoopIndex += 4;
|
|
255
|
-
isKey = false;
|
|
256
|
-
} else if ((char >= 48 && char <= 57) || char === 45) {
|
|
257
|
-
let numberValueIndex = ++outerLoopIndex;
|
|
258
|
-
for (; numberValueIndex < data.length; numberValueIndex++) {
|
|
259
|
-
const char = unsafeCharCodeAt(data, numberValueIndex);
|
|
260
|
-
if (char === COMMA || char === BRACE_RIGHT || isSpace(char)) {
|
|
261
|
-
// @ts-ignore
|
|
262
|
-
schema.__DESERIALIZE_SAFE(data, key_start, key_end, outerLoopIndex - 1, numberValueIndex);
|
|
263
|
-
outerLoopIndex = numberValueIndex;
|
|
264
|
-
isKey = false;
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return schema;
|
|
271
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CHAR_B,
|
|
3
|
-
BACK_SLASH,
|
|
4
|
-
BACKSPACE,
|
|
5
|
-
CARRIAGE_RETURN,
|
|
6
|
-
CHAR_F,
|
|
7
|
-
FORM_FEED,
|
|
8
|
-
FWD_SLASH,
|
|
9
|
-
CHAR_N,
|
|
10
|
-
NEW_LINE,
|
|
11
|
-
QUOTE,
|
|
12
|
-
CHAR_R,
|
|
13
|
-
CHAR_T,
|
|
14
|
-
TAB,
|
|
15
|
-
CHAR_U
|
|
16
|
-
} from "../custom/chars";
|
|
17
|
-
import { Sink } from "../custom/sink";
|
|
18
|
-
import { unsafeCharCodeAt } from "../custom/util";
|
|
19
|
-
|
|
20
|
-
// @ts-ignore: Decorator valid here
|
|
21
|
-
@inline export function deserializeString(data: string, start: i32 = 0, end: i32 = 0): string {
|
|
22
|
-
end = end || data.length - 1;
|
|
23
|
-
let result = Sink.withCapacity(end - start - 1);
|
|
24
|
-
let last = start + 1;
|
|
25
|
-
for (let i = last; i < end; i++) {
|
|
26
|
-
if (unsafeCharCodeAt(data, i) !== BACK_SLASH) {
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
const char = unsafeCharCodeAt(data, ++i);
|
|
30
|
-
result.write(data, last, i - 1);
|
|
31
|
-
switch (char) {
|
|
32
|
-
case QUOTE: {
|
|
33
|
-
result.writeCodePoint(QUOTE);
|
|
34
|
-
last = i + 1;
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
case BACK_SLASH: {
|
|
38
|
-
result.writeCodePoint(BACK_SLASH);
|
|
39
|
-
last = i + 1;
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
case FWD_SLASH: {
|
|
43
|
-
result.writeCodePoint(FWD_SLASH);
|
|
44
|
-
last = i + 1;
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
case CHAR_B: {
|
|
48
|
-
result.writeCodePoint(BACKSPACE);
|
|
49
|
-
last = i + 1;
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case CHAR_F: {
|
|
53
|
-
result.writeCodePoint(FORM_FEED);
|
|
54
|
-
last = i + 1;
|
|
55
|
-
break;
|
|
56
|
-
}
|
|
57
|
-
case CHAR_N: {
|
|
58
|
-
result.writeCodePoint(NEW_LINE);
|
|
59
|
-
last = i + 1;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
case CHAR_R: {
|
|
63
|
-
result.writeCodePoint(CARRIAGE_RETURN);
|
|
64
|
-
last = i + 1;
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
case CHAR_T: {
|
|
68
|
-
result.writeCodePoint(TAB);
|
|
69
|
-
last = i + 1;
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
case CHAR_U: {
|
|
73
|
-
const code = u16.parse(data.slice(i + 1, i + 5), 16);
|
|
74
|
-
result.writeCodePoint(code);
|
|
75
|
-
i += 4;
|
|
76
|
-
last = i + 1;
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
default: {
|
|
80
|
-
throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (end > last) {
|
|
85
|
-
result.write(data, last, end);
|
|
86
|
-
}
|
|
87
|
-
return result.toString()
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// @ts-ignore: Decorator valid here
|
|
91
|
-
// @inline export function deserializeString_BS(data: string, start: i32 = 0, end: i32 = 0): void {
|
|
92
|
-
// end = end || data.length - 1;
|
|
93
|
-
// let last = start + 1;
|
|
94
|
-
// for (let i = last; i < end; i++) {
|
|
95
|
-
// if (unsafeCharCodeAt(data, i) !== BACK_SLASH) {
|
|
96
|
-
// continue;
|
|
97
|
-
// }
|
|
98
|
-
// const char = unsafeCharCodeAt(data, ++i);
|
|
99
|
-
// bs.write_s_se_u(data, last, i - 1);
|
|
100
|
-
// switch (char) {
|
|
101
|
-
// case QUOTE: {
|
|
102
|
-
// bs.write_8(QUOTE);
|
|
103
|
-
// last = i + 1;
|
|
104
|
-
// break;
|
|
105
|
-
// }
|
|
106
|
-
// case BACK_SLASH: {
|
|
107
|
-
// bs.write_8(BACK_SLASH);
|
|
108
|
-
// last = i + 1;
|
|
109
|
-
// break;
|
|
110
|
-
// }
|
|
111
|
-
// case FWD_SLASH: {
|
|
112
|
-
// bs.write_8(FWD_SLASH);
|
|
113
|
-
// last = i + 1;
|
|
114
|
-
// break;
|
|
115
|
-
// }
|
|
116
|
-
// case CHAR_B: {
|
|
117
|
-
// bs.write_8(BACKSPACE);
|
|
118
|
-
// last = i + 1;
|
|
119
|
-
// break;
|
|
120
|
-
// }
|
|
121
|
-
// case CHAR_F: {
|
|
122
|
-
// bs.write_8(FORM_FEED);
|
|
123
|
-
// last = i + 1;
|
|
124
|
-
// break;
|
|
125
|
-
// }
|
|
126
|
-
// case CHAR_N: {
|
|
127
|
-
// bs.write_8(NEW_LINE);
|
|
128
|
-
// last = i + 1;
|
|
129
|
-
// break;
|
|
130
|
-
// }
|
|
131
|
-
// case CHAR_R: {
|
|
132
|
-
// bs.write_8(CARRIAGE_RETURN);
|
|
133
|
-
// last = i + 1;
|
|
134
|
-
// break;
|
|
135
|
-
// }
|
|
136
|
-
// case CHAR_T: {
|
|
137
|
-
// bs.write_8(TAB);
|
|
138
|
-
// last = i + 1;
|
|
139
|
-
// break;
|
|
140
|
-
// }
|
|
141
|
-
// case CHAR_U: {
|
|
142
|
-
|
|
143
|
-
// const code = u16.parse(data.slice(i + 1, i + 5), 16);
|
|
144
|
-
// bs.w(code);
|
|
145
|
-
// i += 4;
|
|
146
|
-
// last = i + 1;
|
|
147
|
-
// break;
|
|
148
|
-
// }
|
|
149
|
-
// default: {
|
|
150
|
-
// throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
|
|
151
|
-
// }
|
|
152
|
-
// }
|
|
153
|
-
// }
|
|
154
|
-
// if (end > last) {
|
|
155
|
-
// result.write(data, last, end);
|
|
156
|
-
// }
|
|
157
|
-
// }
|
|
158
|
-
|
|
159
|
-
// @ts-ignore: Decorator valid here
|
|
160
|
-
@inline export function deserializeString_Safe(data: string, start: i32 = 0, end: i32 = 0): string {
|
|
161
|
-
const firstChar = load<u8>(changetype<usize>(data));
|
|
162
|
-
if (firstChar != QUOTE) throw new Error("Mismatched Types! Expected string but got \""+data.slice(0, 100)+"\" instead!");
|
|
163
|
-
return deserializeString(data, start, end);
|
|
164
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { JSON } from "..";
|
|
2
|
-
import {
|
|
3
|
-
COMMA,
|
|
4
|
-
COMMA_WORD,
|
|
5
|
-
EMPTY_BRACKET_WORD,
|
|
6
|
-
BRACKET_LEFT_WORD,
|
|
7
|
-
BRACKET_RIGHT,
|
|
8
|
-
BRACKET_RIGHT_WORD
|
|
9
|
-
} from "../custom/chars";
|
|
10
|
-
import { Sink } from "../custom/sink";
|
|
11
|
-
import { serializeString } from "./string";
|
|
12
|
-
|
|
13
|
-
// @ts-ignore: Decorator valid here
|
|
14
|
-
@inline export function serializeArray<T extends any[]>(data: T): string {
|
|
15
|
-
if (data.length == 0) {
|
|
16
|
-
return EMPTY_BRACKET_WORD;
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
} else if (isString<valueof<T>>()) {
|
|
19
|
-
let result = BRACKET_LEFT_WORD;
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
for (let i = 0; i < data.length - 1; i++) {
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
result += serializeString(unchecked(data[i]));
|
|
24
|
-
result += COMMA_WORD;
|
|
25
|
-
}
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
result += serializeString(unchecked(data[data.length - 1]));
|
|
28
|
-
result += BRACKET_RIGHT_WORD;
|
|
29
|
-
return result;
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
} else if (isBoolean<valueof<T>>()) {
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
return BRACKET_LEFT_WORD + data.join(COMMA_WORD) + BRACKET_RIGHT_WORD;
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
return BRACKET_LEFT_WORD + data.join(COMMA_WORD) + BRACKET_RIGHT_WORD;
|
|
38
|
-
} else {
|
|
39
|
-
let result = Sink.fromString(BRACKET_LEFT_WORD);
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
for (let i = 0; i < data.length - 1; i++) {
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
result.write(JSON.stringify(unchecked(data[i])));
|
|
44
|
-
result.writeCodePoint(COMMA);
|
|
45
|
-
}
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
result.write(JSON.stringify(unchecked(data[data.length - 1])));
|
|
48
|
-
result.writeCodePoint(BRACKET_RIGHT);
|
|
49
|
-
return result.toString();
|
|
50
|
-
}
|
|
51
|
-
}
|