json-as 0.9.7 → 0.9.8-b
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 +9 -10
- package/CHANGELOG +11 -2
- package/README.md +52 -7
- package/asconfig.json +24 -3
- package/assembly/__tests__/test.spec.ts +564 -0
- package/assembly/__tests__/types.ts +82 -0
- package/assembly/{src/chars.ts → chars.ts} +36 -36
- package/assembly/deserialize/array/array.ts +4 -4
- package/assembly/deserialize/array/bool.ts +4 -4
- package/assembly/deserialize/array/float.ts +4 -4
- package/assembly/deserialize/array/integer.ts +4 -4
- package/assembly/deserialize/array/map.ts +4 -4
- package/assembly/deserialize/array/object.ts +4 -4
- package/assembly/deserialize/array/string.ts +4 -4
- package/assembly/deserialize/array.ts +5 -4
- package/assembly/deserialize/bool.ts +4 -4
- package/assembly/deserialize/date.ts +2 -2
- package/assembly/deserialize/float.ts +2 -2
- package/assembly/deserialize/integer.ts +3 -3
- package/assembly/deserialize/map.ts +4 -4
- package/assembly/deserialize/mpz.ts +12 -0
- package/assembly/deserialize/object.ts +4 -4
- package/assembly/deserialize/string.ts +5 -5
- package/assembly/index.ts +25 -23
- package/assembly/serialize/array.ts +6 -5
- package/assembly/serialize/bool.ts +2 -2
- package/assembly/serialize/date.ts +2 -2
- package/assembly/serialize/float.ts +2 -2
- package/assembly/serialize/integer.ts +2 -2
- package/assembly/serialize/map.ts +8 -7
- package/assembly/serialize/mpz.ts +6 -0
- package/assembly/serialize/object.ts +2 -2
- package/assembly/serialize/string.ts +5 -5
- package/assembly/serialize/unknown.ts +5 -5
- package/assembly/test.ts +10 -47
- package/assembly/types.ts +4 -0
- package/assembly/{src/util.ts → util.ts} +3 -3
- package/bench/benchmark.ts +1 -1
- package/build/test.spec.wasm +0 -0
- package/build/test.spec.wasm.map +1 -0
- package/build/test.spec.wat +112616 -0
- package/build/test.wasm +0 -0
- package/build/test.wasm.map +1 -0
- package/build/test.wat +12267 -0
- package/package.json +13 -14
- package/transform/lib/visitor.js +516 -0
- package/transform/package.json +1 -1
- package/transform/src/visitor.ts +543 -0
- package/transform/tsconfig.json +23 -63
- package/tsconfig.json +13 -13
- package/as-pect.asconfig.json +0 -24
- package/as-pect.config.js +0 -30
- package/assembly/__tests__/deserialize.spec.ts +0 -301
- package/assembly/__tests__/serialize.spec.ts +0 -398
- package/assembly/deserialize/box.ts +0 -17
- package/assembly/serialize/box.ts +0 -11
- package/develop/assembly/serialize/unknown.ts +0 -46
- package/transform/lib/index.old.js +0 -257
- package/transform/lib/types.js +0 -17
- package/transform/src/index.old.ts +0 -312
- /package/assembly/{src/sink.ts → sink.ts} +0 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import {
|
|
3
|
+
describe,
|
|
4
|
+
expect,
|
|
5
|
+
run
|
|
6
|
+
} from "as-test/assembly";
|
|
7
|
+
import { DerivedObject, Null, ObjWithStrangeKey, ObjectWithFloat, OmitIf, Player, Vec3 } from "./types";
|
|
8
|
+
import { MpZ } from "@hypercubed/as-mpz";
|
|
9
|
+
|
|
10
|
+
describe("Should serialize strings", () => {
|
|
11
|
+
|
|
12
|
+
expect(
|
|
13
|
+
JSON.stringify("abcdefg")
|
|
14
|
+
).toBe("\"abcdefg\"");
|
|
15
|
+
|
|
16
|
+
expect(
|
|
17
|
+
JSON.stringify('st"ring" w""ith quotes"')
|
|
18
|
+
).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
|
|
19
|
+
|
|
20
|
+
expect(
|
|
21
|
+
JSON.stringify('string \"with random spa\nces and \nnewlines\n\n\n')
|
|
22
|
+
).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
|
|
23
|
+
|
|
24
|
+
expect(
|
|
25
|
+
JSON.stringify('string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"')
|
|
26
|
+
).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\\\""')
|
|
27
|
+
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("Should serialize integers", () => {
|
|
31
|
+
|
|
32
|
+
expect(
|
|
33
|
+
JSON.stringify(0)
|
|
34
|
+
).toBe("0");
|
|
35
|
+
|
|
36
|
+
expect(
|
|
37
|
+
JSON.stringify<u32>(100)
|
|
38
|
+
).toBe("100");
|
|
39
|
+
|
|
40
|
+
expect(
|
|
41
|
+
JSON.stringify<u64>(101)
|
|
42
|
+
).toBe("101");
|
|
43
|
+
|
|
44
|
+
expect(
|
|
45
|
+
JSON.stringify<i32>(-100)
|
|
46
|
+
).toBe("-100");
|
|
47
|
+
|
|
48
|
+
expect(
|
|
49
|
+
JSON.stringify<i64>(-101)
|
|
50
|
+
).toBe("-101");
|
|
51
|
+
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("Should serialize MpZ (Big Int)", () => {
|
|
55
|
+
|
|
56
|
+
expect(
|
|
57
|
+
JSON.stringify<MpZ>(MpZ.from(0))
|
|
58
|
+
).toBe("0");
|
|
59
|
+
|
|
60
|
+
expect(
|
|
61
|
+
JSON.stringify<MpZ>(MpZ.from(2).pow(512))
|
|
62
|
+
).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
|
|
63
|
+
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("Should serialize floats", () => {
|
|
67
|
+
|
|
68
|
+
expect(
|
|
69
|
+
JSON.stringify<f64>(7.23)
|
|
70
|
+
).toBe("7.23");
|
|
71
|
+
|
|
72
|
+
expect(
|
|
73
|
+
JSON.stringify<f64>(10e2)
|
|
74
|
+
).toBe("1000.0");
|
|
75
|
+
|
|
76
|
+
expect(
|
|
77
|
+
JSON.stringify<f64>(123456e-5)
|
|
78
|
+
).toBe("1.23456");
|
|
79
|
+
|
|
80
|
+
expect(
|
|
81
|
+
JSON.stringify<f64>(0.0)
|
|
82
|
+
).toBe("0.0");
|
|
83
|
+
|
|
84
|
+
expect(
|
|
85
|
+
JSON.stringify<f64>(-7.23)
|
|
86
|
+
).toBe("-7.23");
|
|
87
|
+
|
|
88
|
+
expect(
|
|
89
|
+
JSON.stringify<f64>(1e-6)
|
|
90
|
+
).toBe("0.000001");
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
JSON.stringify<f64>(1e-7)
|
|
94
|
+
).toBe("1e-7");
|
|
95
|
+
|
|
96
|
+
expect(
|
|
97
|
+
JSON.parse<f64>("1E-7")
|
|
98
|
+
).toBe(1e-7);
|
|
99
|
+
|
|
100
|
+
expect(
|
|
101
|
+
JSON.stringify<f64>(1e20)
|
|
102
|
+
).toBe("100000000000000000000.0");
|
|
103
|
+
|
|
104
|
+
expect(
|
|
105
|
+
JSON.stringify<f64>(1e21)
|
|
106
|
+
).toBe("1e+21");
|
|
107
|
+
|
|
108
|
+
expect(
|
|
109
|
+
JSON.parse<f64>("1E+21")
|
|
110
|
+
).toBe(1e21);
|
|
111
|
+
|
|
112
|
+
expect(
|
|
113
|
+
JSON.parse<f64>("1e21")
|
|
114
|
+
).toBe(1e21);
|
|
115
|
+
|
|
116
|
+
expect(
|
|
117
|
+
JSON.parse<f64>("1E21")
|
|
118
|
+
).toBe(1e21);
|
|
119
|
+
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("Should serialize booleans", () => {
|
|
123
|
+
|
|
124
|
+
expect(
|
|
125
|
+
JSON.stringify<bool>(true)
|
|
126
|
+
).toBe("true");
|
|
127
|
+
|
|
128
|
+
expect(
|
|
129
|
+
JSON.stringify<bool>(false)
|
|
130
|
+
).toBe("false");
|
|
131
|
+
|
|
132
|
+
expect(
|
|
133
|
+
JSON.stringify<boolean>(true)
|
|
134
|
+
).toBe("true");
|
|
135
|
+
|
|
136
|
+
expect(
|
|
137
|
+
JSON.stringify<boolean>(false)
|
|
138
|
+
).toBe("false");
|
|
139
|
+
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("Should serialize class inheritance", () => {
|
|
143
|
+
|
|
144
|
+
const obj = new DerivedObject("1", "2");
|
|
145
|
+
|
|
146
|
+
expect(
|
|
147
|
+
JSON.stringify(obj)
|
|
148
|
+
).toBe("{\"a\":\"1\",\"b\":\"2\"}");
|
|
149
|
+
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("Should serialize nulls", () => {
|
|
153
|
+
|
|
154
|
+
expect(
|
|
155
|
+
JSON.stringify<Null>(null)
|
|
156
|
+
).toBe("null");
|
|
157
|
+
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
describe("Should serialize integer arrays", () => {
|
|
162
|
+
|
|
163
|
+
expect(
|
|
164
|
+
JSON.stringify<u32[]>([0, 100, 101])
|
|
165
|
+
).toBe("[0,100,101]");
|
|
166
|
+
|
|
167
|
+
expect(
|
|
168
|
+
JSON.stringify<u64[]>([0, 100, 101])
|
|
169
|
+
).toBe("[0,100,101]");
|
|
170
|
+
|
|
171
|
+
expect(
|
|
172
|
+
JSON.stringify<i32[]>([0, 100, 101, -100, -101])
|
|
173
|
+
).toBe("[0,100,101,-100,-101]");
|
|
174
|
+
|
|
175
|
+
expect(
|
|
176
|
+
JSON.stringify<i64[]>([0, 100, 101, -100, -101])
|
|
177
|
+
).toBe("[0,100,101,-100,-101]");
|
|
178
|
+
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("Should serialize float arrays", () => {
|
|
182
|
+
|
|
183
|
+
expect(
|
|
184
|
+
JSON.stringify<f64[]>([7.23, 10e2, 10e2, 123456e-5, 123456e-5, 0.0, 7.23])
|
|
185
|
+
).toBe("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]");
|
|
186
|
+
|
|
187
|
+
expect(
|
|
188
|
+
JSON.stringify<f64[]>([1e21, 1e22, 1e-7, 1e-8, 1e-9])
|
|
189
|
+
).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
|
|
190
|
+
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("Should serialize boolean arrays", () => {
|
|
194
|
+
|
|
195
|
+
expect(
|
|
196
|
+
JSON.stringify<bool[]>([true, false])
|
|
197
|
+
).toBe("[true,false]");
|
|
198
|
+
|
|
199
|
+
expect(
|
|
200
|
+
JSON.stringify<boolean[]>([true, false])
|
|
201
|
+
).toBe("[true,false]");
|
|
202
|
+
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("Should serialize string arrays", () => {
|
|
206
|
+
|
|
207
|
+
expect(
|
|
208
|
+
JSON.stringify<string[]>(['string \"with random spa\nces and \nnewlines\n\n\n'])
|
|
209
|
+
).toBe('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]');
|
|
210
|
+
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe("Should serialize nested integer arrays", () => {
|
|
214
|
+
|
|
215
|
+
expect(
|
|
216
|
+
JSON.stringify<i64[][]>([[100, 101], [-100, -101], [0]])
|
|
217
|
+
).toBe("[[100,101],[-100,-101],[0]]");
|
|
218
|
+
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("Should serialize nested float arrays", () => {
|
|
222
|
+
|
|
223
|
+
expect(
|
|
224
|
+
JSON.stringify<f64[][]>([
|
|
225
|
+
[7.23],
|
|
226
|
+
[10e2],
|
|
227
|
+
[10e2],
|
|
228
|
+
[123456e-5],
|
|
229
|
+
[123456e-5],
|
|
230
|
+
[0.0],
|
|
231
|
+
[7.23],
|
|
232
|
+
])
|
|
233
|
+
).toBe("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]");
|
|
234
|
+
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("Should serialize nested boolean arrays", () => {
|
|
238
|
+
|
|
239
|
+
expect(
|
|
240
|
+
JSON.stringify<bool[][]>([[true], [false]])
|
|
241
|
+
).toBe("[[true],[false]]");
|
|
242
|
+
|
|
243
|
+
expect(
|
|
244
|
+
JSON.stringify<boolean[][]>([[true], [false]])
|
|
245
|
+
).toBe("[[true],[false]]");
|
|
246
|
+
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("Should serialize object arrays", () => {
|
|
250
|
+
|
|
251
|
+
expect(
|
|
252
|
+
JSON.stringify<Vec3[]>([
|
|
253
|
+
{
|
|
254
|
+
x: 3.4,
|
|
255
|
+
y: 1.2,
|
|
256
|
+
z: 8.3,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
x: 3.4,
|
|
260
|
+
y: -2.1,
|
|
261
|
+
z: 9.3,
|
|
262
|
+
},
|
|
263
|
+
])
|
|
264
|
+
).toBe('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
|
|
265
|
+
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe("Should serialize objects", () => {
|
|
269
|
+
|
|
270
|
+
expect(
|
|
271
|
+
JSON.stringify<Vec3>({
|
|
272
|
+
x: 3.4,
|
|
273
|
+
y: 1.2,
|
|
274
|
+
z: 8.3,
|
|
275
|
+
})
|
|
276
|
+
).toBe('{"x":3.4,"y":1.2,"z":8.3}');
|
|
277
|
+
|
|
278
|
+
expect(
|
|
279
|
+
JSON.stringify<Player>({
|
|
280
|
+
firstName: "Emmet",
|
|
281
|
+
lastName: "West",
|
|
282
|
+
lastActive: [8, 27, 2022],
|
|
283
|
+
age: 23,
|
|
284
|
+
pos: {
|
|
285
|
+
x: 3.4,
|
|
286
|
+
y: 1.2,
|
|
287
|
+
z: 8.3,
|
|
288
|
+
},
|
|
289
|
+
isVerified: true,
|
|
290
|
+
})
|
|
291
|
+
).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
292
|
+
|
|
293
|
+
expect(
|
|
294
|
+
JSON.stringify<ObjectWithFloat>({ f: 7.23 })
|
|
295
|
+
).toBe('{"f":7.23}');
|
|
296
|
+
|
|
297
|
+
expect(
|
|
298
|
+
JSON.stringify<ObjectWithFloat>({ f: 0.000001 })
|
|
299
|
+
).toBe('{"f":0.000001}');
|
|
300
|
+
|
|
301
|
+
expect(
|
|
302
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e-7 })
|
|
303
|
+
).toBe('{"f":1e-7}');
|
|
304
|
+
|
|
305
|
+
expect(
|
|
306
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e20 })
|
|
307
|
+
).toBe('{"f":100000000000000000000.0}');
|
|
308
|
+
|
|
309
|
+
expect(
|
|
310
|
+
JSON.stringify<ObjectWithFloat>({ f: 1e21 })
|
|
311
|
+
).toBe('{"f":1e+21}');
|
|
312
|
+
|
|
313
|
+
expect(
|
|
314
|
+
JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })
|
|
315
|
+
).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
|
|
316
|
+
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
describe("Should serialize @omit'ed objects", () => {
|
|
320
|
+
|
|
321
|
+
expect(
|
|
322
|
+
JSON.stringify(<OmitIf>{
|
|
323
|
+
y: 1
|
|
324
|
+
})
|
|
325
|
+
).toBe('{"x":1,"y":1,"z":1}');
|
|
326
|
+
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
describe("Should deserialize strings", () => {
|
|
330
|
+
|
|
331
|
+
expect(
|
|
332
|
+
JSON.parse<string>("\"abcdefg\"")
|
|
333
|
+
).toBe("abcdefg");
|
|
334
|
+
|
|
335
|
+
expect(
|
|
336
|
+
JSON.parse<string>('"\\"st\\\\\\"ring\\\\\\" w\\\\\\"\\\\\\"ith quotes\\\\\\"\\\""')
|
|
337
|
+
).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
|
|
338
|
+
|
|
339
|
+
expect(
|
|
340
|
+
JSON.parse<string>('"\\"string \\\\\\"with random spa\\\\nces and \\\\nnewlines\\\\n\\\\n\\\\n\\""')
|
|
341
|
+
).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
|
|
342
|
+
|
|
343
|
+
expect(
|
|
344
|
+
JSON.parse<string>('"\\"string with colon : comma , brace [ ] bracket { } and quote \\\\\\" and other quote \\\\\\\\\\"\\\""')
|
|
345
|
+
).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\\\""');
|
|
346
|
+
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe("Should deserialize integers", () => {
|
|
350
|
+
|
|
351
|
+
expect(
|
|
352
|
+
JSON.parse<i32>("0")
|
|
353
|
+
).toBe(<i32>0);
|
|
354
|
+
|
|
355
|
+
expect(
|
|
356
|
+
JSON.parse<u32>("100")
|
|
357
|
+
).toBe(<u32>100);
|
|
358
|
+
|
|
359
|
+
expect(
|
|
360
|
+
JSON.parse<u64>("101")
|
|
361
|
+
).toBe(<u64>101);
|
|
362
|
+
|
|
363
|
+
expect(
|
|
364
|
+
JSON.parse<i32>("-100")
|
|
365
|
+
).toBe(<i32>-100);
|
|
366
|
+
|
|
367
|
+
expect(
|
|
368
|
+
JSON.parse<i64>("-101")
|
|
369
|
+
).toBe(<i64>-101);
|
|
370
|
+
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
describe("Should deserialize MpZ (Big Int)", () => {
|
|
374
|
+
|
|
375
|
+
expect(
|
|
376
|
+
JSON.parse<MpZ>("0").toString()
|
|
377
|
+
).toBe("0");
|
|
378
|
+
|
|
379
|
+
expect(
|
|
380
|
+
JSON.parse<MpZ>("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096").toString()
|
|
381
|
+
).toBe("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096");
|
|
382
|
+
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe("Should deserialize floats", () => {
|
|
386
|
+
|
|
387
|
+
expect(
|
|
388
|
+
JSON.parse<f64>("7.23")
|
|
389
|
+
).toBe(7.23);
|
|
390
|
+
|
|
391
|
+
expect(
|
|
392
|
+
JSON.parse<f64>("1000.0")
|
|
393
|
+
).toBe(1000.0);
|
|
394
|
+
|
|
395
|
+
expect(
|
|
396
|
+
JSON.parse<f64>("1.23456")
|
|
397
|
+
).toBe(1.23456);
|
|
398
|
+
|
|
399
|
+
expect(
|
|
400
|
+
JSON.parse<f64>("0.0")
|
|
401
|
+
).toBe(0.0);
|
|
402
|
+
|
|
403
|
+
expect(
|
|
404
|
+
JSON.parse<f64>("-7.23")
|
|
405
|
+
).toBe(-7.23);
|
|
406
|
+
|
|
407
|
+
expect(
|
|
408
|
+
JSON.parse<f64>("0.000001")
|
|
409
|
+
).toBe(0.000001);
|
|
410
|
+
|
|
411
|
+
expect(
|
|
412
|
+
JSON.parse<f64>("1e-7")
|
|
413
|
+
).toBe(1e-7);
|
|
414
|
+
|
|
415
|
+
expect(
|
|
416
|
+
JSON.parse<f64>("100000000000000000000.0")
|
|
417
|
+
).toBe(1e20);
|
|
418
|
+
|
|
419
|
+
expect(
|
|
420
|
+
JSON.parse<f64>("1e+21")
|
|
421
|
+
).toBe(1e21);
|
|
422
|
+
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
describe("Should deserialize booleans", () => {
|
|
426
|
+
|
|
427
|
+
expect(
|
|
428
|
+
JSON.parse<boolean>("true")
|
|
429
|
+
).toBe(true);
|
|
430
|
+
|
|
431
|
+
expect(
|
|
432
|
+
JSON.parse<boolean>("false")
|
|
433
|
+
).toBe(false);
|
|
434
|
+
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
describe("Should deserialize class inheritance", () => {
|
|
438
|
+
|
|
439
|
+
const jsonStr = "{\"a\":\"1\",\"b\":\"2\"}";
|
|
440
|
+
const obj = JSON.parse<DerivedObject>(jsonStr);
|
|
441
|
+
|
|
442
|
+
expect(obj instanceof DerivedObject).toBe(true);
|
|
443
|
+
expect(obj.a).toBe("1");
|
|
444
|
+
expect(obj.b).toBe("2");
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
describe("Should deserialize nulls", () => {
|
|
448
|
+
|
|
449
|
+
expect(
|
|
450
|
+
JSON.stringify(JSON.parse<Null>("null"))
|
|
451
|
+
).toBe("null");
|
|
452
|
+
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
describe("Should deserialize integer arrays", () => {
|
|
456
|
+
|
|
457
|
+
expect(
|
|
458
|
+
JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))
|
|
459
|
+
).toBe(JSON.stringify([0, 100, 101]));
|
|
460
|
+
|
|
461
|
+
expect(
|
|
462
|
+
JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))
|
|
463
|
+
).toBe(JSON.stringify([0, 100, 101, -100, -101]));
|
|
464
|
+
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
describe("Should deserialize float arrays", () => {
|
|
468
|
+
|
|
469
|
+
expect(
|
|
470
|
+
JSON.stringify(JSON.parse<f64[]>("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]"))
|
|
471
|
+
).toBe(JSON.stringify([7.23, 1000.0, 1000.0, 1.23456, 1.23456, 0.0, 7.23]));
|
|
472
|
+
|
|
473
|
+
expect(
|
|
474
|
+
JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))
|
|
475
|
+
).toBe(JSON.stringify([1e21, 1e22, 1e-7, 1e-8, 1e-9]));
|
|
476
|
+
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
describe("Should deserialize boolean arrays", () => {
|
|
480
|
+
|
|
481
|
+
expect(
|
|
482
|
+
JSON.stringify(JSON.parse<boolean[]>("[true,false]"))
|
|
483
|
+
).toBe(JSON.stringify([true, false]));
|
|
484
|
+
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
describe("Should deserialize string arrays", () => {
|
|
488
|
+
|
|
489
|
+
expect(
|
|
490
|
+
JSON.stringify(JSON.parse<string[]>('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]'))
|
|
491
|
+
).toBe(JSON.stringify([ 'string "with random spa\nces and \nnewlines\n\n\n' ]));
|
|
492
|
+
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
describe("Should deserialize nested integer arrays", () => {
|
|
496
|
+
|
|
497
|
+
expect(
|
|
498
|
+
JSON.stringify(JSON.parse<i64[][]>("[[100,101],[-100,-101],[0]]"))
|
|
499
|
+
).toBe(JSON.stringify([[100, 101], [-100, -101], [0]]));
|
|
500
|
+
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
describe("Should deserialize nested float arrays", () => {
|
|
504
|
+
|
|
505
|
+
expect(
|
|
506
|
+
JSON.stringify(JSON.parse<f64[][]>("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]"))
|
|
507
|
+
).toBe(JSON.stringify([[7.23], [1000.0], [1000.0], [1.23456], [1.23456], [0.0], [7.23]]));
|
|
508
|
+
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
describe("Should deserialize nested boolean arrays", () => {
|
|
512
|
+
|
|
513
|
+
expect(
|
|
514
|
+
JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))
|
|
515
|
+
).toBe(JSON.stringify([[true], [false]]));
|
|
516
|
+
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
describe("Should deserialize object arrays", () => {
|
|
520
|
+
|
|
521
|
+
expect(
|
|
522
|
+
JSON.stringify(JSON.parse<Vec3[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]'))
|
|
523
|
+
).toBe(JSON.stringify(<Vec3[]>[
|
|
524
|
+
{ x: 3.4, y: 1.2, z: 8.3 },
|
|
525
|
+
{ x: 3.4, y: -2.1, z: 9.3 }
|
|
526
|
+
]));
|
|
527
|
+
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
describe("Should deserialize Objects", () => {
|
|
531
|
+
|
|
532
|
+
expect(
|
|
533
|
+
JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}'))
|
|
534
|
+
).toBe(JSON.stringify(<Vec3>{ x: 3.4, y: 1.2, z: 8.3 }));
|
|
535
|
+
|
|
536
|
+
expect(
|
|
537
|
+
JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}'))
|
|
538
|
+
).toBe(JSON.stringify(<Player>{
|
|
539
|
+
firstName: "Emmet",
|
|
540
|
+
lastName: "West",
|
|
541
|
+
lastActive: [8, 27, 2022],
|
|
542
|
+
age: 23,
|
|
543
|
+
pos: { x: 3.4, y: 1.2, z: 8.3 },
|
|
544
|
+
isVerified: true
|
|
545
|
+
}));
|
|
546
|
+
|
|
547
|
+
expect(
|
|
548
|
+
JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":7.23}'))
|
|
549
|
+
).toBe(JSON.stringify(<ObjectWithFloat>{ f: 7.23 }));
|
|
550
|
+
|
|
551
|
+
expect(
|
|
552
|
+
JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":0.000001}'))
|
|
553
|
+
).toBe(JSON.stringify(<ObjectWithFloat>{ f: 0.000001 }));
|
|
554
|
+
|
|
555
|
+
expect(
|
|
556
|
+
JSON.stringify(JSON.parse<ObjWithStrangeKey<string>>('{"a\\\\\\t\\"\\u0002b`c":"foo"}'))
|
|
557
|
+
).toBe(JSON.stringify(<ObjWithStrangeKey<string>>{ data: "foo" }));
|
|
558
|
+
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
run({
|
|
562
|
+
log: true,
|
|
563
|
+
coverage: true
|
|
564
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
|
|
2
|
+
@json
|
|
3
|
+
export class ObjWithString {
|
|
4
|
+
s!: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
@json
|
|
8
|
+
export class ObjWithStrangeKey<T> {
|
|
9
|
+
@alias('a\\\t"\x02b`c')
|
|
10
|
+
data!: T;
|
|
11
|
+
}
|
|
12
|
+
@json
|
|
13
|
+
export class ObjectWithStringArray {
|
|
14
|
+
sa!: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@json
|
|
18
|
+
export class ObjectWithFloat {
|
|
19
|
+
f!: f64;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@json
|
|
23
|
+
export class ObjectWithFloatArray {
|
|
24
|
+
fa!: f64[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@json
|
|
28
|
+
export class BaseObject {
|
|
29
|
+
a: string;
|
|
30
|
+
constructor(a: string) {
|
|
31
|
+
this.a = a;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@json
|
|
36
|
+
export class DerivedObject extends BaseObject {
|
|
37
|
+
b: string;
|
|
38
|
+
constructor(a: string, b: string) {
|
|
39
|
+
super(a);
|
|
40
|
+
this.b = b;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@json
|
|
45
|
+
export class Map4 {
|
|
46
|
+
a: string;
|
|
47
|
+
b: string;
|
|
48
|
+
c: string;
|
|
49
|
+
d: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@json
|
|
53
|
+
export class Vec3 {
|
|
54
|
+
x: f64;
|
|
55
|
+
y: f64;
|
|
56
|
+
z: f64;
|
|
57
|
+
|
|
58
|
+
static shouldIgnore: string = "should not be serialized";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@json
|
|
62
|
+
export class Player {
|
|
63
|
+
firstName: string;
|
|
64
|
+
lastName: string;
|
|
65
|
+
lastActive: i32[];
|
|
66
|
+
age: i32;
|
|
67
|
+
pos: Vec3 | null;
|
|
68
|
+
isVerified: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class Nullable { }
|
|
72
|
+
export type Null = Nullable | null;
|
|
73
|
+
|
|
74
|
+
@json
|
|
75
|
+
export class OmitIf {
|
|
76
|
+
x: i32 = 1;
|
|
77
|
+
@omitif("this.y == -1")
|
|
78
|
+
y: i32 = -1;
|
|
79
|
+
z: i32 = 1;
|
|
80
|
+
@omitnull()
|
|
81
|
+
foo: string | null = null
|
|
82
|
+
}
|