json-as 0.4.4 → 0.4.7
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/README.md +9 -5
- package/as-pect.asconfig.json +23 -23
- package/asconfig.json +1 -16
- package/assembly/__benches__/benchmark.ts +33 -27
- package/assembly/__tests__/as-json.spec.ts +84 -0
- package/assembly/chars.ts +2 -1
- package/assembly/index.ts +297 -154
- package/assembly/test.ts +29 -55
- package/assembly/util.ts +30 -21
- package/index.ts +0 -1
- package/package.json +4 -3
- package/transform/lib/index.js +74 -73
- package/transform/package.json +2 -3
- package/transform/src/index.ts +108 -95
- package/assembly/__tests__/example.spec.ts +0 -40
package/assembly/index.ts
CHANGED
|
@@ -1,22 +1,48 @@
|
|
|
1
1
|
import { StringSink } from "as-string-sink/assembly";
|
|
2
2
|
import { Variant } from "as-variant/assembly";
|
|
3
|
+
import { isSpace } from "assemblyscript/std/assembly/util/string";
|
|
4
|
+
import { stringify } from "as-console/assembly";
|
|
3
5
|
import {
|
|
4
6
|
backSlashCode,
|
|
5
7
|
colonCode,
|
|
6
8
|
commaCode,
|
|
9
|
+
eCode,
|
|
10
|
+
fCode,
|
|
11
|
+
forwardSlashCode,
|
|
7
12
|
leftBraceCode,
|
|
8
13
|
leftBracketCode,
|
|
9
14
|
quoteCode,
|
|
10
15
|
rightBraceCode,
|
|
11
|
-
rightBracketCode
|
|
16
|
+
rightBracketCode,
|
|
17
|
+
tCode,
|
|
12
18
|
} from "./chars";
|
|
13
|
-
import { removeWhitespace } from "./util";
|
|
19
|
+
import { removeWhitespace, unsafeCharCodeAt } from "./util";
|
|
14
20
|
|
|
15
21
|
/**
|
|
16
22
|
* JSON Encoder/Decoder for AssemblyScript
|
|
17
23
|
*/
|
|
18
|
-
export
|
|
19
|
-
|
|
24
|
+
export class JSON {
|
|
25
|
+
private static parseObjectValue<T>(data: string): T {
|
|
26
|
+
let type!: T;
|
|
27
|
+
if (isString<T>()) {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
return data.replaceAll('\\"', '"');
|
|
30
|
+
} else if (isBoolean<T>()) {
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
return parseBoolean<T>(data);
|
|
33
|
+
} else if (isFloat<T>() || isInteger<T>()) {
|
|
34
|
+
return parseNumber<T>(data);
|
|
35
|
+
} else if (isArrayLike<T>()) {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
return parseArray<T>(data);
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
40
|
+
return parseObject<T>(data);
|
|
41
|
+
} else {
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
20
46
|
/**
|
|
21
47
|
* Stringifies valid JSON data.
|
|
22
48
|
* ```js
|
|
@@ -25,7 +51,7 @@ export namespace JSON {
|
|
|
25
51
|
* @param data T
|
|
26
52
|
* @returns string
|
|
27
53
|
*/
|
|
28
|
-
|
|
54
|
+
static stringify<T = Nullable | null>(data: T): string {
|
|
29
55
|
// String
|
|
30
56
|
if (isString<T>()) {
|
|
31
57
|
return '"' + (<string>data).replaceAll('"', '\\"') + '"';
|
|
@@ -77,8 +103,7 @@ export namespace JSON {
|
|
|
77
103
|
* @param data string
|
|
78
104
|
* @returns T
|
|
79
105
|
*/
|
|
80
|
-
|
|
81
|
-
data = removeWhitespace(data);
|
|
106
|
+
static parse<T = Variant>(data: string): T {
|
|
82
107
|
let type!: T;
|
|
83
108
|
if (isString<T>()) {
|
|
84
109
|
// @ts-ignore
|
|
@@ -89,53 +114,11 @@ export namespace JSON {
|
|
|
89
114
|
} else if (isFloat<T>() || isInteger<T>()) {
|
|
90
115
|
return parseNumber<T>(data);
|
|
91
116
|
} else if (isArrayLike<T>()) {
|
|
92
|
-
return parseArray<T>(data);
|
|
93
117
|
// @ts-ignore
|
|
94
|
-
|
|
95
|
-
const len: u32 = data.length - 1
|
|
96
|
-
let schema!: T
|
|
97
|
-
const result = new Map<string, string>()
|
|
98
|
-
let lastPos: u32 = 1
|
|
99
|
-
let key: string = ''
|
|
100
|
-
let instr: boolean = false
|
|
101
|
-
let char: u32 = 0
|
|
102
|
-
let depth: u32 = 0
|
|
103
|
-
let fdepth: u32 = 0
|
|
104
|
-
for (let i: u32 = 1; i < len; i++) {
|
|
105
|
-
char = data.charCodeAt(i);
|
|
106
|
-
if (instr === false && char === quoteCode) instr = true;
|
|
107
|
-
else if (instr === true && char === quoteCode && data.charCodeAt(i - 1) !== "/".charCodeAt(0)) instr = false;
|
|
108
|
-
if (instr === false) {
|
|
109
|
-
if (char === leftBraceCode || char === leftBracketCode) depth++
|
|
110
|
-
if (char === rightBraceCode || char === rightBracketCode) fdepth++
|
|
111
|
-
}
|
|
112
|
-
if (depth !== 0 && depth === fdepth) {
|
|
113
|
-
//console.log(`Found Struct: ${data.slice(lastPos + 1, i + 1)}`)
|
|
114
|
-
result.set(key, data.slice(lastPos + 1, i + 1))
|
|
115
|
-
// Reset the depth
|
|
116
|
-
depth = 0
|
|
117
|
-
fdepth = 0
|
|
118
|
-
// Set new lastPos
|
|
119
|
-
lastPos = i + 1
|
|
120
|
-
}
|
|
121
|
-
if (!instr && depth === 0) {
|
|
122
|
-
if (char === colonCode) {
|
|
123
|
-
key = data.slice(lastPos + 1, i - 1)
|
|
124
|
-
//console.log(`Found Key: ${data.slice(lastPos + 1, i - 1)}`)
|
|
125
|
-
lastPos = i
|
|
126
|
-
} else if (char === commaCode) {
|
|
127
|
-
//console.log(`Found Comma: ${data.slice(lastPos + 1, i)}`)
|
|
128
|
-
if ((i - lastPos) > 0) result.set(key, data.slice(lastPos + 1, i))
|
|
129
|
-
lastPos = i + 1
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if ((len - lastPos) > 1 && (len - lastPos) !== 0) {
|
|
135
|
-
result.set(key, data.slice(lastPos + 1, len))
|
|
136
|
-
}
|
|
118
|
+
return parseArray<T>(data.trimStart());
|
|
137
119
|
// @ts-ignore
|
|
138
|
-
|
|
120
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
121
|
+
return parseObject<T>(data.trimStart());
|
|
139
122
|
} else {
|
|
140
123
|
// @ts-ignore
|
|
141
124
|
return null;
|
|
@@ -143,23 +126,23 @@ export namespace JSON {
|
|
|
143
126
|
}
|
|
144
127
|
}
|
|
145
128
|
|
|
146
|
-
|
|
147
129
|
// @ts-ignore
|
|
148
130
|
@inline
|
|
149
|
-
|
|
131
|
+
function parseString(data: string): string {
|
|
150
132
|
return data.slice(1, data.length - 1).replaceAll('\\"', '"');
|
|
151
133
|
}
|
|
152
134
|
|
|
153
135
|
// @ts-ignore
|
|
154
136
|
@inline
|
|
155
|
-
|
|
137
|
+
function parseBoolean<T extends boolean>(data: string): T {
|
|
156
138
|
if (data.length > 3 && data.startsWith("true")) return <T>true;
|
|
157
139
|
else if (data.length > 4 && data.startsWith("false")) return <T>false;
|
|
158
140
|
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
|
|
159
141
|
}
|
|
142
|
+
|
|
160
143
|
// @ts-ignore
|
|
161
144
|
@inline
|
|
162
|
-
|
|
145
|
+
function parseNumber<T>(data: string): T {
|
|
163
146
|
let type: T;
|
|
164
147
|
// @ts-ignore
|
|
165
148
|
if (type instanceof f64) return F64.parseFloat(data);
|
|
@@ -185,127 +168,287 @@ export namespace JSON {
|
|
|
185
168
|
|
|
186
169
|
// @ts-ignore
|
|
187
170
|
@inline
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
let
|
|
192
|
-
let
|
|
193
|
-
let
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
171
|
+
export function parseObject<T>(data: string): T {
|
|
172
|
+
let schema!: T;
|
|
173
|
+
const result = new Map<string, string>();
|
|
174
|
+
let key = "";
|
|
175
|
+
let isKey = false;
|
|
176
|
+
let depth = 1;
|
|
177
|
+
let char = 0;
|
|
178
|
+
for (
|
|
179
|
+
let outerLoopIndex = 1;
|
|
180
|
+
outerLoopIndex < data.length - 1;
|
|
181
|
+
outerLoopIndex++
|
|
182
|
+
) {
|
|
183
|
+
char = unsafeCharCodeAt(data, outerLoopIndex);
|
|
184
|
+
if (char === leftBracketCode) {
|
|
185
|
+
for (
|
|
186
|
+
let arrayValueIndex = outerLoopIndex;
|
|
187
|
+
arrayValueIndex < data.length - 1;
|
|
188
|
+
arrayValueIndex++
|
|
189
|
+
) {
|
|
190
|
+
char = unsafeCharCodeAt(data, arrayValueIndex);
|
|
191
|
+
if (char === leftBracketCode) {
|
|
192
|
+
depth = depth << 1;
|
|
193
|
+
} else if (char === rightBracketCode) {
|
|
194
|
+
depth = depth >> 1;
|
|
195
|
+
if (depth === 1) {
|
|
196
|
+
++arrayValueIndex;
|
|
197
|
+
result.set(
|
|
198
|
+
key,
|
|
199
|
+
data.slice(outerLoopIndex, arrayValueIndex)
|
|
200
|
+
);
|
|
201
|
+
outerLoopIndex = arrayValueIndex;
|
|
202
|
+
isKey = false;
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
} else if (char === leftBraceCode) {
|
|
208
|
+
for (
|
|
209
|
+
let objectValueIndex = outerLoopIndex;
|
|
210
|
+
objectValueIndex < data.length - 1;
|
|
211
|
+
objectValueIndex++
|
|
212
|
+
) {
|
|
213
|
+
char = unsafeCharCodeAt(data, objectValueIndex);
|
|
214
|
+
if (char === leftBraceCode) {
|
|
215
|
+
depth = depth << 1;
|
|
216
|
+
} else if (char === rightBraceCode) {
|
|
217
|
+
depth = depth >> 1;
|
|
218
|
+
if (depth === 1) {
|
|
219
|
+
++objectValueIndex;
|
|
220
|
+
result.set(
|
|
221
|
+
key,
|
|
222
|
+
data.slice(outerLoopIndex, objectValueIndex)
|
|
223
|
+
);
|
|
224
|
+
outerLoopIndex = objectValueIndex;
|
|
225
|
+
isKey = false;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
} else if (char === quoteCode) {
|
|
231
|
+
for (
|
|
232
|
+
let stringValueIndex = ++outerLoopIndex;
|
|
233
|
+
stringValueIndex < data.length - 1;
|
|
234
|
+
stringValueIndex++
|
|
235
|
+
) {
|
|
236
|
+
char = unsafeCharCodeAt(data, stringValueIndex);
|
|
237
|
+
if (
|
|
238
|
+
char === quoteCode &&
|
|
239
|
+
unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
|
|
240
|
+
) {
|
|
241
|
+
if (isKey === false) {
|
|
242
|
+
key = data.slice(outerLoopIndex, stringValueIndex);
|
|
243
|
+
isKey = true;
|
|
244
|
+
} else {
|
|
245
|
+
result.set(
|
|
246
|
+
key,
|
|
247
|
+
data.slice(outerLoopIndex, stringValueIndex)
|
|
248
|
+
);
|
|
249
|
+
isKey = false;
|
|
250
|
+
}
|
|
251
|
+
outerLoopIndex = ++stringValueIndex;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
} else if (
|
|
256
|
+
char === tCode &&
|
|
257
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "r".charCodeAt(0) &&
|
|
258
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "u".charCodeAt(0) &&
|
|
259
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
260
|
+
) {
|
|
261
|
+
result.set(key, "true");
|
|
262
|
+
isKey = false;
|
|
263
|
+
} else if (
|
|
264
|
+
char === fCode &&
|
|
265
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "a".charCodeAt(0) &&
|
|
266
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "l".charCodeAt(0) &&
|
|
267
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === "s".charCodeAt(0) &&
|
|
268
|
+
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
|
|
269
|
+
) {
|
|
270
|
+
result.set(key, "false");
|
|
271
|
+
isKey = false;
|
|
272
|
+
} else if (char >= 48 && char <= 57) {
|
|
273
|
+
let numberValueIndex = ++outerLoopIndex;
|
|
274
|
+
for (; numberValueIndex < data.length - 1; numberValueIndex++) {
|
|
275
|
+
char = unsafeCharCodeAt(data, numberValueIndex);
|
|
276
|
+
if (
|
|
277
|
+
char === commaCode ||
|
|
278
|
+
isSpace(char) ||
|
|
279
|
+
numberValueIndex == data.length - 2
|
|
280
|
+
) {
|
|
281
|
+
result.set(
|
|
282
|
+
key,
|
|
283
|
+
data.slice(outerLoopIndex - 1, numberValueIndex)
|
|
284
|
+
);
|
|
285
|
+
outerLoopIndex = numberValueIndex;
|
|
286
|
+
isKey = false;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
201
290
|
}
|
|
202
291
|
}
|
|
203
|
-
//console.log(data.slice(lastPos, data.length - 1))
|
|
204
292
|
// @ts-ignore
|
|
205
|
-
|
|
293
|
+
return schema.__JSON_Deserialize(result);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// @ts-ignore
|
|
297
|
+
@inline
|
|
298
|
+
// @ts-ignore
|
|
299
|
+
export function parseArray<T extends unknown[]>(data: string): T {
|
|
300
|
+
// TODO: Replace with opt
|
|
301
|
+
let type!: valueof<T>;
|
|
302
|
+
if (type instanceof String) {
|
|
303
|
+
return <T>parseStringArray(data);
|
|
304
|
+
} else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
|
|
305
|
+
// @ts-ignore
|
|
306
|
+
return parseNumberArray<T>(data);
|
|
307
|
+
} else if (isBoolean<valueof<T>>()) {
|
|
308
|
+
// @ts-ignore
|
|
309
|
+
return parseBooleanArray<T>(data);
|
|
310
|
+
} else if (isArrayLike<valueof<T>>()) {
|
|
206
311
|
// @ts-ignore
|
|
207
|
-
|
|
208
|
-
|
|
312
|
+
return parseArrayArray<T>(data);
|
|
313
|
+
// @ts-ignore
|
|
314
|
+
} else if (isDefined(type.__JSON_Deserialize)) {
|
|
315
|
+
// @ts-ignore
|
|
316
|
+
return parseObjectArray<T>(data);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// @ts-ignore
|
|
321
|
+
@inline
|
|
322
|
+
export function parseStringArray(data: string): string[] {
|
|
323
|
+
const result: string[] = [];
|
|
324
|
+
let lastPos = 0;
|
|
325
|
+
let instr = false;
|
|
326
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
327
|
+
if (unsafeCharCodeAt(data, i) === quoteCode) {
|
|
328
|
+
if (instr === false) {
|
|
329
|
+
instr = true;
|
|
330
|
+
lastPos = i;
|
|
331
|
+
} else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
|
|
332
|
+
instr = false;
|
|
333
|
+
result.push(data.slice(lastPos + 1, i).replaceAll('\\"', '"'));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
209
337
|
return result;
|
|
210
338
|
}
|
|
211
339
|
|
|
212
340
|
// @ts-ignore
|
|
213
341
|
@inline
|
|
214
|
-
|
|
342
|
+
export function parseBooleanArray<T extends boolean[]>(data: string): T {
|
|
215
343
|
const result = instantiate<T>();
|
|
216
|
-
|
|
217
|
-
let
|
|
218
|
-
let i
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
char
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
344
|
+
let lastPos = 1;
|
|
345
|
+
let char = 0;
|
|
346
|
+
for (let i = 1; i < data.length - 1; i++) {
|
|
347
|
+
char = unsafeCharCodeAt(data, i);
|
|
348
|
+
/*// if char == "t" && i+3 == "e"
|
|
349
|
+
if (char === tCode && data.charCodeAt(i + 3) === eCode) {
|
|
350
|
+
//i += 3;
|
|
351
|
+
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
|
|
352
|
+
//i++;
|
|
353
|
+
} else if (char === fCode && data.charCodeAt(i + 4) === eCode) {
|
|
354
|
+
//i += 4;
|
|
355
|
+
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
|
|
356
|
+
//i++;
|
|
357
|
+
}*/
|
|
358
|
+
if (char === tCode || char === fCode) {
|
|
359
|
+
lastPos = i;
|
|
360
|
+
} else if (char === eCode) {
|
|
361
|
+
i++;
|
|
362
|
+
result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i)));
|
|
226
363
|
}
|
|
227
364
|
}
|
|
228
|
-
// @ts-ignore
|
|
229
|
-
result.push(
|
|
230
|
-
// @ts-ignore
|
|
231
|
-
parseBoolean<valueof<T>>(data.slice(lastPos, data.length - 1).trimStart())
|
|
232
|
-
);
|
|
233
365
|
return result;
|
|
234
366
|
}
|
|
235
367
|
|
|
236
368
|
// @ts-ignore
|
|
237
369
|
@inline
|
|
238
|
-
|
|
370
|
+
export function parseNumberArray<T extends number[]>(data: string): T {
|
|
239
371
|
const result = instantiate<T>();
|
|
240
|
-
|
|
241
|
-
let
|
|
242
|
-
let
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
let inDepth: u32 = 0;
|
|
251
|
-
let outDepth: u32 = 0;
|
|
252
|
-
for (; i < len; i++) {
|
|
253
|
-
char = data.charCodeAt(i);
|
|
254
|
-
if (char == quoteCode && data.charCodeAt(i - 1) != backSlashCode)
|
|
255
|
-
isStr = !isStr;
|
|
256
|
-
if (char == leftBraceCode || char == leftBracketCode) {
|
|
257
|
-
inDepth++;
|
|
258
|
-
isStruct = true;
|
|
259
|
-
} else if (char == rightBraceCode || char == rightBracketCode) {
|
|
260
|
-
outDepth++;
|
|
261
|
-
isStruct = true;
|
|
372
|
+
let lastPos = 0;
|
|
373
|
+
let char = 0;
|
|
374
|
+
let i = 1;
|
|
375
|
+
for (; i < data.length - 1; i++) {
|
|
376
|
+
char = unsafeCharCodeAt(data, i);
|
|
377
|
+
if (lastPos === 0 && char >= 48 && char <= 57) {
|
|
378
|
+
lastPos = i;
|
|
379
|
+
} else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
|
|
380
|
+
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
|
|
381
|
+
lastPos = 0;
|
|
262
382
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (isSpace(char)) offset++;
|
|
270
|
-
}*/
|
|
271
|
-
// This checks to see if we are dealing with structures such as Objects and Arrays
|
|
272
|
-
if (char == commaCode) {
|
|
273
|
-
// @ts-ignore
|
|
274
|
-
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i).trim()));
|
|
275
|
-
//offset = 0;
|
|
276
|
-
lastPos = i + 1;
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
if (inDepth == outDepth) {
|
|
280
|
-
i++;
|
|
281
|
-
//console.log(`Struct-${data.slice(lastPos, i).trim()}-`)
|
|
282
|
-
lastPos = i + 1;
|
|
283
|
-
inDepth = 0;
|
|
284
|
-
outDepth = 0;
|
|
285
|
-
isStruct = false;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
383
|
+
}
|
|
384
|
+
for (; i > lastPos; i--) {
|
|
385
|
+
char = unsafeCharCodeAt(data, i);
|
|
386
|
+
if (char !== rightBracketCode) {
|
|
387
|
+
result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
|
|
388
|
+
break;
|
|
288
389
|
}
|
|
289
390
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// @ts-ignore
|
|
395
|
+
@inline
|
|
396
|
+
export function parseArrayArray<T extends unknown[][]>(data: string): T {
|
|
397
|
+
const result = instantiate<T>();
|
|
398
|
+
let char = 0;
|
|
399
|
+
let lastPos = 0;
|
|
400
|
+
let depth = 1;
|
|
401
|
+
let i = 1;
|
|
402
|
+
// Find start of bracket
|
|
403
|
+
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
|
|
404
|
+
//i++;
|
|
405
|
+
for (; i < data.length - 1; i++) {
|
|
406
|
+
char = unsafeCharCodeAt(data, i);
|
|
407
|
+
if (char === leftBracketCode) {
|
|
408
|
+
if (depth === 1) {
|
|
409
|
+
lastPos = i;
|
|
410
|
+
}
|
|
411
|
+
// Shifting is 6% faster than incrementing
|
|
412
|
+
depth = depth << 1;
|
|
413
|
+
} else if (char === rightBracketCode) {
|
|
414
|
+
depth = depth >> 1;
|
|
415
|
+
if (depth === 1) {
|
|
416
|
+
i++;
|
|
417
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
418
|
+
}
|
|
419
|
+
}
|
|
295
420
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
len--;
|
|
299
|
-
char = data.charCodeAt(len);
|
|
300
|
-
}*/
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
301
423
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
424
|
+
// @ts-ignore
|
|
425
|
+
@inline
|
|
426
|
+
export function parseObjectArray<T extends unknown[][]>(data: string): T {
|
|
427
|
+
const result = instantiate<T>();
|
|
428
|
+
let char = 0;
|
|
429
|
+
let lastPos = 1;
|
|
430
|
+
let depth = 1;
|
|
431
|
+
let i = 1;
|
|
432
|
+
// Find start of bracket
|
|
433
|
+
//for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
|
|
434
|
+
//i++;
|
|
435
|
+
for (; i < data.length - 1; i++) {
|
|
436
|
+
char = unsafeCharCodeAt(data, i);
|
|
437
|
+
if (char === leftBraceCode) {
|
|
438
|
+
if (depth === 1) {
|
|
439
|
+
lastPos = i;
|
|
440
|
+
}
|
|
441
|
+
// Shifting is 6% faster than incrementing
|
|
442
|
+
depth = depth << 1;
|
|
443
|
+
} else if (char === rightBraceCode) {
|
|
444
|
+
depth = depth >> 1;
|
|
445
|
+
if (depth === 1) {
|
|
446
|
+
i++;
|
|
447
|
+
result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
308
451
|
return result;
|
|
309
452
|
}
|
|
310
453
|
|
|
311
|
-
class Nullable {
|
|
454
|
+
class Nullable {}
|
package/assembly/test.ts
CHANGED
|
@@ -1,75 +1,49 @@
|
|
|
1
1
|
import "wasi";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
JSON
|
|
4
|
+
} from ".";
|
|
4
5
|
|
|
5
6
|
// @ts-ignore
|
|
6
7
|
@json
|
|
7
8
|
class Vec2 {
|
|
8
|
-
x: f32
|
|
9
|
-
y: f32
|
|
9
|
+
x: f32;
|
|
10
|
+
y: f32;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
// @ts-ignore
|
|
13
14
|
@json
|
|
14
15
|
class Player {
|
|
15
|
-
firstName: string
|
|
16
|
-
lastName: string
|
|
17
|
-
lastActive: i32[]
|
|
18
|
-
age: i32
|
|
19
|
-
pos: Vec2
|
|
16
|
+
firstName: string;
|
|
17
|
+
lastName: string;
|
|
18
|
+
lastActive: i32[];
|
|
19
|
+
age: i32;
|
|
20
|
+
pos: Vec2;
|
|
21
|
+
isVerified: boolean;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
const
|
|
24
|
+
const player: Player = {
|
|
23
25
|
firstName: "Emmet",
|
|
24
26
|
lastName: "West",
|
|
25
27
|
lastActive: [8, 27, 2022],
|
|
26
28
|
age: 23,
|
|
27
29
|
pos: {
|
|
28
30
|
x: -3.4,
|
|
29
|
-
y: 1.2
|
|
30
|
-
}
|
|
31
|
+
y: 1.2,
|
|
32
|
+
},
|
|
33
|
+
isVerified: true,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const vec: Vec2 = {
|
|
37
|
+
x: 0.0,
|
|
38
|
+
y: 0.0
|
|
31
39
|
}
|
|
40
|
+
const serializedPlayer = JSON.stringify<Player>(player);
|
|
41
|
+
console.log("Serialized Player: " + serializedPlayer);
|
|
42
|
+
const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
|
|
43
|
+
console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
|
|
44
|
+
|
|
45
|
+
const serializedVec2 = JSON.stringify<Vec2>(vec);
|
|
46
|
+
console.log("Serialized Vec2: " + serializedVec2);
|
|
47
|
+
const deserializedVec2 = JSON.parse<Vec2>(serializedVec2);
|
|
32
48
|
|
|
33
|
-
|
|
34
|
-
// {
|
|
35
|
-
// "firstName": "Emmet",
|
|
36
|
-
// "lastName": "West",
|
|
37
|
-
// "lastActive": [8, 27, 2022],
|
|
38
|
-
// "age": 23,
|
|
39
|
-
// "pos": {
|
|
40
|
-
// "x": -3.4000000953674318,
|
|
41
|
-
// "y": 1.2000000476837159
|
|
42
|
-
// }
|
|
43
|
-
// }
|
|
44
|
-
console.log(`Stringified: ${stringified}`);
|
|
45
|
-
console.log(`Whitespace: ${removeWhitespace(`{
|
|
46
|
-
"firstName": "Emmet",
|
|
47
|
-
"lastName": "West",
|
|
48
|
-
"lastActive": [8, 27, 2022],
|
|
49
|
-
"age": 23,
|
|
50
|
-
"pos": {
|
|
51
|
-
"x": -3.4000000953674318,
|
|
52
|
-
"y": 1.2000000476837159
|
|
53
|
-
}
|
|
54
|
-
}`)}`)
|
|
55
|
-
const parsed = JSON.parse<Player>(`{
|
|
56
|
-
"firstName": "Emmet",
|
|
57
|
-
"lastName": "West",
|
|
58
|
-
"lastActive": [8, 27, 2022],
|
|
59
|
-
"age": 23,
|
|
60
|
-
"pos": {
|
|
61
|
-
"x": -3.4000000953674318,
|
|
62
|
-
"y": 1.2000000476837159
|
|
63
|
-
}
|
|
64
|
-
}`);
|
|
65
|
-
// Player {
|
|
66
|
-
// firstName: "Emmet",
|
|
67
|
-
// lastName: "West",
|
|
68
|
-
// lastActive: [8, 27, 2022],
|
|
69
|
-
// age: 23,
|
|
70
|
-
// pos: {
|
|
71
|
-
// x: -3.4000000953674318,
|
|
72
|
-
// y: 1.2000000476837159
|
|
73
|
-
// }
|
|
74
|
-
// }
|
|
75
|
-
console.log(`Parsed: ${JSON.stringify(parsed)}`);
|
|
49
|
+
console.log("Deserialized: " + JSON.stringify(deserializedVec2));
|