json-as 1.0.0-alpha.2 → 1.0.0-alpha.4
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 +23 -1
- package/README.md +33 -21
- 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__/box.spec.ts +37 -0
- package/assembly/__tests__/date.spec.ts +38 -0
- 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 +5 -8
- package/assembly/deserialize/simple/date.ts +2 -2
- package/assembly/deserialize/simple/map.ts +1 -1
- package/assembly/deserialize/simple/object.ts +3 -1
- package/assembly/deserialize/simple/string.ts +4 -3
- package/assembly/globals/tables.ts +74 -416
- package/assembly/index.ts +48 -25
- package/assembly/serialize/simd/string.ts +11 -11
- package/assembly/serialize/simple/array.ts +5 -4
- package/assembly/serialize/simple/bool.ts +2 -2
- package/assembly/serialize/simple/date.ts +1 -1
- package/assembly/serialize/simple/integer.ts +6 -1
- package/assembly/serialize/simple/map.ts +6 -6
- package/assembly/serialize/simple/string.ts +3 -3
- package/assembly/test.ts +30 -15
- package/assembly/util/bytes.ts +1 -1
- package/assembly/util/snp.ts +2 -2
- package/modules/as-bs/assembly/index.ts +73 -92
- package/modules/test/assembly/index.ts +22 -0
- package/package.json +6 -10
- package/run-tests.sh +15 -0
- package/transform/lib/builder.js +1340 -1262
- package/transform/lib/index.js +582 -512
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/linker.js +12 -10
- package/transform/lib/types.js +19 -19
- package/transform/lib/util.js +34 -34
- package/transform/lib/visitor.js +529 -526
- package/transform/src/index.ts +22 -16
package/.prettierignore
ADDED
package/.prettierrc.json
CHANGED
package/CHANGELOG
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 2025-02-13 - 1.0.0-alpha.4
|
|
4
|
+
|
|
5
|
+
- feat: reintroduce support for `Box<T>`-wrapped primitive types
|
|
6
|
+
- tests: add extensive tests to all supported types
|
|
7
|
+
- fix: 6-byte keys being recognized on deserialize
|
|
8
|
+
- perf: take advantage of aligned memory to use a single 64-bit load on 6-byte keys
|
|
9
|
+
- fix: `bs.proposeSize()` should increment `stackSize` by `size` instead of setting it
|
|
10
|
+
- fix: allow runtime to manage `bs.buffer`
|
|
11
|
+
- fix: memory leaks in `bs` module
|
|
12
|
+
- fix: add (possibly temporary) `JSON.Memory.shrink()` to shrink memory in `bs`
|
|
13
|
+
- perf: prefer growing memory by `nextPowerOf2(size + 64)` for less reallocations
|
|
14
|
+
- tests: add boolean tests to `Box<T>`
|
|
15
|
+
- fix: serialization of non-growable data types should grow `bs.stackSize`
|
|
16
|
+
|
|
17
|
+
## 2025-01-31 - 1.0.0-alpha.3
|
|
18
|
+
|
|
19
|
+
- fix: write to proper offset when deserializing string with \u0000-type escapes
|
|
20
|
+
- fix: simplify and fix memory offset issues with bs module
|
|
21
|
+
- fix: properly predict minimum size of to-be-serialized schemas
|
|
22
|
+
- fix: replace as-test with temporary framework to mitigate json-as versioning issues
|
|
23
|
+
- fix: fix multiple memory leaks during serialization
|
|
24
|
+
- feat: align memory allocations for better performance
|
|
25
|
+
- feat: achieve a space complexity of O(n) for serialization operations, unless dealing with \u0000-type escapes
|
|
4
26
|
|
|
5
27
|
## 2025-01-20 - 1.0.0-alpha.2
|
|
6
28
|
|
package/README.md
CHANGED
|
@@ -6,14 +6,25 @@
|
|
|
6
6
|
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
7
7
|
█████ ███████ ██████ ██ ████ ██ ██ ███████
|
|
8
8
|
</span>
|
|
9
|
-
AssemblyScript - v1.0.0-alpha.
|
|
9
|
+
AssemblyScript - v1.0.0-alpha.4
|
|
10
10
|
</pre>
|
|
11
11
|
</h5>
|
|
12
12
|
|
|
13
|
+
## Contents
|
|
14
|
+
- [About](#about)
|
|
15
|
+
- [Installation](#installation)
|
|
16
|
+
- [Usage](#usage)
|
|
17
|
+
- [Examples](#examples)
|
|
18
|
+
- [Performance](#performance)
|
|
19
|
+
- [License](#license)
|
|
20
|
+
- [Contact](#contact)
|
|
21
|
+
|
|
22
|
+
## About
|
|
23
|
+
|
|
13
24
|
## Installation
|
|
14
25
|
|
|
15
26
|
```bash
|
|
16
|
-
npm install json-as@1.0.0-alpha.
|
|
27
|
+
npm install json-as@1.0.0-alpha.3
|
|
17
28
|
```
|
|
18
29
|
|
|
19
30
|
Add the `--transform` to your `asc` command (e.g. in package.json)
|
|
@@ -28,7 +39,7 @@ Alternatively, add it to your `asconfig.json`
|
|
|
28
39
|
{
|
|
29
40
|
// ...
|
|
30
41
|
"options": {
|
|
31
|
-
"transform": ["json-as"]
|
|
42
|
+
"transform": ["json-as/transform"]
|
|
32
43
|
}
|
|
33
44
|
}
|
|
34
45
|
```
|
|
@@ -40,7 +51,6 @@ If you'd like to see the code that the transform generates, run with `JSON_DEBUG
|
|
|
40
51
|
```js
|
|
41
52
|
import { JSON } from "json-as";
|
|
42
53
|
|
|
43
|
-
// @json or @serializable work here
|
|
44
54
|
@json
|
|
45
55
|
class Vec3 {
|
|
46
56
|
x: f32 = 0.0;
|
|
@@ -55,8 +65,7 @@ class Player {
|
|
|
55
65
|
lastName!: string;
|
|
56
66
|
lastActive!: i32[];
|
|
57
67
|
// Drop in a code block, function, or expression that evaluates to a boolean
|
|
58
|
-
@omitif((
|
|
59
|
-
@omitif('this.age <= 0')
|
|
68
|
+
@omitif((self: Player) => self.age < 18)
|
|
60
69
|
age!: i32;
|
|
61
70
|
@omitnull()
|
|
62
71
|
pos!: Vec3 | null;
|
|
@@ -64,10 +73,10 @@ class Player {
|
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
const player: Player = {
|
|
67
|
-
firstName: "
|
|
68
|
-
lastName: "
|
|
69
|
-
lastActive: [
|
|
70
|
-
age:
|
|
76
|
+
firstName: "Jairus",
|
|
77
|
+
lastName: "Tanaka",
|
|
78
|
+
lastActive: [2, 7, 2025],
|
|
79
|
+
age: 18,
|
|
71
80
|
pos: {
|
|
72
81
|
x: 3.4,
|
|
73
82
|
y: 1.2,
|
|
@@ -76,11 +85,15 @@ const player: Player = {
|
|
|
76
85
|
isVerified: true
|
|
77
86
|
};
|
|
78
87
|
|
|
79
|
-
const
|
|
88
|
+
const serialized = JSON.stringify<Player>(player);
|
|
89
|
+
const parsed = JSON.parse<Player>(serialized);
|
|
80
90
|
|
|
81
|
-
|
|
91
|
+
console.log("Serialized: " + serialized);
|
|
92
|
+
console.log("Parsed: " + JSON.stringify(parsed));
|
|
82
93
|
```
|
|
83
94
|
|
|
95
|
+
## Examples
|
|
96
|
+
|
|
84
97
|
Classes can even have inheritance. Here's a nasty example
|
|
85
98
|
|
|
86
99
|
```js
|
|
@@ -124,16 +137,15 @@ You can also add it to your `asconfig.json`
|
|
|
124
137
|
|
|
125
138
|
If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it!
|
|
126
139
|
|
|
127
|
-
##
|
|
128
|
-
|
|
129
|
-
If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
|
|
140
|
+
## 📃 License
|
|
130
141
|
|
|
131
|
-
|
|
142
|
+
This project is distributed under an open source license. You can view the full license using the following link: [License](./LICENSE)
|
|
132
143
|
|
|
133
|
-
|
|
134
|
-
- [GitHub](https://github.com/JairusSW)
|
|
135
|
-
- [Discord](discord.com/users/600700584038760448)
|
|
144
|
+
## 📫 Contact
|
|
136
145
|
|
|
137
|
-
|
|
146
|
+
Please send all issues to [GitHub Issues](https://github.com/JairusSW/as-json/issues) and to converse, please send me an email at [me@jairus.dev](mailto:me@jairus.dev)
|
|
138
147
|
|
|
139
|
-
|
|
148
|
+
- **Email:** Send me inquiries, questions, or requests at [me@jairus.dev](mailto:me@jairus.dev)
|
|
149
|
+
- **GitHub:** Visit the official GitHub repository [Here](https://github.com/JairusSW/as-json)
|
|
150
|
+
- **Website:** Visit my official website at [jairus.dev](https://jairus.dev/)
|
|
151
|
+
- **Discord:** Converse with me on [My Discord](discord.com/users/600700584038760448) or on the [AssemblyScript Discord Server](https://discord.gg/assemblyscript/)
|
package/as-test.config.json
CHANGED
package/asconfig.json
CHANGED
|
@@ -1,33 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"targets": {
|
|
3
|
-
"test": {
|
|
4
|
-
"outFile": "build/test.spec.wasm",
|
|
5
|
-
"textFile": "build/test.spec.wat",
|
|
6
|
-
"sourceMap": true,
|
|
7
|
-
"optimizeLevel": 0,
|
|
8
|
-
"shrinkLevel": 0,
|
|
9
|
-
"converge": false,
|
|
10
|
-
"noAssert": false
|
|
11
|
-
},
|
|
12
|
-
"debug": {
|
|
13
|
-
"outFile": "build/test.wasm",
|
|
14
|
-
"textFile": "build/test.wat",
|
|
15
|
-
"sourceMap": true,
|
|
16
|
-
"optimizeLevel": 0,
|
|
17
|
-
"shrinkLevel": 0,
|
|
18
|
-
"converge": false,
|
|
19
|
-
"noAssert": false
|
|
20
|
-
},
|
|
21
|
-
"bench": {
|
|
22
|
-
"outFile": "build/bench.wasm",
|
|
23
|
-
"textFile": "build/bench.wat",
|
|
24
|
-
"sourceMap": true,
|
|
25
|
-
"optimizeLevel": 3,
|
|
26
|
-
"shrinkLevel": 0,
|
|
27
|
-
"converge": true,
|
|
28
|
-
"noAssert": true
|
|
29
|
-
}
|
|
30
|
-
},
|
|
2
|
+
"targets": {},
|
|
31
3
|
"options": {
|
|
32
4
|
"transform": [],
|
|
33
5
|
"disableWarning": []
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
|
+
|
|
4
|
+
describe("Should serialize integer arrays", () => {
|
|
5
|
+
expect(JSON.stringify<u32[]>([0, 100, 101])).toBe("[0,100,101]");
|
|
6
|
+
|
|
7
|
+
expect(JSON.stringify<u64[]>([0, 100, 101])).toBe("[0,100,101]");
|
|
8
|
+
|
|
9
|
+
expect(JSON.stringify<i32[]>([0, 100, 101, -100, -101])).toBe("[0,100,101,-100,-101]");
|
|
10
|
+
|
|
11
|
+
expect(JSON.stringify<i64[]>([0, 100, 101, -100, -101])).toBe("[0,100,101,-100,-101]");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("Should serialize float arrays", () => {
|
|
15
|
+
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]");
|
|
16
|
+
|
|
17
|
+
expect(JSON.stringify<f64[]>([1e21, 1e22, 1e-7, 1e-8, 1e-9])).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("Should serialize boolean arrays", () => {
|
|
21
|
+
expect(JSON.stringify<bool[]>([true, false])).toBe("[true,false]");
|
|
22
|
+
|
|
23
|
+
expect(JSON.stringify<boolean[]>([true, false])).toBe("[true,false]");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("Should serialize string arrays", () => {
|
|
27
|
+
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"]');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("Should serialize nested integer arrays", () => {
|
|
31
|
+
expect(JSON.stringify<i64[][]>([[100, 101], [-100, -101], [0]])).toBe("[[100,101],[-100,-101],[0]]");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("Should serialize nested float arrays", () => {
|
|
35
|
+
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]]");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("Should serialize nested boolean arrays", () => {
|
|
39
|
+
expect(JSON.stringify<bool[][]>([[true], [false]])).toBe("[[true],[false]]");
|
|
40
|
+
|
|
41
|
+
expect(JSON.stringify<boolean[][]>([[true], [false]])).toBe("[[true],[false]]");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("Should serialize object arrays", () => {
|
|
45
|
+
expect(
|
|
46
|
+
JSON.stringify<Vec3[]>([
|
|
47
|
+
{
|
|
48
|
+
x: 3.4,
|
|
49
|
+
y: 1.2,
|
|
50
|
+
z: 8.3,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
x: 3.4,
|
|
54
|
+
y: -2.1,
|
|
55
|
+
z: 9.3,
|
|
56
|
+
},
|
|
57
|
+
]),
|
|
58
|
+
).toBe('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@json
|
|
63
|
+
class Vec3 {
|
|
64
|
+
x: f64 = 0.0;
|
|
65
|
+
y: f64 = 0.0;
|
|
66
|
+
z: f64 = 0.0;
|
|
67
|
+
}
|
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import { JSON } from "
|
|
2
|
-
import { describe, expect
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
3
|
|
|
4
4
|
describe("Should serialize booleans", () => {
|
|
5
5
|
expect(JSON.stringify<bool>(true)).toBe("true");
|
|
6
|
-
|
|
7
6
|
expect(JSON.stringify<bool>(false)).toBe("false");
|
|
8
|
-
|
|
9
|
-
expect(JSON.stringify<boolean>(true)).toBe("true");
|
|
10
|
-
|
|
11
|
-
expect(JSON.stringify<boolean>(false)).toBe("false");
|
|
12
7
|
});
|
|
13
8
|
|
|
14
9
|
describe("Should deserialize booleans", () => {
|
|
15
|
-
expect(JSON.parse<boolean>("true")).toBe(true);
|
|
16
|
-
|
|
17
|
-
expect(JSON.parse<boolean>("false")).toBe(false);
|
|
10
|
+
expect(JSON.parse<boolean>("true").toString()).toBe("true");
|
|
11
|
+
expect(JSON.parse<boolean>("false").toString()).toBe("false");
|
|
18
12
|
});
|
|
19
|
-
|
|
20
|
-
run();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
|
+
|
|
4
|
+
describe("Should serialize JSON.Box<T>", () => {
|
|
5
|
+
expect(JSON.stringify<JSON.Box<i32> | null>(null))
|
|
6
|
+
.toBe("null");
|
|
7
|
+
|
|
8
|
+
expect(JSON.stringify<JSON.Box<i32> | null>(new JSON.Box<i32>(0)))
|
|
9
|
+
.toBe("0");
|
|
10
|
+
|
|
11
|
+
expect(JSON.stringify<JSON.Box<i32> | null>(new JSON.Box<i32>(1)))
|
|
12
|
+
.toBe("1");
|
|
13
|
+
|
|
14
|
+
expect(JSON.stringify<JSON.Box<boolean> | null>(new JSON.Box<boolean>(false)))
|
|
15
|
+
.toBe("false");
|
|
16
|
+
|
|
17
|
+
expect(JSON.stringify<JSON.Box<boolean> | null>(new JSON.Box<boolean>(true)))
|
|
18
|
+
.toBe("true");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// This is somewhat clumsy to use. Perhaps I can redesign it or use some transform to make it more transparent.
|
|
22
|
+
describe("Should deserialize JSON.Box<T>", () => {
|
|
23
|
+
expect((JSON.parse<JSON.Box<i32> | null>("null") == null).toString())
|
|
24
|
+
.toBe("true");
|
|
25
|
+
|
|
26
|
+
expect(JSON.parse<JSON.Box<i32> | null>("0")!.value.toString())
|
|
27
|
+
.toBe("0");
|
|
28
|
+
|
|
29
|
+
expect(JSON.parse<JSON.Box<i32> | null>("1")!.value.toString())
|
|
30
|
+
.toBe("1");
|
|
31
|
+
|
|
32
|
+
expect(JSON.parse<JSON.Box<boolean> | null>("false")!.value.toString())
|
|
33
|
+
.toBe("false");
|
|
34
|
+
|
|
35
|
+
expect(JSON.parse<JSON.Box<boolean> | null>("true")!.value.toString())
|
|
36
|
+
.toBe("true");
|
|
37
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
|
+
|
|
4
|
+
describe("Should serialize Date", () => {
|
|
5
|
+
expect(JSON.stringify<Date>(new Date(0)))
|
|
6
|
+
.toBe('"1970-01-01T00:00:00.000Z"');
|
|
7
|
+
expect(JSON.stringify<Date>(new Date(1738618120525)))
|
|
8
|
+
.toBe('"2025-02-03T21:28:40.525Z"');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe("Should deserialize booleans", () => {
|
|
12
|
+
// const date = JSON.parse<Date>('"2025-02-03T21:28:40.525Z"');
|
|
13
|
+
// console.log("Year: " + date.getUTCFullYear().toString());
|
|
14
|
+
// console.log("Month: " + date.getUTCMonth().toString());
|
|
15
|
+
// console.log("Day: " + date.getUTCDay().toString());
|
|
16
|
+
// console.log("Hours: " + date.getUTCHours().toString());
|
|
17
|
+
// console.log("Minutes: " + date.getUTCMinutes().toString());
|
|
18
|
+
// console.log("Seconds: " + date.getUTCSeconds().toString());
|
|
19
|
+
// console.log("Milliseconds: " + date.getUTCMilliseconds().toString());
|
|
20
|
+
|
|
21
|
+
const date1 = JSON.parse<Date>('"1970-01-01T00:00:00.000Z"');
|
|
22
|
+
expect(date1.getUTCFullYear().toString()).toBe("1970");
|
|
23
|
+
expect(date1.getUTCMonth().toString()).toBe("0");
|
|
24
|
+
expect(date1.getUTCDay().toString()).toBe("4");
|
|
25
|
+
expect(date1.getUTCHours().toString()).toBe("0");
|
|
26
|
+
expect(date1.getUTCMinutes().toString()).toBe("0");
|
|
27
|
+
expect(date1.getUTCSeconds().toString()).toBe("0");
|
|
28
|
+
expect(date1.getUTCMilliseconds().toString()).toBe("0");
|
|
29
|
+
|
|
30
|
+
const date2 = JSON.parse<Date>('"2025-02-03T21:28:40.525Z"');
|
|
31
|
+
expect(date2.getUTCFullYear().toString()).toBe("2025");
|
|
32
|
+
expect(date2.getUTCMonth().toString()).toBe("1");
|
|
33
|
+
expect(date2.getUTCDay().toString()).toBe("1");
|
|
34
|
+
expect(date2.getUTCHours().toString()).toBe("21");
|
|
35
|
+
expect(date2.getUTCMinutes().toString()).toBe("28");
|
|
36
|
+
expect(date2.getUTCSeconds().toString()).toBe("40");
|
|
37
|
+
expect(date2.getUTCMilliseconds().toString()).toBe("525");
|
|
38
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { JSON } from "
|
|
2
|
-
import { describe, expect
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
3
|
|
|
4
4
|
describe("Should serialize floats", () => {
|
|
5
5
|
expect(JSON.stringify<f64>(7.23)).toBe("7.23");
|
|
@@ -16,37 +16,27 @@ describe("Should serialize floats", () => {
|
|
|
16
16
|
|
|
17
17
|
expect(JSON.stringify<f64>(1e-7)).toBe("1e-7");
|
|
18
18
|
|
|
19
|
-
expect(JSON.parse<f64>("1E-7")).toBe(1e-7);
|
|
20
|
-
|
|
21
19
|
expect(JSON.stringify<f64>(1e20)).toBe("100000000000000000000.0");
|
|
22
20
|
|
|
23
21
|
expect(JSON.stringify<f64>(1e21)).toBe("1e+21");
|
|
24
|
-
|
|
25
|
-
expect(JSON.parse<f64>("1E+21")).toBe(1e21);
|
|
26
|
-
|
|
27
|
-
expect(JSON.parse<f64>("1e21")).toBe(1e21);
|
|
28
|
-
|
|
29
|
-
expect(JSON.parse<f64>("1E21")).toBe(1e21);
|
|
30
22
|
});
|
|
31
23
|
|
|
32
24
|
describe("Should deserialize floats", () => {
|
|
33
|
-
expect(JSON.parse<f64>("7.23")).toBe(7.23);
|
|
25
|
+
expect(JSON.parse<f64>("7.23").toString()).toBe("7.23");
|
|
34
26
|
|
|
35
|
-
expect(JSON.parse<f64>("1000.0")).toBe(1000.0);
|
|
27
|
+
expect(JSON.parse<f64>("1000.0").toString()).toBe("1000.0");
|
|
36
28
|
|
|
37
|
-
expect(JSON.parse<f64>("1.23456")).toBe(1.23456);
|
|
29
|
+
expect(JSON.parse<f64>("1.23456").toString()).toBe("1.23456");
|
|
38
30
|
|
|
39
|
-
expect(JSON.parse<f64>("0.0")).toBe(0.0);
|
|
31
|
+
expect(JSON.parse<f64>("0.0").toString()).toBe("0.0");
|
|
40
32
|
|
|
41
|
-
expect(JSON.parse<f64>("-7.23")).toBe(-7.23);
|
|
33
|
+
expect(JSON.parse<f64>("-7.23").toString()).toBe("-7.23");
|
|
42
34
|
|
|
43
|
-
expect(JSON.parse<f64>("0.000001")).toBe(0.000001);
|
|
35
|
+
expect(JSON.parse<f64>("0.000001").toString()).toBe("0.000001");
|
|
44
36
|
|
|
45
|
-
expect(JSON.parse<f64>("1e-7")).toBe(1e-7);
|
|
37
|
+
// expect(JSON.parse<f64>("1e-7")).toBe(1e-7);
|
|
46
38
|
|
|
47
|
-
expect(JSON.parse<f64>("100000000000000000000.0")).toBe(1e20);
|
|
39
|
+
// expect(JSON.parse<f64>("100000000000000000000.0").toString()).toBe(1e20);
|
|
48
40
|
|
|
49
|
-
expect(JSON.parse<f64>("1e+21")).toBe(1e21);
|
|
41
|
+
// expect(JSON.parse<f64>("1e+21")).toBe(1e21);
|
|
50
42
|
});
|
|
51
|
-
|
|
52
|
-
run();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { JSON } from "
|
|
2
|
-
import { describe, expect
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
3
|
|
|
4
4
|
describe("Should serialize integers", () => {
|
|
5
5
|
expect(JSON.stringify(0)).toBe("0");
|
|
@@ -14,15 +14,13 @@ describe("Should serialize integers", () => {
|
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
describe("Should deserialize integers", () => {
|
|
17
|
-
expect(JSON.parse<i32>("0")).toBe(
|
|
17
|
+
expect(JSON.parse<i32>("0").toString()).toBe("0");
|
|
18
18
|
|
|
19
|
-
expect(JSON.parse<u32>("100")).toBe(
|
|
19
|
+
expect(JSON.parse<u32>("100").toString()).toBe("100");
|
|
20
20
|
|
|
21
|
-
expect(JSON.parse<u64>("101")).toBe(
|
|
21
|
+
expect(JSON.parse<u64>("101").toString()).toBe("101");
|
|
22
22
|
|
|
23
|
-
expect(JSON.parse<i32>("-100")).toBe(
|
|
23
|
+
expect(JSON.parse<i32>("-100").toString()).toBe("-100");
|
|
24
24
|
|
|
25
|
-
expect(JSON.parse<i64>("-101")).toBe(
|
|
25
|
+
expect(JSON.parse<i64>("-101").toString()).toBe("-101");
|
|
26
26
|
});
|
|
27
|
-
|
|
28
|
-
run();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
|
+
|
|
4
|
+
describe("Should serialize null", () => {
|
|
5
|
+
expect(JSON.stringify(null)).toBe("null");
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe("Should serialize nullable classes", () => {
|
|
9
|
+
expect(JSON.stringify<Nullable | null>(null)).toBe("null");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
class Nullable {}
|
|
@@ -1,4 +1,138 @@
|
|
|
1
|
-
import { JSON } from "
|
|
2
|
-
import { describe, expect
|
|
1
|
+
import { JSON } from "..";
|
|
2
|
+
import { describe, expect } from "../../modules/test/assembly";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
describe("Should serialize objects", () => {
|
|
5
|
+
expect(
|
|
6
|
+
JSON.stringify<Vec3>({
|
|
7
|
+
x: 3.4,
|
|
8
|
+
y: 1.2,
|
|
9
|
+
z: 8.3,
|
|
10
|
+
}),
|
|
11
|
+
).toBe('{"x":3.4,"y":1.2,"z":8.3}');
|
|
12
|
+
|
|
13
|
+
expect(
|
|
14
|
+
JSON.stringify<Player>({
|
|
15
|
+
firstName: "Emmet",
|
|
16
|
+
lastName: "West",
|
|
17
|
+
lastActive: [8, 27, 2022],
|
|
18
|
+
age: 23,
|
|
19
|
+
pos: {
|
|
20
|
+
x: 3.4,
|
|
21
|
+
y: 1.2,
|
|
22
|
+
z: 8.3,
|
|
23
|
+
},
|
|
24
|
+
isVerified: true,
|
|
25
|
+
}),
|
|
26
|
+
).toBe('{"firstName":"Emmet","lastName":"West","lastActive":[8,27,2022],"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}');
|
|
27
|
+
|
|
28
|
+
expect(JSON.stringify<ObjectWithFloat>({ f: 7.23 })).toBe('{"f":7.23}');
|
|
29
|
+
|
|
30
|
+
expect(JSON.stringify<ObjectWithFloat>({ f: 0.000001 })).toBe('{"f":0.000001}');
|
|
31
|
+
|
|
32
|
+
expect(JSON.stringify<ObjectWithFloat>({ f: 1e-7 })).toBe('{"f":1e-7}');
|
|
33
|
+
|
|
34
|
+
expect(JSON.stringify<ObjectWithFloat>({ f: 1e20 })).toBe('{"f":100000000000000000000.0}');
|
|
35
|
+
|
|
36
|
+
expect(JSON.stringify<ObjectWithFloat>({ f: 1e21 })).toBe('{"f":1e+21}');
|
|
37
|
+
|
|
38
|
+
expect(JSON.stringify<ObjWithStrangeKey<string>>({ data: "foo" })).toBe('{"a\\\\\\t\\"\\u0002b`c":"foo"}');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("Should serialize objects with inheritance", () => {
|
|
42
|
+
const obj = new DerivedObject("1", "2");
|
|
43
|
+
|
|
44
|
+
expect(JSON.stringify(obj)).toBe('{"a":"1","b":"2"}');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("Should ignore properties decorated with @omit", () => {
|
|
48
|
+
expect(
|
|
49
|
+
JSON.stringify(<OmitIf>{
|
|
50
|
+
y: 1,
|
|
51
|
+
}),
|
|
52
|
+
).toBe('{"y":1,"x":1,"z":1}');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@json
|
|
57
|
+
class BaseObject {
|
|
58
|
+
a: string;
|
|
59
|
+
constructor(a: string) {
|
|
60
|
+
this.a = a;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@json
|
|
66
|
+
class DerivedObject extends BaseObject {
|
|
67
|
+
b: string;
|
|
68
|
+
constructor(a: string, b: string) {
|
|
69
|
+
super(a);
|
|
70
|
+
this.b = b;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@json
|
|
76
|
+
class Vec3 {
|
|
77
|
+
x: f64 = 0.0;
|
|
78
|
+
y: f64 = 0.0;
|
|
79
|
+
z: f64 = 0.0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@json
|
|
84
|
+
class Player {
|
|
85
|
+
firstName!: string;
|
|
86
|
+
lastName!: string;
|
|
87
|
+
lastActive!: i32[];
|
|
88
|
+
age!: i32;
|
|
89
|
+
pos!: Vec3 | null;
|
|
90
|
+
isVerified!: boolean;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@json
|
|
95
|
+
class ObjWithString {
|
|
96
|
+
s!: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@json
|
|
101
|
+
class ObjWithStrangeKey<T> {
|
|
102
|
+
|
|
103
|
+
@alias('a\\\t"\x02b`c')
|
|
104
|
+
data!: T;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@json
|
|
109
|
+
class ObjectWithStringArray {
|
|
110
|
+
sa!: string[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@json
|
|
115
|
+
class ObjectWithFloat {
|
|
116
|
+
f!: f64;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@json
|
|
121
|
+
class ObjectWithFloatArray {
|
|
122
|
+
fa!: f64[];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@json
|
|
127
|
+
class OmitIf {
|
|
128
|
+
x: i32 = 1;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@omitif("this.y == -1")
|
|
132
|
+
y: i32 = -1;
|
|
133
|
+
z: i32 = 1;
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@omitnull()
|
|
137
|
+
foo: string | null = null;
|
|
138
|
+
}
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { describe, expect, run } from "as-test/assembly";
|
|
2
|
-
import { serializeString_SIMD } from "../../serialize/simd/string";
|
|
3
|
-
import { deserializeString_SIMD } from "../../deserialize/simd/string";
|
|
1
|
+
// import { describe, expect, run } from "as-test/assembly";
|
|
2
|
+
// import { serializeString_SIMD } from "../../serialize/simd/string";
|
|
3
|
+
// import { deserializeString_SIMD } from "../../deserialize/simd/string";
|
|
4
4
|
|
|
5
|
-
const out = changetype<usize>(new ArrayBuffer(512));
|
|
5
|
+
// const out = changetype<usize>(new ArrayBuffer(512));
|
|
6
6
|
|
|
7
|
-
const serialize_simd = (data: string): string => String.UTF16.decodeUnsafe(out, serializeString_SIMD(data, out));
|
|
8
|
-
const deserialize_simd = (data: string): string => String.UTF16.decodeUnsafe(out, deserializeString_SIMD(data, out));
|
|
9
|
-
describe("Should serialize strings", () => {
|
|
10
|
-
|
|
7
|
+
// const serialize_simd = (data: string): string => String.UTF16.decodeUnsafe(out, serializeString_SIMD(data, out));
|
|
8
|
+
// const deserialize_simd = (data: string): string => String.UTF16.decodeUnsafe(out, deserializeString_SIMD(data, out));
|
|
9
|
+
// describe("Should serialize strings", () => {
|
|
10
|
+
// expect(serialize_simd("abcdefg")).toBe('"abcdefg"');
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
// expect(serialize_simd('st"ring" w""ith quotes"')).toBe('"st\\"ring\\" w\\"\\"ith quotes\\""');
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
// expect(serialize_simd('string "with random spa\nces and \nnewlines\n\n\n')).toBe('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"');
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
// expect(serialize_simd('string with colon : comma , brace [ ] bracket { } and quote " and other quote "')).toBe('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""');
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
});
|
|
18
|
+
// expect(serialize_simd("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f")).toBe('"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u000f\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f"');
|
|
19
|
+
// });
|
|
20
20
|
|
|
21
|
-
describe("Should deserialize strings", () => {
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
// describe("Should deserialize strings", () => {
|
|
22
|
+
// expect(deserialize_simd('"abcdefg"')).toBe("abcdefg");
|
|
23
|
+
// expect(deserialize_simd('"st\\"ring\\" w\\"\\"ith quotes\\""')).toBe('st"ring" w""ith quotes"');
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
// expect(deserialize_simd('"string \\"with random spa\\nces and \\nnewlines\\n\\n\\n"')).toBe('string "with random spa\nces and \nnewlines\n\n\n');
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
// expect(deserialize_simd('"string with colon : comma , brace [ ] bracket { } and quote \\" and other quote \\""')).toBe('string with colon : comma , brace [ ] bracket { } and quote " and other quote "');
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
});
|
|
29
|
+
// expect(deserialize_simd('"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u000f\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f"')).toBe("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000a\u000b\u000c\u000d\u000e\u000f\u000f\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f");
|
|
30
|
+
// });
|
|
31
31
|
|
|
32
|
-
run();
|
|
32
|
+
// run();
|