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.
Files changed (37) hide show
  1. package/.prettierignore +6 -0
  2. package/.prettierrc.json +0 -1
  3. package/CHANGELOG +9 -1
  4. package/README.md +3 -3
  5. package/as-test.config.json +1 -1
  6. package/asconfig.json +1 -29
  7. package/assembly/__tests__/array.spec.ts +67 -0
  8. package/assembly/__tests__/bool.spec.ts +4 -12
  9. package/assembly/__tests__/float.spec.ts +11 -21
  10. package/assembly/__tests__/integer.spec.ts +7 -9
  11. package/assembly/__tests__/null.spec.ts +12 -0
  12. package/assembly/__tests__/obj.spec.ts +137 -3
  13. package/assembly/__tests__/simd/string.spec.ts +21 -21
  14. package/assembly/__tests__/string.spec.ts +6 -4
  15. package/assembly/__tests__/test.spec.ts +120 -191
  16. package/assembly/deserialize/simple/bool.ts +1 -1
  17. package/assembly/deserialize/simple/map.ts +1 -1
  18. package/assembly/deserialize/simple/string.ts +4 -3
  19. package/assembly/globals/tables.ts +74 -416
  20. package/assembly/index.ts +18 -18
  21. package/assembly/serialize/simd/string.ts +10 -10
  22. package/assembly/serialize/simple/array.ts +4 -4
  23. package/assembly/serialize/simple/bool.ts +2 -2
  24. package/assembly/serialize/simple/date.ts +1 -1
  25. package/assembly/serialize/simple/float.ts +1 -1
  26. package/assembly/serialize/simple/integer.ts +1 -1
  27. package/assembly/serialize/simple/map.ts +6 -6
  28. package/assembly/serialize/simple/string.ts +3 -3
  29. package/assembly/test.ts +4 -43
  30. package/assembly/util/bytes.ts +1 -1
  31. package/modules/as-bs/assembly/index.ts +33 -83
  32. package/modules/test/assembly/index.ts +22 -0
  33. package/package.json +3 -7
  34. package/run-tests.sh +15 -0
  35. package/transform/lib/index.js +1 -1
  36. package/transform/lib/index.js.map +1 -1
  37. package/transform/src/index.ts +1 -1
