json-as 0.8.5 → 0.8.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/.github/workflows/nodejs.yml +7 -1
- package/CHANGELOG +6 -1
- package/README.md +26 -16
- package/assembly/__tests__/deserialize.spec.ts +298 -0
- package/assembly/__tests__/serialize.spec.ts +375 -0
- package/assembly/deserialize/array/array.ts +31 -0
- package/assembly/deserialize/array/bool.ts +19 -0
- package/assembly/deserialize/array/float.ts +24 -0
- package/assembly/deserialize/array/integer.ts +24 -0
- package/assembly/deserialize/array/map.ts +27 -0
- package/assembly/deserialize/array/object.ts +27 -0
- package/assembly/deserialize/array/string.ts +29 -0
- package/assembly/deserialize/array.ts +37 -0
- package/assembly/deserialize/bool.ts +18 -0
- package/assembly/deserialize/box.ts +17 -0
- package/assembly/deserialize/date.ts +11 -0
- package/assembly/deserialize/float.ts +9 -0
- package/assembly/deserialize/integer.ts +7 -0
- package/assembly/deserialize/map.ts +182 -0
- package/assembly/deserialize/object.ts +136 -0
- package/assembly/deserialize/string.ts +88 -0
- package/assembly/index.d.ts +7 -1
- package/assembly/index.ts +129 -1
- package/assembly/serialize/array.ts +52 -0
- package/assembly/serialize/bool.ts +4 -0
- package/assembly/serialize/box.ts +10 -0
- package/assembly/serialize/date.ts +4 -0
- package/assembly/serialize/float.ts +4 -0
- package/assembly/serialize/integer.ts +5 -0
- package/assembly/serialize/map.ts +24 -0
- package/assembly/serialize/object.ts +7 -0
- package/assembly/serialize/string.ts +64 -0
- package/assembly/src/sink.ts +286 -0
- package/assembly/src/util.ts +6 -0
- package/assembly/test.ts +34 -16
- package/bench/benchmark.ts +7 -3
- package/bench.js +14 -3
- package/index.ts +1 -1
- package/package.json +6 -8
- package/transform/lib/index.js +296 -198
- package/transform/lib/index.old.js +257 -0
- package/transform/lib/types.js +17 -0
- package/transform/package.json +1 -1
- package/transform/src/index.old.ts +312 -0
- package/transform/src/index.ts +298 -234
- package/transform/tsconfig.json +2 -2
- package/tsconfig.json +94 -102
- package/assembly/__benches__/as-json.ts +0 -88
- package/assembly/__benches__/as-tral.d.ts +0 -1
- package/assembly/__tests__/as-json.spec.ts +0 -671
- package/assembly/__tests__/as-pect.d.ts +0 -1
- package/assembly/src/json.ts +0 -941
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import {
|
|
3
|
+
describe,
|
|
4
|
+
expect
|
|
5
|
+
} from "as-test/assembly";
|
|
6
|
+
|
|
7
|
+
@json
|
|
8
|
+
class BaseObject {
|
|
9
|
+
a: string;
|
|
10
|
+
constructor(a: string) {
|
|
11
|
+
this.a = a;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@json
|
|
16
|
+
class DerivedObject extends BaseObject {
|
|
17
|
+
b: string;
|
|
18
|
+
constructor(a: string, b: string) {
|
|
19
|
+
super(a);
|
|
20
|
+
this.b = b;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@json
|
|
25
|
+
class Map4 {
|
|
26
|
+
a: string;
|
|
27
|
+
b: string;
|
|
28
|
+
c: string;
|
|
29
|
+
d: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@json
|
|
33
|
+
class Vec3 {
|
|
34
|
+
x: f64;
|
|
35
|
+
y: f64;
|
|
36
|
+
z: f64;
|
|
37
|
+
|
|
38
|
+
static shouldIgnore: string = "should not be serialized";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@json
|
|
42
|
+
class Player {
|
|
43
|
+
firstName: string;
|
|
44
|
+
lastName: string;
|
|
45
|
+
lastActive: i32[];
|
|
46
|
+
age: i32;
|
|
47
|
+
pos: Vec3 | null;
|
|
48
|
+
isVerified: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class Nullable { }
|
|
52
|
+
type Null = Nullable | null;
|
|
53
|
+
|
|
54
|
+
describe("Should serialize strings", () => {
|
|
55
|
+
|
|
56
|
+
expect(
|
|
57
|
+
JSON.stringify("abcdefg")
|
|
58
|
+
).toBe("\"abcdefg\"");
|
|
59
|
+
|
|
60
|
+
expect(
|
|
61
|
+
JSON.stringify('st"ring" w""ith quotes"')
|
|
62
|
+
).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
|
|
63
|
+
|
|
64
|
+
expect(
|
|
65
|
+
JSON.stringify('string \"with random spa\nces and \nnewlines\n\n\n')
|
|
66
|
+
).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
|
|
67
|
+
|
|
68
|
+
expect(
|
|
69
|
+
JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"')
|
|
70
|
+
).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\\\""')
|
|
71
|
+
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("Should serialize integers", () => {
|
|
75
|
+
|
|
76
|
+
expect(
|
|
77
|
+
JSON.stringify(0)
|
|
78
|
+
).toBe("0");
|
|
79
|
+
|
|
80
|
+
expect(
|
|
81
|
+
JSON.stringify<u32>(100)
|
|
82
|
+
).toBe("100");
|
|
83
|
+
|
|
84
|
+
expect(
|
|
85
|
+
JSON.stringify<u64>(101)
|
|
86
|
+
).toBe("101");
|
|
87
|
+
|
|
88
|
+
expect(
|
|
89
|
+
JSON.stringify<i32>(-100)
|
|
90
|
+
).toBe("-100");
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
JSON.stringify<i64>(-101)
|
|
94
|
+
).toBe("-101");
|
|
95
|
+
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("Should serialize floats", () => {
|
|
99
|
+
|
|
100
|
+
expect(
|
|
101
|
+
JSON.stringify<f64>(7.23)
|
|
102
|
+
).toBe("7.23");
|
|
103
|
+
|
|
104
|
+
expect(
|
|
105
|
+
JSON.stringify<f64>(10e2)
|
|
106
|
+
).toBe("1000.0");
|
|
107
|
+
|
|
108
|
+
expect(
|
|
109
|
+
JSON.stringify<f64>(123456e-5)
|
|
110
|
+
).toBe("1.23456");
|
|
111
|
+
|
|
112
|
+
expect(
|
|
113
|
+
JSON.stringify<f64>(0.0)
|
|
114
|
+
).toBe("0.0");
|
|
115
|
+
|
|
116
|
+
expect(
|
|
117
|
+
JSON.stringify<f64>(-7.23)
|
|
118
|
+
).toBe("-7.23");
|
|
119
|
+
|
|
120
|
+
expect(
|
|
121
|
+
JSON.stringify<f64>(1e-6)
|
|
122
|
+
).toBe("0.000001");
|
|
123
|
+
|
|
124
|
+
expect(
|
|
125
|
+
JSON.stringify<f64>(1e-7)
|
|
126
|
+
).toBe("1e-7");
|
|
127
|
+
|
|
128
|
+
expect(
|
|
129
|
+
JSON.parse<f64>("1E-7")
|
|
130
|
+
).toBe(1e-7);
|
|
131
|
+
|
|
132
|
+
expect(
|
|
133
|
+
JSON.stringify<f64>(1e20)
|
|
134
|
+
).toBe("100000000000000000000.0");
|
|
135
|
+
|
|
136
|
+
expect(
|
|
137
|
+
JSON.stringify<f64>(1e21)
|
|
138
|
+
).toBe("1e+21");
|
|
139
|
+
|
|
140
|
+
expect(
|
|
141
|
+
JSON.parse<f64>("1E+21")
|
|
142
|
+
).toBe(1e21);
|
|
143
|
+
|
|
144
|
+
expect(
|
|
145
|
+
JSON.parse<f64>("1e21")
|
|
146
|
+
).toBe(1e21);
|
|
147
|
+
|
|
148
|
+
expect(
|
|
149
|
+
JSON.parse<f64>("1E21")
|
|
150
|
+
).toBe(1e21);
|
|
151
|
+
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("Should serialize booleans", () => {
|
|
155
|
+
|
|
156
|
+
expect(
|
|
157
|
+
JSON.stringify<bool>(true)
|
|
158
|
+
).toBe("true");
|
|
159
|
+
|
|
160
|
+
expect(
|
|
161
|
+
JSON.stringify<bool>(false)
|
|
162
|
+
).toBe("false");
|
|
163
|
+
|
|
164
|
+
expect(
|
|
165
|
+
JSON.stringify<boolean>(true)
|
|
166
|
+
).toBe("true");
|
|
167
|
+
|
|
168
|
+
expect(
|
|
169
|
+
JSON.stringify<boolean>(false)
|
|
170
|
+
).toBe("false");
|
|
171
|
+
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("Should serialize class inheritance", () => {
|
|
175
|
+
|
|
176
|
+
const obj = new DerivedObject("1", "2");
|
|
177
|
+
|
|
178
|
+
expect(
|
|
179
|
+
JSON.stringify(obj)
|
|
180
|
+
).toBe("{\"a\":\"1\",\"b\":\"2\"}");
|
|
181
|
+
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("Should serialize nulls", () => {
|
|
185
|
+
|
|
186
|
+
expect(
|
|
187
|
+
JSON.stringify<Null>(null)
|
|
188
|
+
).toBe("null");
|
|
189
|
+
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
describe("Should serialize integer arrays", () => {
|
|
194
|
+
|
|
195
|
+
expect(
|
|
196
|
+
JSON.stringify<u32[]>([0, 100, 101])
|
|
197
|
+
).toBe("[0,100,101]");
|
|
198
|
+
|
|
199
|
+
expect(
|
|
200
|
+
JSON.stringify<u64[]>([0, 100, 101])
|
|
201
|
+
).toBe("[0,100,101]");
|
|
202
|
+
|
|
203
|
+
expect(
|
|
204
|
+
JSON.stringify<i32[]>([0, 100, 101, -100, -101])
|
|
205
|
+
).toBe("[0,100,101,-100,-101]");
|
|
206
|
+
|
|
207
|
+
expect(
|
|
208
|
+
JSON.stringify<i64[]>([0, 100, 101, -100, -101])
|
|
209
|
+
).toBe("[0,100,101,-100,-101]");
|
|
210
|
+
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe("Should serialize float arrays", () => {
|
|
214
|
+
|
|
215
|
+
expect(
|
|
216
|
+
JSON.stringify<f64[]>([7.23, 10e2, 10e2, 123456e-5, 123456e-5, 0.0, 7.23])
|
|
217
|
+
).toBe("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]");
|
|
218
|
+
|
|
219
|
+
expect(
|
|
220
|
+
JSON.stringify<f64[]>([1e21, 1e22, 1e-7, 1e-8, 1e-9])
|
|
221
|
+
).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
|
|
222
|
+
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe("Should serialize boolean arrays", () => {
|
|
226
|
+
|
|
227
|
+
expect(
|
|
228
|
+
JSON.stringify<bool[]>([true, false])
|
|
229
|
+
).toBe("[true,false]");
|
|
230
|
+
|
|
231
|
+
expect(
|
|
232
|
+
JSON.stringify<boolean[]>([true, false])
|
|
233
|
+
).toBe("[true,false]");
|
|
234
|
+
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("Should serialize string arrays", () => {
|
|
238
|
+
|
|
239
|
+
expect(
|
|
240
|
+
JSON.stringify<string[]>(['string \"with random spa\nces and \nnewlines\n\n\n'])
|
|
241
|
+
).toBe('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]');
|
|
242
|
+
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("Should serialize nested integer arrays", () => {
|
|
246
|
+
|
|
247
|
+
expect(
|
|
248
|
+
JSON.stringify<i64[][]>([[100, 101], [-100, -101], [0]])
|
|
249
|
+
).toBe("[[100,101],[-100,-101],[0]]");
|
|
250
|
+
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe("Should serialize nested float arrays", () => {
|
|
254
|
+
|
|
255
|
+
expect(
|
|
256
|
+
JSON.stringify<f64[][]>([
|
|
257
|
+
[7.23],
|
|
258
|
+
[10e2],
|
|
259
|
+
[10e2],
|
|
260
|
+
[123456e-5],
|
|
261
|
+
[123456e-5],
|
|
262
|
+
[0.0],
|
|
263
|
+
[7.23],
|
|
264
|
+
])
|
|
265
|
+
).toBe("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]");
|
|
266
|
+
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe("Should serialize nested boolean arrays", () => {
|
|
270
|
+
|
|
271
|
+
expect(
|
|
272
|
+
JSON.stringify<bool[][]>([[true], [false]])
|
|
273
|
+
).toBe("[[true],[false]]");
|
|
274
|
+
|
|
275
|
+
expect(
|
|
276
|
+
JSON.stringify<boolean[][]>([[true], [false]])
|
|
277
|
+
).toBe("[[true],[false]]");
|
|
278
|
+
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("Should serialize object arrays", () => {
|
|
282
|
+
|
|
283
|
+
expect(
|
|
284
|
+
JSON.stringify<Vec3[]>([
|
|
285
|
+
{
|
|
286
|
+
x: 3.4,
|
|
287
|
+
y: 1.2,
|
|
288
|
+
z: 8.3,
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
x: 3.4,
|
|
292
|
+
y: -2.1,
|
|
293
|
+
z: 9.3,
|
|
294
|
+
},
|
|
295
|
+
])
|
|
296
|
+
).toBe('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
|
|
297
|
+
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("Ser/de Objects", () => {
|
|
301
|
+
|
|
302
|
+
expect(
|
|
303
|
+
JSON.stringify<Vec3>({
|
|
304
|
+
x: 3.4,
|
|
305
|
+
y: 1.2,
|
|
306
|
+
z: 8.3,
|
|
307
|
+
})
|
|
308
|
+
).toBe('{"x":3.4,"y":1.2,"z":8.3}');
|
|
309
|
+
|
|
310
|
+
expect(
|
|
311
|
+
JSON.stringify<Player>({
|
|
312
|
+
firstName: "Emmet",
|
|
313
|
+
lastName: "West",
|
|
314
|
+
lastActive: [8, 27, 2022],
|
|
315
|
+
age: 23,
|
|
316
|
+
pos: {
|
|
317
|
+
x: 3.4,
|
|
318
|
+
y: 1.2,
|
|
319
|
+
z: 8.3,
|
|
320
|
+
},
|
|
321
|
+
isVerified: true,
|
|
322
|
+
})
|
|
323
|
+
).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
324
|
+
|
|
325
|
+
expect(
|
|
326
|
+
JSON.stringify<ObjectWithFloat>({ f: 7.23 })
|
|
327
|
+
).toBe('{"f":7.23}');
|
|
328
|
+
|
|
329
|
+
expect(
|
|
330
|
+
JSON.stringify<ObjectWithFloat>({ f: 0.000001 })
|
|
331
|
+
).toBe('{"f":0.000001}');
|
|
332
|
+
|
|
333
|
+
expect(
|
|
334
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e-7 })
|
|
335
|
+
).toBe('{"f":1e-7}');
|
|
336
|
+
|
|
337
|
+
expect(
|
|
338
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e20 })
|
|
339
|
+
).toBe('{"f":100000000000000000000.0}');
|
|
340
|
+
|
|
341
|
+
expect(
|
|
342
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e21 })
|
|
343
|
+
).toBe('{"f":1e+21}');
|
|
344
|
+
|
|
345
|
+
expect(
|
|
346
|
+
JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })
|
|
347
|
+
).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
|
|
348
|
+
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
@json
|
|
352
|
+
class ObjWithString {
|
|
353
|
+
s!: string;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
@json
|
|
357
|
+
class ObjectWithStringArray {
|
|
358
|
+
sa!: string[];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@json
|
|
362
|
+
class ObjectWithFloat {
|
|
363
|
+
f!: f64;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
@json
|
|
367
|
+
class ObjectWithFloatArray {
|
|
368
|
+
fa!: f64[];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
@json
|
|
372
|
+
class ObjWithStrangeKey<T> {
|
|
373
|
+
@alias('a\\\t"\x02b`c')
|
|
374
|
+
data!: T;
|
|
375
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { leftBracketCode, rightBracketCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../..";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
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 === leftBracketCode) {
|
|
17
|
+
if (depth === 0) {
|
|
18
|
+
lastPos = i;
|
|
19
|
+
}
|
|
20
|
+
// Shifting is 6% faster than incrementing
|
|
21
|
+
depth++;
|
|
22
|
+
} else if (char === rightBracketCode) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { eCode, fCode, tCode } from "../../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { deserializeBoolean } from "../bool";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
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 === tCode || char === fCode) {
|
|
12
|
+
lastPos = i;
|
|
13
|
+
} else if (char === eCode) {
|
|
14
|
+
i++;
|
|
15
|
+
result.push(deserializeBoolean(data.slice(lastPos, i)));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isSpace } from "util/string";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { commaCode, rightBracketCode } from "../../src/chars";
|
|
4
|
+
import { deserializeFloat } from "../float";
|
|
5
|
+
|
|
6
|
+
// @ts-ignore: Decorator
|
|
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 == commaCode || char == rightBracketCode) && lastPos > 0) {
|
|
18
|
+
awaitingParse = false;
|
|
19
|
+
result.push(deserializeFloat<valueof<T>>(data.slice(lastPos, i)));
|
|
20
|
+
lastPos = 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isSpace } from "util/string";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { commaCode, rightBracketCode } from "../../src/chars";
|
|
4
|
+
import { deserializeInteger } from "../integer";
|
|
5
|
+
|
|
6
|
+
// @ts-ignore: Decorator
|
|
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 == commaCode || char == rightBracketCode) && lastPos > 0) {
|
|
18
|
+
awaitingParse = false;
|
|
19
|
+
result.push(deserializeInteger<valueof<T>>(data.slice(lastPos, i)));
|
|
20
|
+
lastPos = 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { leftBraceCode, rightBraceCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../..";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
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 === leftBraceCode) {
|
|
13
|
+
if (depth === 0) {
|
|
14
|
+
lastPos = pos;
|
|
15
|
+
}
|
|
16
|
+
depth++;
|
|
17
|
+
} else if (char === rightBraceCode) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { leftBraceCode, rightBraceCode } from "../../src/chars";
|
|
2
|
+
import { JSON } from "../..";
|
|
3
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
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 === leftBraceCode) {
|
|
13
|
+
if (depth === 0) {
|
|
14
|
+
lastPos = pos;
|
|
15
|
+
}
|
|
16
|
+
depth++;
|
|
17
|
+
} else if (char === rightBraceCode) {
|
|
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
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { backSlashCode, quoteCode } from "../../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../../src/util";
|
|
3
|
+
import { deserializeString } from "../string";
|
|
4
|
+
|
|
5
|
+
// @ts-ignore: Decorator
|
|
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 === backSlashCode && !escaping) {
|
|
14
|
+
escaping = true;
|
|
15
|
+
} else {
|
|
16
|
+
if (char === quoteCode && !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
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isMap } from "../src/util";
|
|
2
|
+
import { deserializeArrayArray } from "./array/array";
|
|
3
|
+
import { deserializeBooleanArray } from "./array/bool";
|
|
4
|
+
import { deserializeFloatArray } from "./array/float";
|
|
5
|
+
import { deserializeIntegerArray } from "./array/integer";
|
|
6
|
+
import { deserializeMapArray } from "./array/map";
|
|
7
|
+
import { deserializeObjectArray } from "./array/object";
|
|
8
|
+
import { deserializeStringArray } from "./array/string";
|
|
9
|
+
|
|
10
|
+
// @ts-ignore: Decorator
|
|
11
|
+
@inline export function deserializeArray<T extends unknown[]>(data: string): T {
|
|
12
|
+
if (isString<valueof<T>>()) {
|
|
13
|
+
return <T>deserializeStringArray(data);
|
|
14
|
+
} else if (isBoolean<valueof<T>>()) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
return deserializeBooleanArray<T>(data);
|
|
17
|
+
} else if (isInteger<valueof<T>>()) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
return deserializeIntegerArray<T>(data);
|
|
20
|
+
} else if (isFloat<valueof<T>>()) {
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
return deserializeFloatArray<T>(data);
|
|
23
|
+
} else if (isArrayLike<valueof<T>>()) {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
return deserializeArrayArray<T>(data);
|
|
26
|
+
} else if (isMap<valueof<T>>()) {
|
|
27
|
+
return deserializeMapArray<T>(data);
|
|
28
|
+
} else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) {
|
|
29
|
+
const type = changetype<nonnull<valueof<T>>>(0);
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
if (isDefined(type.__DESERIALIZE)) {
|
|
32
|
+
return deserializeObjectArray<T>(data);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new Error("Could not parse array of type " + nameof<T>() + "!");
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { fCode, tCode } from "../src/chars";
|
|
2
|
+
import { unsafeCharCodeAt } from "../src/util";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Deserialize a string to type boolean
|
|
6
|
+
* @param data data to parse
|
|
7
|
+
* @returns boolean
|
|
8
|
+
*/
|
|
9
|
+
// @ts-ignore: Decorator
|
|
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 === tCode && load<u64>(ptr) === 28429475166421108) return true;
|
|
16
|
+
else if (len === 5 && firstChar === fCode && load<u64>(ptr, 2) === 28429466576093281) return false;
|
|
17
|
+
return false//ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
|
|
3
|
+
// @ts-ignore: Decorator
|
|
4
|
+
@inline export function deserializeBox<T extends Box<any>>(data: string): T {
|
|
5
|
+
if (isNullable<T>() && data == "null") {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const instance = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()))// as Box<usize>;
|
|
9
|
+
const val = instance._val;
|
|
10
|
+
instance._val = parseDirectInference(val, data);
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
return changetype<T>(instance);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@inline function parseDirectInference<T>(type: T, data: string, initializeDefaultValues: boolean = false): T {
|
|
16
|
+
return JSON.parse<T>(data, initializeDefaultValues)
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
@inline export function deserializeDate(dateTimeString: string): Date {
|
|
3
|
+
// Use AssemblyScript's date parser
|
|
4
|
+
const d = Date.fromString(dateTimeString);
|
|
5
|
+
|
|
6
|
+
// Return a new object instead of the one that the parser returned.
|
|
7
|
+
// This may seem redundant, but addreses the issue when Date
|
|
8
|
+
// is globally aliased to wasi_Date (or some other superclass).
|
|
9
|
+
return new Date(d.getTime());
|
|
10
|
+
}
|
|
11
|
+
|