json-as 1.0.0-alpha.2 → 1.0.0-alpha.3
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/.prettierignore +6 -0
- package/.prettierrc.json +0 -1
- package/CHANGELOG +9 -1
- package/README.md +3 -3
- package/as-test.config.json +1 -1
- package/asconfig.json +1 -29
- package/assembly/__tests__/array.spec.ts +67 -0
- package/assembly/__tests__/bool.spec.ts +4 -12
- package/assembly/__tests__/float.spec.ts +11 -21
- package/assembly/__tests__/integer.spec.ts +7 -9
- package/assembly/__tests__/null.spec.ts +12 -0
- package/assembly/__tests__/obj.spec.ts +137 -3
- package/assembly/__tests__/simd/string.spec.ts +21 -21
- package/assembly/__tests__/string.spec.ts +6 -4
- package/assembly/__tests__/test.spec.ts +120 -191
- package/assembly/deserialize/simple/bool.ts +1 -1
- package/assembly/deserialize/simple/map.ts +1 -1
- package/assembly/deserialize/simple/string.ts +4 -3
- package/assembly/globals/tables.ts +74 -416
- package/assembly/index.ts +18 -18
- package/assembly/serialize/simd/string.ts +10 -10
- package/assembly/serialize/simple/array.ts +4 -4
- package/assembly/serialize/simple/bool.ts +2 -2
- package/assembly/serialize/simple/date.ts +1 -1
- package/assembly/serialize/simple/float.ts +1 -1
- package/assembly/serialize/simple/integer.ts +1 -1
- package/assembly/serialize/simple/map.ts +6 -6
- package/assembly/serialize/simple/string.ts +3 -3
- package/assembly/test.ts +4 -43
- package/assembly/util/bytes.ts +1 -1
- package/modules/as-bs/assembly/index.ts +33 -83
- package/modules/test/assembly/index.ts +22 -0
- package/package.json +3 -7
- package/run-tests.sh +15 -0
- package/transform/lib/index.js +1 -1
- package/transform/lib/index.js.map +1 -1
- package/transform/src/index.ts +1 -1
|
@@ -1,193 +1,122 @@
|
|
|
1
|
-
import { JSON } from "
|
|
2
|
-
import { describe, expect
|
|
1
|
+
import { JSON } from "../";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly/index";
|
|
3
3
|
import { DerivedObject, Null, ObjWithStrangeKey, ObjectWithFloat, OmitIf, Player, Vec3 } from "./types";
|
|
4
4
|
|
|
5
|
-
describe("Should serialize
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe("Should
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
describe("Should deserialize nulls", () => {
|
|
126
|
-
expect(JSON.stringify(JSON.parse<Null>("null"))).toBe("null");
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe("Should deserialize integer arrays", () => {
|
|
130
|
-
expect(JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))).toBe(JSON.stringify([0, 100, 101]));
|
|
131
|
-
|
|
132
|
-
expect(JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))).toBe(JSON.stringify([0, 100, 101, -100, -101]));
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe("Should deserialize float arrays", () => {
|
|
136
|
-
expect(JSON.stringify(JSON.parse<f64[]>("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]"))).toBe(JSON.stringify([7.23, 1000.0, 1000.0, 1.23456, 1.23456, 0.0, 7.23]));
|
|
137
|
-
|
|
138
|
-
expect(JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))).toBe(JSON.stringify([1e21, 1e22, 1e-7, 1e-8, 1e-9]));
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
describe("Should deserialize boolean arrays", () => {
|
|
142
|
-
expect(JSON.stringify(JSON.parse<boolean[]>("[true,false]"))).toBe(JSON.stringify([true, false]));
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe("Should deserialize string arrays", () => {
|
|
146
|
-
expect(JSON.stringify(JSON.parse<string[]>('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]'))).toBe(JSON.stringify(['string "with random spa\nces and \nnewlines\n\n\n']));
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
describe("Should deserialize nested integer arrays", () => {
|
|
150
|
-
expect(JSON.stringify(JSON.parse<i64[][]>("[[100,101],[-100,-101],[0]]"))).toBe(JSON.stringify([[100, 101], [-100, -101], [0]]));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe("Should deserialize nested float arrays", () => {
|
|
154
|
-
expect(JSON.stringify(JSON.parse<f64[][]>("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]"))).toBe(JSON.stringify([[7.23], [1000.0], [1000.0], [1.23456], [1.23456], [0.0], [7.23]]));
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe("Should deserialize nested boolean arrays", () => {
|
|
158
|
-
expect(JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))).toBe(JSON.stringify([[true], [false]]));
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
describe("Should deserialize object arrays", () => {
|
|
162
|
-
expect(JSON.stringify(JSON.parse<Vec3[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]'))).toBe(
|
|
163
|
-
JSON.stringify(<Vec3[]>[
|
|
164
|
-
{ x: 3.4, y: 1.2, z: 8.3 },
|
|
165
|
-
{ x: 3.4, y: -2.1, z: 9.3 },
|
|
166
|
-
]),
|
|
167
|
-
);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
describe("Should deserialize Objects", () => {
|
|
171
|
-
expect(JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}'))).toBe(JSON.stringify(<Vec3>{ x: 3.4, y: 1.2, z: 8.3 }));
|
|
172
|
-
|
|
173
|
-
expect(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}'))).toBe(
|
|
174
|
-
JSON.stringify(<Player>{
|
|
175
|
-
firstName: "Emmet",
|
|
176
|
-
lastName: "West",
|
|
177
|
-
lastActive: [8, 27, 2022],
|
|
178
|
-
age: 23,
|
|
179
|
-
pos: { x: 3.4, y: 1.2, z: 8.3 },
|
|
180
|
-
isVerified: true,
|
|
181
|
-
}),
|
|
182
|
-
);
|
|
183
|
-
|
|
184
|
-
expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":7.23}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 7.23 }));
|
|
185
|
-
|
|
186
|
-
expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":0.000001}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 0.000001 }));
|
|
187
|
-
|
|
188
|
-
expect(JSON.stringify(JSON.parse<ObjWithStrangeKey<string>>('{"a\\\\\\t\\"\\u0002b`c":"foo"}'))).toBe(JSON.stringify(<ObjWithStrangeKey<string>>{ data: "foo" }));
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
run({
|
|
192
|
-
log: true,
|
|
193
|
-
});
|
|
5
|
+
// describe("Should serialize objects", () => {
|
|
6
|
+
// expect(
|
|
7
|
+
// JSON.stringify<Vec3>({
|
|
8
|
+
// x: 3.4,
|
|
9
|
+
// y: 1.2,
|
|
10
|
+
// z: 8.3,
|
|
11
|
+
// }),
|
|
12
|
+
// ).toBe('{"x":3.4,"y":1.2,"z":8.3}');
|
|
13
|
+
|
|
14
|
+
// expect(
|
|
15
|
+
// JSON.stringify<Player>({
|
|
16
|
+
// firstName: "Emmet",
|
|
17
|
+
// lastName: "West",
|
|
18
|
+
// lastActive: [8, 27, 2022],
|
|
19
|
+
// age: 23,
|
|
20
|
+
// pos: {
|
|
21
|
+
// x: 3.4,
|
|
22
|
+
// y: 1.2,
|
|
23
|
+
// z: 8.3,
|
|
24
|
+
// },
|
|
25
|
+
// isVerified: true,
|
|
26
|
+
// }),
|
|
27
|
+
// ).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
28
|
+
|
|
29
|
+
// expect(JSON.stringify<ObjectWithFloat>({ f: 7.23 })).toBe('{"f":7.23}');
|
|
30
|
+
|
|
31
|
+
// expect(JSON.stringify<ObjectWithFloat>({ f: 0.000001 })).toBe('{"f":0.000001}');
|
|
32
|
+
|
|
33
|
+
// expect(JSON.stringify<ObjectWithFloat>({ f: 1e-7 })).toBe('{"f":1e-7}');
|
|
34
|
+
|
|
35
|
+
// expect(JSON.stringify<ObjectWithFloat>({ f: 1e20 })).toBe('{"f":100000000000000000000.0}');
|
|
36
|
+
|
|
37
|
+
// expect(JSON.stringify<ObjectWithFloat>({ f: 1e21 })).toBe('{"f":1e+21}');
|
|
38
|
+
|
|
39
|
+
// expect(JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
|
|
40
|
+
// });
|
|
41
|
+
|
|
42
|
+
// describe("Should serialize @omit'ed objects", () => {
|
|
43
|
+
// expect(
|
|
44
|
+
// JSON.stringify(<OmitIf>{
|
|
45
|
+
// y: 1,
|
|
46
|
+
// }),
|
|
47
|
+
// ).toBe('{"x":1,"y":1,"z":1}');
|
|
48
|
+
// });
|
|
49
|
+
// describe("Should deserialize class inheritance", () => {
|
|
50
|
+
// const jsonStr = '{"a":"1","b":"2"}';
|
|
51
|
+
// const obj = JSON.parse<DerivedObject>(jsonStr);
|
|
52
|
+
|
|
53
|
+
// expect((obj instanceof DerivedObject).toString()).toBe("true");
|
|
54
|
+
// expect(obj.a).toBe("1");
|
|
55
|
+
// expect(obj.b).toBe("2");
|
|
56
|
+
// });
|
|
57
|
+
|
|
58
|
+
// describe("Should deserialize nulls", () => {
|
|
59
|
+
// expect(JSON.stringify(JSON.parse<Null>("null"))).toBe("null");
|
|
60
|
+
// });
|
|
61
|
+
|
|
62
|
+
// describe("Should deserialize integer arrays", () => {
|
|
63
|
+
// expect(JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))).toBe(JSON.stringify([0, 100, 101]));
|
|
64
|
+
|
|
65
|
+
// expect(JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))).toBe(JSON.stringify([0, 100, 101, -100, -101]));
|
|
66
|
+
// });
|
|
67
|
+
|
|
68
|
+
// describe("Should deserialize float arrays", () => {
|
|
69
|
+
// expect(JSON.stringify(JSON.parse<f64[]>("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]"))).toBe(JSON.stringify([7.23, 1000.0, 1000.0, 1.23456, 1.23456, 0.0, 7.23]));
|
|
70
|
+
|
|
71
|
+
// expect(JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))).toBe(JSON.stringify([1e21, 1e22, 1e-7, 1e-8, 1e-9]));
|
|
72
|
+
// });
|
|
73
|
+
|
|
74
|
+
// describe("Should deserialize boolean arrays", () => {
|
|
75
|
+
// expect(JSON.stringify(JSON.parse<boolean[]>("[true,false]"))).toBe(JSON.stringify([true, false]));
|
|
76
|
+
// });
|
|
77
|
+
|
|
78
|
+
// describe("Should deserialize string arrays", () => {
|
|
79
|
+
// expect(JSON.stringify(JSON.parse<string[]>('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]'))).toBe(JSON.stringify(['string "with random spa\nces and \nnewlines\n\n\n']));
|
|
80
|
+
// });
|
|
81
|
+
|
|
82
|
+
// describe("Should deserialize nested integer arrays", () => {
|
|
83
|
+
// expect(JSON.stringify(JSON.parse<i64[][]>("[[100,101],[-100,-101],[0]]"))).toBe(JSON.stringify([[100, 101], [-100, -101], [0]]));
|
|
84
|
+
// });
|
|
85
|
+
|
|
86
|
+
// describe("Should deserialize nested float arrays", () => {
|
|
87
|
+
// expect(JSON.stringify(JSON.parse<f64[][]>("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]"))).toBe(JSON.stringify([[7.23], [1000.0], [1000.0], [1.23456], [1.23456], [0.0], [7.23]]));
|
|
88
|
+
// });
|
|
89
|
+
|
|
90
|
+
// describe("Should deserialize nested boolean arrays", () => {
|
|
91
|
+
// expect(JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))).toBe(JSON.stringify([[true], [false]]));
|
|
92
|
+
// });
|
|
93
|
+
|
|
94
|
+
// describe("Should deserialize object arrays", () => {
|
|
95
|
+
// expect(JSON.stringify(JSON.parse<Vec3[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]'))).toBe(
|
|
96
|
+
// JSON.stringify(<Vec3[]>[
|
|
97
|
+
// { x: 3.4, y: 1.2, z: 8.3 },
|
|
98
|
+
// { x: 3.4, y: -2.1, z: 9.3 },
|
|
99
|
+
// ]),
|
|
100
|
+
// );
|
|
101
|
+
// });
|
|
102
|
+
|
|
103
|
+
// describe("Should deserialize Objects", () => {
|
|
104
|
+
// expect(JSON.stringify(JSON.parse<Vec3>('{"x":3.4,"y":1.2,"z":8.3}'))).toBe(JSON.stringify(<Vec3>{ x: 3.4, y: 1.2, z: 8.3 }));
|
|
105
|
+
|
|
106
|
+
// expect(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}'))).toBe(
|
|
107
|
+
// JSON.stringify(<Player>{
|
|
108
|
+
// firstName: "Emmet",
|
|
109
|
+
// lastName: "West",
|
|
110
|
+
// lastActive: [8, 27, 2022],
|
|
111
|
+
// age: 23,
|
|
112
|
+
// pos: { x: 3.4, y: 1.2, z: 8.3 },
|
|
113
|
+
// isVerified: true,
|
|
114
|
+
// }),
|
|
115
|
+
// );
|
|
116
|
+
|
|
117
|
+
// expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":7.23}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 7.23 }));
|
|
118
|
+
|
|
119
|
+
// expect(JSON.stringify(JSON.parse<ObjectWithFloat>('{"f":0.000001}'))).toBe(JSON.stringify(<ObjectWithFloat>{ f: 0.000001 }));
|
|
120
|
+
|
|
121
|
+
// expect(JSON.stringify(JSON.parse<ObjWithStrangeKey<string>>('{"a\\\\\\t\\"\\u0002b`c":"foo"}'))).toBe(JSON.stringify(<ObjWithStrangeKey<string>>{ data: "foo" }));
|
|
122
|
+
// });
|
|
@@ -5,5 +5,5 @@ export function deserializeBoolean(srcStart: usize, srcEnd: usize): boolean {
|
|
|
5
5
|
const firstChar = load<u16>(srcStart);
|
|
6
6
|
if (firstChar == CHAR_T && load<u64>(srcStart) == 28429475166421108) return true;
|
|
7
7
|
else if (firstChar == CHAR_F && load<u64>(srcSize, 2) == 28429466576093281) return false;
|
|
8
|
-
return false; //
|
|
8
|
+
return false; //throw new Error(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
|
|
9
9
|
}
|
|
@@ -5,7 +5,7 @@ import { isSpace } from "../../util";
|
|
|
5
5
|
export function deserializeMap<T extends Map<any, any>>(srcStart: usize, srcEnd: usize, dst: usize): T {
|
|
6
6
|
const out = changetype<T>(dst || __new(offsetof<T>(), idof<T>()));
|
|
7
7
|
// @ts-ignore: type
|
|
8
|
-
if (!isString<indexof<T>>() && !isInteger<indexof<T>>() && !isFloat<indexof<T>>())
|
|
8
|
+
if (!isString<indexof<T>>() && !isInteger<indexof<T>>() && !isFloat<indexof<T>>()) throw new Error("Map key must also be a valid JSON key!");
|
|
9
9
|
|
|
10
10
|
const srcPtr = srcStart;
|
|
11
11
|
let key: string | null = null;
|
|
@@ -4,13 +4,14 @@ import { DESERIALIZE_ESCAPE_TABLE, ESCAPE_HEX_TABLE } from "../../globals/tables
|
|
|
4
4
|
export function deserializeString(srcStart: usize, srcEnd: usize, dst: usize): string {
|
|
5
5
|
srcStart += 2;
|
|
6
6
|
srcEnd -= 2;
|
|
7
|
+
const startPtr = srcStart;
|
|
7
8
|
if (dst == 0) dst = __new(srcEnd - srcStart, idof<string>());
|
|
8
9
|
let dstPtr = dst;
|
|
9
10
|
let lastPtr = srcStart;
|
|
10
11
|
while (srcStart < srcEnd) {
|
|
11
12
|
let code = load<u16>(srcStart);
|
|
12
13
|
if (code == BACK_SLASH) {
|
|
13
|
-
code =
|
|
14
|
+
code = <u16>load<u8>(DESERIALIZE_ESCAPE_TABLE + load<u8>(srcStart, 2));
|
|
14
15
|
if (code == 117 && load<u32>(srcStart, 4) == 3145776) {
|
|
15
16
|
const block = load<u32>(srcStart, 8);
|
|
16
17
|
const codeA = block & 0xffff;
|
|
@@ -21,7 +22,7 @@ export function deserializeString(srcStart: usize, srcEnd: usize, dst: usize): s
|
|
|
21
22
|
const remBytes = srcStart - lastPtr;
|
|
22
23
|
memory.copy(dstPtr, lastPtr, remBytes);
|
|
23
24
|
dstPtr += remBytes;
|
|
24
|
-
store<u16>(
|
|
25
|
+
store<u16>(dstPtr, escaped);
|
|
25
26
|
dstPtr += 2;
|
|
26
27
|
srcStart += 12;
|
|
27
28
|
lastPtr = srcStart;
|
|
@@ -43,6 +44,6 @@ export function deserializeString(srcStart: usize, srcEnd: usize, dst: usize): s
|
|
|
43
44
|
memory.copy(dstPtr, lastPtr, remBytes);
|
|
44
45
|
dstPtr += remBytes;
|
|
45
46
|
|
|
46
|
-
if (lastPtr !=
|
|
47
|
+
if (lastPtr != startPtr) dst = __renew(dst, dstPtr - dst);
|
|
47
48
|
return changetype<string>(dst);
|
|
48
49
|
}
|