@@ -1,193 +1,122 @@
1
- import { JSON } from "json-as";
2
- import { describe, expect, run } from "as-test/assembly";
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 class inheritance", () => {
6
- const obj = new DerivedObject("1", "2");
7
-
8
- expect(JSON.stringify(obj)).toBe('{"a":"1","b":"2"}');
9
- });
10
-
11
- describe("Should serialize nulls", () => {
12
- expect(JSON.stringify<Null>(null)).toBe("null");
13
- });
14
-
15
- describe("Should serialize integer arrays", () => {
16
- expect(JSON.stringify<u32[]>([0, 100, 101])).toBe("[0,100,101]");
17
-
18
- expect(JSON.stringify<u64[]>([0, 100, 101])).toBe("[0,100,101]");
19
-
20
- expect(JSON.stringify<i32[]>([0, 100, 101, -100, -101])).toBe("[0,100,101,-100,-101]");
21
-
22
- expect(JSON.stringify<i64[]>([0, 100, 101, -100, -101])).toBe("[0,100,101,-100,-101]");
23
- });
24
-
25
- describe("Should serialize float arrays", () => {
26
- expect(JSON.stringify<f64[]>([7.23, 10e2, 10e2, 123456e-5, 123456e-5, 0.0, 7.23])).toBe("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]");
27
-
28
- expect(JSON.stringify<f64[]>([1e21, 1e22, 1e-7, 1e-8, 1e-9])).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
29
- });
30
-
31
- describe("Should serialize boolean arrays", () => {
32
- expect(JSON.stringify<bool[]>([true, false])).toBe("[true,false]");
33
-
34
- expect(JSON.stringify<boolean[]>([true, false])).toBe("[true,false]");
35
- });
36
-
37
- describe("Should serialize string arrays", () => {
38
- expect(JSON.stringify<string[]>(['string "with random spa\nces and \nnewlines\n\n\n'])).toBe('["string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"]');
39
- });
40
-
41
- describe("Should serialize nested integer arrays", () => {
42
- expect(JSON.stringify<i64[][]>([[100, 101], [-100, -101], [0]])).toBe("[[100,101],[-100,-101],[0]]");
43
- });
44
-
45
- describe("Should serialize nested float arrays", () => {
46
- expect(JSON.stringify<f64[][]>([[7.23], [10e2], [10e2], [123456e-5], [123456e-5], [0.0], [7.23]])).toBe("[[7.23],[1000.0],[1000.0],[1.23456],[1.23456],[0.0],[7.23]]");
47
- });
48
-
49
- describe("Should serialize nested boolean arrays", () => {
50
- expect(JSON.stringify<bool[][]>([[true], [false]])).toBe("[[true],[false]]");
51
-
52
- expect(JSON.stringify<boolean[][]>([[true], [false]])).toBe("[[true],[false]]");
53
- });
54
-
55
- describe("Should serialize object arrays", () => {
56
- expect(
57
- JSON.stringify<Vec3[]>([
58
- {
59
- x: 3.4,
60
- y: 1.2,
61
- z: 8.3,
62
- },
63
- {
64
- x: 3.4,
65
- y: -2.1,
66
- z: 9.3,
67
- },
68
- ]),
69
- ).toBe('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
70
- });
71
-
72
- describe("Should serialize objects", () => {
73
- expect(
74
- JSON.stringify<Vec3>({
75
- x: 3.4,
76
- y: 1.2,
77
- z: 8.3,
78
- }),
79
- ).toBe('{"x":3.4,"y":1.2,"z":8.3}');
80
-
81
- expect(
82
- JSON.stringify<Player>({
83
- firstName: "Emmet",
84
- lastName: "West",
85
- lastActive: [8, 27, 2022],
86
- age: 23,
87
- pos: {
88
- x: 3.4,
89
- y: 1.2,
90
- z: 8.3,
91
- },
92
- isVerified: true,
93
- }),
94
- ).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
95
-
96
- expect(JSON.stringify<ObjectWithFloat>({ f: 7.23 })).toBe('{"f":7.23}');
97
-
98
- expect(JSON.stringify<ObjectWithFloat>({ f: 0.000001 })).toBe('{"f":0.000001}');
99
-
100
- expect(JSON.stringify<ObjectWithFloat>({ f: 1e-7 })).toBe('{"f":1e-7}');
101
-
102
- expect(JSON.stringify<ObjectWithFloat>({ f: 1e20 })).toBe('{"f":100000000000000000000.0}');
103
-
104
- expect(JSON.stringify<ObjectWithFloat>({ f: 1e21 })).toBe('{"f":1e+21}');
105
-
106
- expect(JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
107
- });
108
-
109
- describe("Should serialize @omit'ed objects", () => {
110
- expect(
111
- JSON.stringify(<OmitIf>{
112
- y: 1,
113
- }),
114
- ).toBe('{"x":1,"y":1,"z":1}');
115
- });
116
- describe("Should deserialize class inheritance", () => {
117
- const jsonStr = '{"a":"1","b":"2"}';
118
- const obj = JSON.parse<DerivedObject>(jsonStr);
119
-
120
- expect(obj instanceof DerivedObject).toBe(true);
121
- expect(obj.a).toBe("1");
122
- expect(obj.b).toBe("2");
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; //ERROR(`Expected to find boolean, but found "${data.slice(0, 100)}" instead!`);
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>>()) ERROR("Map key must also be a valid JSON key!");
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 = load<u16>(DESERIALIZE_ESCAPE_TABLE + load<u8>(srcStart, 2));
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>(dst, escaped);
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 != srcStart) dst = __renew(dst, dstPtr - dst);
47
+ if (lastPtr != startPtr) dst = __renew(dst, dstPtr - dst);
47
48
  return changetype<string>(dst);
48
49
  }