json-as 1.2.1 → 1.2.2-beta.1

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 (79) hide show
  1. package/package.json +1 -1
  2. package/.github/FUNDING.yml +0 -1
  3. package/.github/dependabot.yml +0 -11
  4. package/.github/workflows/benchmark.yml +0 -72
  5. package/.github/workflows/release-package.yml +0 -47
  6. package/.github/workflows/tests.yml +0 -25
  7. package/.prettierignore +0 -9
  8. package/.prettierrc.json +0 -7
  9. package/.trunk/configs/.markdownlint.yaml +0 -2
  10. package/.trunk/configs/.shellcheckrc +0 -7
  11. package/.trunk/configs/.yamllint.yaml +0 -7
  12. package/.trunk/trunk.yaml +0 -37
  13. package/CHANGELOG.md +0 -334
  14. package/SECURITY.md +0 -32
  15. package/asconfig.json +0 -7
  16. package/assembly/__benches__/abc.bench.ts +0 -28
  17. package/assembly/__benches__/large.bench.ts +0 -238
  18. package/assembly/__benches__/lib/bench.ts +0 -85
  19. package/assembly/__benches__/medium.bench.ts +0 -128
  20. package/assembly/__benches__/small.bench.ts +0 -46
  21. package/assembly/__benches__/throughput.ts +0 -172
  22. package/assembly/__benches__/vec3.bench.ts +0 -37
  23. package/assembly/__tests__/arbitrary.spec.ts +0 -35
  24. package/assembly/__tests__/array.spec.ts +0 -145
  25. package/assembly/__tests__/bool.spec.ts +0 -12
  26. package/assembly/__tests__/box.spec.ts +0 -27
  27. package/assembly/__tests__/custom.spec.ts +0 -56
  28. package/assembly/__tests__/date.spec.ts +0 -36
  29. package/assembly/__tests__/enum.spec.ts +0 -35
  30. package/assembly/__tests__/float.spec.ts +0 -42
  31. package/assembly/__tests__/generics.spec.ts +0 -49
  32. package/assembly/__tests__/hierarchy.spec.ts +0 -61
  33. package/assembly/__tests__/integer.spec.ts +0 -26
  34. package/assembly/__tests__/lib/index.ts +0 -41
  35. package/assembly/__tests__/map.spec.ts +0 -7
  36. package/assembly/__tests__/namespace.spec.ts +0 -63
  37. package/assembly/__tests__/null.spec.ts +0 -12
  38. package/assembly/__tests__/raw.spec.ts +0 -23
  39. package/assembly/__tests__/resolving.spec.ts +0 -55
  40. package/assembly/__tests__/staticarray.spec.ts +0 -12
  41. package/assembly/__tests__/string.spec.ts +0 -30
  42. package/assembly/__tests__/struct.spec.ts +0 -163
  43. package/assembly/__tests__/test.spec.ts +0 -3
  44. package/assembly/__tests__/types.spec.ts +0 -27
  45. package/assembly/__tests__/types.ts +0 -98
  46. package/assembly/test.tmp.ts +0 -133
  47. package/bench/abc.bench.ts +0 -25
  48. package/bench/large.bench.ts +0 -127
  49. package/bench/lib/bench.d.ts +0 -27
  50. package/bench/lib/bench.js +0 -53
  51. package/bench/lib/chart.ts +0 -217
  52. package/bench/medium.bench.ts +0 -68
  53. package/bench/runners/assemblyscript.js +0 -34
  54. package/bench/small.bench.ts +0 -34
  55. package/bench/throughput.ts +0 -87
  56. package/bench/tsconfig.json +0 -13
  57. package/bench/vec3.bench.ts +0 -30
  58. package/bench.ts +0 -18
  59. package/ci/bench/lib/bench.ts +0 -42
  60. package/ci/bench/runners/assemblyscript.js +0 -29
  61. package/ci/run-bench.as.sh +0 -63
  62. package/publish.sh +0 -78
  63. package/run-bench.as.sh +0 -60
  64. package/run-bench.js.sh +0 -36
  65. package/run-tests.sh +0 -51
  66. package/scripts/build-chart01.ts +0 -38
  67. package/scripts/build-chart02.ts +0 -38
  68. package/scripts/build-chart03.ts +0 -139
  69. package/scripts/build-chart05.ts +0 -47
  70. package/scripts/generate-as-class.ts +0 -50
  71. package/scripts/lib/bench-utils.ts +0 -308
  72. package/transform/src/builder.ts +0 -1375
  73. package/transform/src/index.ts +0 -1486
  74. package/transform/src/linkers/alias.ts +0 -58
  75. package/transform/src/linkers/custom.ts +0 -32
  76. package/transform/src/linkers/imports.ts +0 -22
  77. package/transform/src/types.ts +0 -300
  78. package/transform/src/util.ts +0 -128
  79. package/transform/src/visitor.ts +0 -530
@@ -1,172 +0,0 @@
1
- import { JSON } from "..";
2
- import { bs } from "../../lib/as-bs";
3
- import { expect } from "../__tests__/lib";
4
- import { bytes } from "../util";
5
- import { bench, blackbox, dumpToFile } from "./lib/bench";
6
-
7
- // @json // 256b
8
- // class ObjSmall {
9
- // lorum: i32 = I32.MAX_VALUE;
10
- // ipsum: boolean = true;
11
- // dolor: Array<i32> = [1];
12
- // sit: string = "abcdefghijklmnopdasfqrstfuvwYZ1234567890`~!@#$%^&*()_+=-{}][\\|;\":'<>,./?";
13
- // }
14
-
15
- // @json // 512b
16
- // class ObjMedium {
17
- // lorum: u32 = U32.MAX_VALUE;
18
- // ipsum: boolean = true;
19
- // dolor: Array<i32> = [1,2,3,4,5];
20
- // sit: string = "abcdefghijklmnopdasfqrstfuvwYZ1234567890`~!@#$%^&*()_+=-{}][\\|;\":'<>,./?";
21
- // consectetur: i32 = 123456;
22
- // adipiscing: boolean = false;
23
- // elit: Array<i32> = [6,7,8,9,10];
24
- // sed: f64 = F64.MAX_VALUE;
25
- // eiusmod: string = "abcdYZ12345890./?";
26
- // }
27
-
28
- @json // 1 KB
29
- class ObjLarge {
30
- lorum: u32 = U32.MAX_VALUE;
31
- ipsum: boolean = true;
32
- dolor: Array<i32> = [1,2,3,4,5];
33
- sit: string = "abcdefghijklmnopdasfqrstfuvwYZ1234567890;~!@#$%^&*()_+=-{}][\\|;\":'<>,./?";
34
- consectetur: i32 = 123456;
35
- adipiscing: boolean = false;
36
- elit: Array<i32> = [6,7,8,9,10];
37
- sed: f64 = F64.MAX_VALUE;
38
- eiusmod: string = "abcdYZ12345890./?abcdYZ12345890./?abcdYZ12340./?";
39
- tempor: i32 = 999999;
40
- incididunt: boolean = true;
41
- ut: Array<i32> = [16,17,18,19,20];
42
- labore: f64 = 3.1415926535;
43
- et: string = "xyzXYZ09876!@#";
44
- dolore: i32 = -123456;
45
- magna: boolean = false;
46
- aliqua: Array<i32> = [21,22,23,24,25];
47
- argw: string = "abcdYZ12345890sdfw\"vie91kfESDFOK12i9i12dsf./?";
48
- }
49
-
50
- const obj = new ObjLarge();
51
-
52
- const arrSmall = new Array<i32>(25_600).fill(0);
53
- const arrMedium = new Array<i32>(128_000).fill(0);
54
- const arrLarge = new Array<i32>(256_000).fill(0);
55
-
56
- const strSmall = "a".repeat(100 * 1024);
57
- const strMedium = "a".repeat(500 * 1024);
58
- const strLarge = "a".repeat(1024 * 1024);
59
-
60
- const arrSmallStr = JSON.stringify(arrSmall);
61
- const arrMediumStr = JSON.stringify(arrMedium);
62
- const arrLargeStr = JSON.stringify(arrLarge);
63
-
64
- const strSmallStr = JSON.stringify(strSmall);
65
- const strMediumStr = JSON.stringify(strMedium);
66
- const strLargeStr = JSON.stringify(strLarge);
67
-
68
- const objStr = JSON.stringify(obj); // `{"lorum":4294967295,"ipsum":true,"dolor":[1,2,3,4,5],"sit":"abcdefghijklmnopdasfqrstfuvwYZ1234567890;~!@#$%^&*()_+=-{}][\\\\|;\\":'<>,./?","consectetur":123456,"adipiscing":false,"elit":[6,7,8,9,10],"sed":1.7976931348623157e+308,"eiusmod":"abcdYZ12345890./?abcdYZ12345890./?abcdYZ12340./?","tempor":999999,"incididunt":true,"ut":[16,17,18,19,20],"labore":3.1415926535,"et":"xyzXYZ09876!@#","dolore":-123456,"magna":false,"aliqua":[21,22,23,24,25],"argw":"abcdYZ12345890sdfw\\"vie91kfESDFOK12i9i12dsf./?"}`;
69
- const objStrEnd = changetype<usize>(objStr) + (objStr.length << 1);
70
-
71
- console.log(objStr)
72
- expect(JSON.stringify(obj)).toBe(objStr);
73
- expect(JSON.stringify(JSON.parse<ObjLarge>(objStr))).toBe(objStr);
74
- const ITER = 2_000_000;
75
-
76
- /* --- OBJECTS --- */
77
- bench("Serialize Small Object", () => { // 1kb
78
- // @ts-ignore
79
- obj.__SERIALIZE(changetype<usize>(obj));
80
- blackbox(bs.out<string>());
81
- }, 250_0000, objStr.length << 1);
82
- dumpToFile("small-obj", "serialize");
83
-
84
- // bench("Deserialize Small Object", () => {
85
- // blackbox(JSON.parse<typeof objSmall>(objSmallStr))
86
- // }, ITER, objSmallStr.length << 1);
87
- // dumpToFile("small-obj", "deserialize");
88
-
89
- // bench("Serialize Medium Object", () => { // 500kb
90
- // let ops = 500;
91
- // while(ops > 0) {
92
- // // @ts-ignore
93
- // objLarge.__SERIALIZE(changetype<usize>(objLarge));
94
- // ops--;
95
- // }
96
- // blackbox(bs.out<string>());
97
- // }, 5_000, (objLargeStr.length << 1) * 500);
98
- // dumpToFile("medium-obj", "serialize");
99
-
100
- // bench("Serialize Large Object", () => { // 500kb
101
- // let ops = 1000;
102
- // while(ops > 0) {
103
- // // @ts-ignore
104
- // objLarge.__SERIALIZE(changetype<usize>(objLarge));
105
- // ops--;
106
- // }
107
- // blackbox(bs.out<string>());
108
- // }, 500, (objLargeStr.length << 1) * 1000);
109
- // dumpToFile("large-obj", "serialize");
110
-
111
- // bench("Deserialize Small Object", () => {
112
- // // @ts-ignore
113
- // objLarge.__DESERIALIZE<ObjLarge>(changetype<usize>(objLargeStr), objLargeStrEnd, changetype<ObjLarge>(__new(sizeof<ObjLarge>(), idof<ObjLarge>())));
114
- // }, 250_000, objLargeStr.length << 1);
115
- // dumpToFile("small-obj", "deserialize");
116
-
117
- // bench("Deserialize Medium Object", () => {
118
- // let ops = 500;
119
- // while (ops > 0) {
120
- // // @ts-ignore
121
- // objLarge.__DESERIALIZE<ObjLarge>(changetype<usize>(objLargeStr), objLargeStrEnd, changetype<ObjLarge>(__new(sizeof<ObjLarge>(), idof<ObjLarge>())));
122
- // ops--;
123
- // }
124
- // }, 5_000, (objLargeStr.length << 1) * 500);
125
- // dumpToFile("medium-obj", "deserialize");
126
-
127
- // bench("Deserialize Large Object", () => {
128
- // let ops = 1000;
129
- // while (ops > 0) {
130
- // // @ts-ignore
131
- // objLarge.__DESERIALIZE<ObjLarge>(changetype<usize>(objLargeStr), objLargeStrEnd, changetype<ObjLarge>(__new(sizeof<ObjLarge>(), idof<ObjLarge>())));
132
- // ops--;
133
- // }
134
- // }, 500, (objLargeStr.length << 1)*1000);
135
- // dumpToFile("large-obj", "deserialize");
136
-
137
- // // bench("Deserialize Medium Object", () => blackbox(JSON.parse<typeof objMedium>(objMediumStr)), ITER, objMediumStr.length << 1);
138
- // // dumpToFile("medium-obj", "deserialize");
139
- // // bench("Deserialize Large Object", () => blackbox(JSON.parse<typeof objLarge>(objLargeStr)), ITER, objLargeStr.length << 1);
140
- // // dumpToFile("large-obj", "deserialize");
141
-
142
- // /* --- ARRAYS --- */
143
- // bench("Serialize Small Array", () => blackbox(JSON.stringify(arrSmall)), ITER, arrSmallStr.length << 1);
144
- // dumpToFile("small-arr", "serialize");
145
- // bench("Deserialize Small Array", () => blackbox(JSON.parse<i32[]>(arrSmallStr)), ITER, arrSmallStr.length << 1);
146
- // dumpToFile("small-arr", "deserialize");
147
-
148
- // bench("Serialize Medium Array", () => blackbox(JSON.stringify(arrMedium)), ITER, arrMediumStr.length << 1);
149
- // dumpToFile("medium-arr", "serialize");
150
- // bench("Deserialize Medium Array", () => blackbox(JSON.parse<i32[]>(arrMediumStr)), ITER, arrMediumStr.length << 1);
151
- // dumpToFile("medium-arr", "deserialize");
152
-
153
- // bench("Serialize Large Array", () => blackbox(JSON.stringify(arrLarge)), ITER, arrLargeStr.length << 1);
154
- // dumpToFile("large-arr", "serialize");
155
- // bench("Deserialize Large Array", () => blackbox(JSON.parse<i32[]>(arrLargeStr)), ITER, arrLargeStr.length << 1);
156
- // dumpToFile("large-arr", "deserialize");
157
-
158
- // /* --- STRINGS --- */
159
- // bench("Serialize Small String", () => blackbox(JSON.stringify(strSmall)), ITER, strSmallStr.length << 1);
160
- // dumpToFile("small-str", "serialize");
161
- // bench("Deserialize Small String", () => blackbox(JSON.parse<string>(strSmallStr)), ITER, strSmallStr.length << 1);
162
- // dumpToFile("small-str", "deserialize");
163
-
164
- // bench("Serialize Medium String", () => blackbox(JSON.stringify(strMedium)), ITER, strMediumStr.length << 1);
165
- // dumpToFile("medium-str", "serialize");
166
- // bench("Deserialize Medium String", () => blackbox(JSON.parse<string>(strMediumStr)), ITER, strMediumStr.length << 1);
167
- // dumpToFile("medium-str", "deserialize");
168
-
169
- // bench("Serialize Large String", () => blackbox(JSON.stringify(strLarge)), ITER, strLargeStr.length << 1);
170
- // dumpToFile("large-str", "serialize");
171
- // bench("Deserialize Large String", () => blackbox(JSON.parse<string>(strLargeStr)), ITER, strLargeStr.length << 1);
172
- // dumpToFile("large-str", "deserialize");
@@ -1,37 +0,0 @@
1
- import { JSON } from "..";
2
- import { expect } from "../__tests__/lib";
3
- import { bench, blackbox, dumpToFile } from "./lib/bench";
4
-
5
-
6
- @json
7
- class Vec3 {
8
- public x!: i32;
9
- public y!: i32;
10
- public z!: i32;
11
- }
12
-
13
- const v1: Vec3 = { x: 1, y: 2, z: 3 };
14
- const v2 = '{"x":1,"y":2,"z":3}';
15
-
16
- expect(JSON.stringify(v1)).toBe(v2);
17
- expect(JSON.stringify(JSON.parse<Vec3>(v2))).toBe(v2);
18
-
19
- bench(
20
- "Serialize Vec3",
21
- () => {
22
- blackbox(inline.always(JSON.stringify(v1)));
23
- },
24
- 128_000_00,
25
- v2.length << 1
26
- );
27
- dumpToFile("vec3", "serialize")
28
-
29
- bench(
30
- "Deserialize Vec3",
31
- () => {
32
- blackbox(inline.always(JSON.parse<Vec3>(v2)));
33
- },
34
- 128_000_00,
35
- v2.length << 1
36
- );
37
- dumpToFile("vec3", "deserialize")
@@ -1,35 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
- import { Vec3 } from "./types";
4
-
5
- describe("Should serialize arbitrary types", () => {
6
- expect(JSON.stringify(JSON.Value.from("hello world"))).toBe('"hello world"');
7
- expect(JSON.stringify(JSON.Value.from(0))).toBe("0");
8
- expect(JSON.stringify(JSON.Value.from(true))).toBe("true");
9
- expect(JSON.stringify(JSON.Value.from(new Vec3()))).toBe('{"x":1.0,"y":2.0,"z":3.0}');
10
- expect(JSON.stringify([JSON.Value.from("string"), JSON.Value.from(true), JSON.Value.from(3.14), JSON.Value.from(new Vec3())])).toBe('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0}]');
11
-
12
- const o = new JSON.Obj();
13
- o.set("schema", "http://json-schema.org/draft-07/schema#");
14
- o.set("additionalProperties", false);
15
- o.set("properties", new JSON.Obj());
16
- o.get("properties")!.as<JSON.Obj>().set("duration", new JSON.Obj());
17
- o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("default", 10.0);
18
- o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("description", "Duration of the operation in seconds");
19
- o.get("properties")!.as<JSON.Obj>().get("duration")!.as<JSON.Obj>().set("type", "number");
20
- o.get("properties")!.as<JSON.Obj>().set("steps", new JSON.Obj());
21
- o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("default", 5.0);
22
- o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("description", "Number of steps in the operation");
23
- o.get("properties")!.as<JSON.Obj>().get("steps")!.as<JSON.Obj>().set("type", "number");
24
- o.set("type", "object");
25
-
26
- expect(o.toString()).toBe('{"schema":"http://json-schema.org/draft-07/schema#","additionalProperties":false,"properties":{"duration":{"default":10.0,"description":"Duration of the operation in seconds","type":"number"},"steps":{"default":5.0,"description":"Number of steps in the operation","type":"number"}},"type":"object"}');
27
- });
28
-
29
- describe("Should deserialize arbitrary types", () => {
30
- expect(JSON.parse<JSON.Value>('"hello world"').get<string>()).toBe("hello world");
31
- expect(JSON.parse<JSON.Value>("0.0").toString()).toBe("0.0");
32
- expect(JSON.parse<JSON.Value>("true").toString()).toBe("true");
33
- expect(JSON.stringify(JSON.parse<JSON.Value>('{"x":1.0,"y":2.0,"z":3.0}'))).toBe('{"x":1.0,"y":2.0,"z":3.0}');
34
- expect(JSON.stringify(JSON.parse<JSON.Value[]>('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1.0,2.0,3,true]]'))).toBe('["string",true,3.14,{"x":1.0,"y":2.0,"z":3.0},[1.0,2.0,3.0,true]]');
35
- });
@@ -1,145 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
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
- describe("Should deserialize integer arrays", () => {
62
- expect(JSON.stringify(JSON.parse<u32[]>("[0,100,101]"))).toBe("[0,100,101]");
63
- expect(JSON.stringify(JSON.parse<u64[]>("[0,100,101]"))).toBe("[0,100,101]");
64
- expect(JSON.stringify(JSON.parse<i32[]>("[0,100,101,-100,-101]"))).toBe("[0,100,101,-100,-101]");
65
- expect(JSON.stringify(JSON.parse<i64[]>("[0,100,101,-100,-101]"))).toBe("[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("[7.23,1000.0,1000.0,1.23456,1.23456,0.0,7.23]");
70
- expect(JSON.stringify(JSON.parse<f64[]>("[1e+21,1e+22,1e-7,1e-8,1e-9]"))).toBe("[1e+21,1e+22,1e-7,1e-8,1e-9]");
71
- });
72
-
73
- describe("Should deserialize boolean arrays", () => {
74
- expect(JSON.stringify(JSON.parse<bool[]>("[true,false]"))).toBe("[true,false]");
75
- expect(JSON.stringify(JSON.parse<boolean[]>("[true,false]"))).toBe("[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('["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("[[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("[[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<bool[][]>("[[true],[false]]"))).toBe("[[true],[false]]");
92
- expect(JSON.stringify(JSON.parse<boolean[][]>("[[true],[false]]"))).toBe("[[true],[false]]");
93
- });
94
-
95
- describe("Should deserialize object arrays", () => {
96
- 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('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
97
- });
98
-
99
- describe("Should deserialize raw arrays", () => {
100
- const r1 = JSON.parse<JSON.Raw[]>('[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}]');
101
- expect(r1[0].toString()).toBe('{"x":3.4,"y":1.2,"z":8.3}');
102
- expect(r1[1].toString()).toBe('{"x":3.4,"y":-2.1,"z":9.3}');
103
-
104
- const r2 = JSON.parse<JSON.Raw[][]>('[[{"x":3.4,"y":1.2,"z":8.3},{"x":3.4,"y":-2.1,"z":9.3}],[{"x":0.1,"y":-7.3,"z":4.5}]]');
105
- expect(r2[0][0].toString()).toBe('{"x":3.4,"y":1.2,"z":8.3}');
106
- expect(r2[0][1].toString()).toBe('{"x":3.4,"y":-2.1,"z":9.3}');
107
- expect(r2[1][0].toString()).toBe('{"x":0.1,"y":-7.3,"z":4.5}');
108
-
109
- const r3 = JSON.parse<JSON.Raw[]>("[1,2,3,4,5]");
110
- expect(r3[0]).toBe(1);
111
- expect(r3[1]).toBe(2);
112
- expect(r3[2]).toBe(3);
113
- expect(r3[3]).toBe(4);
114
- expect(r3[4]).toBe(5);
115
-
116
- const r4 = JSON.parse<JSON.Raw[][]>("[[1,2,3,4,5],[6,7,8,9,10]]");
117
- expect(r4[0][0]).toBe(1);
118
- expect(r4[0][1]).toBe(2);
119
- expect(r4[0][2]).toBe(3);
120
- expect(r4[0][3]).toBe(4);
121
- expect(r4[0][4]).toBe(5);
122
-
123
- expect(r4[1][0]).toBe(6);
124
- expect(r4[1][1]).toBe(7);
125
- expect(r4[1][2]).toBe(8);
126
- expect(r4[1][3]).toBe(9);
127
- expect(r4[1][4]).toBe(10);
128
-
129
- const r5 = JSON.parse<JSON.Raw[]>('[{"x":3.4,"y":1.2,"z":8.3},[1,2,3,4,5],"12345",true,false,null,[[]]]');
130
- expect(r5[0].toString()).toBe('{"x":3.4,"y":1.2,"z":8.3}');
131
- expect(r5[1].toString()).toBe("[1,2,3,4,5]");
132
- expect(r5[2]).toBe('"12345"');
133
- expect(r5[3]).toBe(true);
134
- expect(r5[4]).toBe(false);
135
- expect(r5[5]).toBe(null);
136
- expect(r5[6].toString()).toBe("[[]]");
137
- });
138
-
139
-
140
- @json
141
- class Vec3 {
142
- x: f64 = 0.0;
143
- y: f64 = 0.0;
144
- z: f64 = 0.0;
145
- }
@@ -1,12 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
- describe("Should serialize booleans", () => {
5
- expect(JSON.stringify<bool>(true)).toBe("true");
6
- expect(JSON.stringify<bool>(false)).toBe("false");
7
- });
8
-
9
- describe("Should deserialize booleans", () => {
10
- expect(JSON.parse<boolean>("true").toString()).toBe("true");
11
- expect(JSON.parse<boolean>("false").toString()).toBe("false");
12
- });
@@ -1,27 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
- describe("Should serialize JSON.Box<T>", () => {
5
- expect(JSON.stringify<JSON.Box<i32> | null>(null)).toBe("null");
6
-
7
- expect(JSON.stringify<JSON.Box<i32> | null>(new JSON.Box<i32>(0))).toBe("0");
8
-
9
- expect(JSON.stringify<JSON.Box<i32> | null>(new JSON.Box<i32>(1))).toBe("1");
10
-
11
- expect(JSON.stringify<JSON.Box<boolean> | null>(new JSON.Box<boolean>(false))).toBe("false");
12
-
13
- expect(JSON.stringify<JSON.Box<boolean> | null>(new JSON.Box<boolean>(true))).toBe("true");
14
- });
15
-
16
- // This is somewhat clumsy to use. Perhaps I can redesign it or use some transform to make it more transparent.
17
- describe("Should deserialize JSON.Box<T>", () => {
18
- expect((JSON.parse<JSON.Box<i32> | null>("null") == null).toString()).toBe("true");
19
-
20
- expect(JSON.parse<JSON.Box<i32> | null>("0")!.value.toString()).toBe("0");
21
-
22
- expect(JSON.parse<JSON.Box<i32> | null>("1")!.value.toString()).toBe("1");
23
-
24
- expect(JSON.parse<JSON.Box<boolean> | null>("false")!.value.toString()).toBe("false");
25
-
26
- expect(JSON.parse<JSON.Box<boolean> | null>("true")!.value.toString()).toBe("true");
27
- });
@@ -1,56 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
- import { bytes } from "../util";
4
-
5
-
6
- @json
7
- class Point {
8
- x: f64 = 0.0;
9
- y: f64 = 0.0;
10
- constructor(x: f64, y: f64) {
11
- this.x = x;
12
- this.y = y;
13
- }
14
-
15
-
16
- @serializer
17
- serializer(self: Point): string {
18
- return `(${self.x},${self.y})`;
19
- }
20
-
21
-
22
- @deserializer
23
- deserializer(data: string): Point {
24
- const dataSize = bytes(data);
25
- if (dataSize <= 2) throw new Error("Could not deserialize provided data as type Point");
26
-
27
- const c = data.indexOf(",");
28
- const x = data.slice(1, c);
29
- const y = data.slice(c + 1, data.length - 1);
30
-
31
- return new Point(f64.parse(x), f64.parse(y));
32
- }
33
- }
34
-
35
-
36
- @json
37
- class ObjectWithCustom {
38
- value: Point = new Point(0, 0);
39
- constructor(value: Point) {
40
- this.value = value;
41
- }
42
- }
43
-
44
- describe("Should serialize using custom serializers", () => {
45
- expect(JSON.stringify<Point>(new Point(1, 2))).toBe("(1.0,2.0)");
46
- });
47
-
48
- describe("Should deserialize using custom deserializers", () => {
49
- const p1 = JSON.parse<Point>("(1.0,2.0)");
50
- expect(p1.x.toString()).toBe("1.0");
51
- expect(p1.y.toString()).toBe("2.0");
52
- });
53
-
54
- describe("Should serialize and deserialize using nested custom serializers", () => {
55
- expect(JSON.stringify<ObjectWithCustom>(new ObjectWithCustom(new Point(1, 2)))).toBe(`{"value":(1.0,2.0)}`);
56
- });
@@ -1,36 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
- describe("Should serialize Date", () => {
5
- expect(JSON.stringify<Date>(new Date(0))).toBe('"1970-01-01T00:00:00.000Z"');
6
- expect(JSON.stringify<Date>(new Date(1738618120525))).toBe('"2025-02-03T21:28:40.525Z"');
7
- });
8
-
9
- describe("Should deserialize booleans", () => {
10
- // const date = JSON.parse<Date>('"2025-02-03T21:28:40.525Z"');
11
- // console.log("Year: " + date.getUTCFullYear().toString());
12
- // console.log("Month: " + date.getUTCMonth().toString());
13
- // console.log("Day: " + date.getUTCDay().toString());
14
- // console.log("Hours: " + date.getUTCHours().toString());
15
- // console.log("Minutes: " + date.getUTCMinutes().toString());
16
- // console.log("Seconds: " + date.getUTCSeconds().toString());
17
- // console.log("Milliseconds: " + date.getUTCMilliseconds().toString());
18
-
19
- const date1 = JSON.parse<Date>('"1970-01-01T00:00:00.000Z"');
20
- expect(date1.getUTCFullYear().toString()).toBe("1970");
21
- expect(date1.getUTCMonth().toString()).toBe("0");
22
- expect(date1.getUTCDay().toString()).toBe("4");
23
- expect(date1.getUTCHours().toString()).toBe("0");
24
- expect(date1.getUTCMinutes().toString()).toBe("0");
25
- expect(date1.getUTCSeconds().toString()).toBe("0");
26
- expect(date1.getUTCMilliseconds().toString()).toBe("0");
27
-
28
- const date2 = JSON.parse<Date>('"2025-02-03T21:28:40.525Z"');
29
- expect(date2.getUTCFullYear().toString()).toBe("2025");
30
- expect(date2.getUTCMonth().toString()).toBe("1");
31
- expect(date2.getUTCDay().toString()).toBe("1");
32
- expect(date2.getUTCHours().toString()).toBe("21");
33
- expect(date2.getUTCMinutes().toString()).toBe("28");
34
- expect(date2.getUTCSeconds().toString()).toBe("40");
35
- expect(date2.getUTCMilliseconds().toString()).toBe("525");
36
- });
@@ -1,35 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
- enum Enum1 {
5
- Zero = 0,
6
- One = 1,
7
- Two = 2,
8
- Three = 3,
9
- }
10
-
11
-
12
- @json
13
- class DataWithEnum {
14
- v: Enum1 = Enum1.One;
15
- constructor(v: Enum1) {
16
- this.v = v;
17
- }
18
- }
19
-
20
- describe("Should serialize enums", () => {
21
- expect(JSON.stringify<Enum1>(Enum1.One)).toBe("1");
22
- expect(JSON.stringify<Enum1>(Enum1.Zero)).toBe("0");
23
- expect(JSON.stringify<DataWithEnum>(new DataWithEnum(Enum1.Two))).toBe('{"v":2}');
24
- });
25
-
26
- describe("Should deserialize enums", () => {
27
- const date1 = JSON.parse<Enum1>("2");
28
- expect(date1).toBe(Enum1.Two);
29
-
30
- const date2 = JSON.parse<Enum1>("0");
31
- expect(date2).toBe(Enum1.Zero);
32
-
33
- const date3 = JSON.parse<DataWithEnum>('{"v":3}');
34
- expect(date3.v).toBe(Enum1.Three);
35
- });
@@ -1,42 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
- describe("Should serialize floats", () => {
5
- expect(JSON.stringify<f64>(7.23)).toBe("7.23");
6
-
7
- expect(JSON.stringify<f64>(10e2)).toBe("1000.0");
8
-
9
- expect(JSON.stringify<f64>(123456e-5)).toBe("1.23456");
10
-
11
- expect(JSON.stringify<f64>(0.0)).toBe("0.0");
12
-
13
- expect(JSON.stringify<f64>(-7.23)).toBe("-7.23");
14
-
15
- expect(JSON.stringify<f64>(1e-6)).toBe("0.000001");
16
-
17
- expect(JSON.stringify<f64>(1e-7)).toBe("1e-7");
18
-
19
- expect(JSON.stringify<f64>(1e20)).toBe("100000000000000000000.0");
20
-
21
- expect(JSON.stringify<f64>(1e21)).toBe("1e+21");
22
- });
23
-
24
- describe("Should deserialize floats", () => {
25
- expect(JSON.parse<f64>("7.23").toString()).toBe("7.23");
26
-
27
- expect(JSON.parse<f64>("1000.0").toString()).toBe("1000.0");
28
-
29
- expect(JSON.parse<f64>("1.23456").toString()).toBe("1.23456");
30
-
31
- expect(JSON.parse<f64>("0.0").toString()).toBe("0.0");
32
-
33
- expect(JSON.parse<f64>("-7.23").toString()).toBe("-7.23");
34
-
35
- expect(JSON.parse<f64>("0.000001").toString()).toBe("0.000001");
36
-
37
- expect(JSON.parse<f64>("1e-7").toString()).toBe((1e-7).toString());
38
-
39
- expect(JSON.parse<f64>("100000000000000000000.0").toString()).toBe((1e20).toString());
40
-
41
- expect(JSON.parse<f64>("1e+21").toString()).toBe((1e21).toString());
42
- });
@@ -1,49 +0,0 @@
1
- import { JSON } from "..";
2
- import { describe, expect } from "./lib";
3
-
4
-
5
- @json
6
- class GenericTest<T> {
7
- public foo: T;
8
-
9
- constructor(foo: T) {
10
- this.foo = foo;
11
- }
12
- }
13
-
14
-
15
- @json
16
- class Vec3 {
17
- public x!: i32;
18
- public y!: i32;
19
- public z!: i32;
20
- }
21
-
22
- describe("Should serialize generics", () => {
23
- expect(JSON.stringify(new GenericTest<string>("bar"))).toBe('{"foo":"bar"}');
24
- expect(JSON.stringify(new GenericTest<i32>(42))).toBe('{"foo":42}');
25
- expect(JSON.stringify(new GenericTest<boolean>(true))).toBe('{"foo":true}');
26
- expect(JSON.stringify(new GenericTest<Vec3>({ x: 1, y: 2, z: 3 }))).toBe('{"foo":{"x":1,"y":2,"z":3}}');
27
- expect(JSON.stringify(new GenericTest<string[]>(["item1", "item2"]))).toBe('{"foo":["item1","item2"]}');
28
- expect(
29
- JSON.stringify(
30
- new GenericTest<Vec3[]>([
31
- { x: 1, y: 2, z: 3 },
32
- { x: 4, y: 5, z: 6 },
33
- ]),
34
- ),
35
- ).toBe('{"foo":[{"x":1,"y":2,"z":3},{"x":4,"y":5,"z":6}]}');
36
- expect(JSON.stringify(new GenericTest<i32[]>([1, 2, 3]))).toBe('{"foo":[1,2,3]}');
37
- expect(JSON.stringify(new GenericTest<boolean[]>([true, false, true]))).toBe('{"foo":[true,false,true]}');
38
- });
39
-
40
- describe("Should deserialize generics", () => {
41
- expect(JSON.parse<GenericTest<string>>('{"foo":"bar"}').foo).toBe("bar");
42
- expect(JSON.parse<GenericTest<i32>>('{"foo":42}').foo.toString()).toBe("42");
43
- expect(JSON.parse<GenericTest<boolean>>('{"foo":true}').foo).toBe(true);
44
- expect(JSON.stringify(JSON.parse<GenericTest<Vec3>>('{"foo":{"x":1,"y":2,"z":3}}'))).toBe('{"foo":{"x":1,"y":2,"z":3}}');
45
- expect(JSON.stringify(JSON.parse<GenericTest<string[]>>('{"foo":["item1","item2"]}'))).toBe('{"foo":["item1","item2"]}');
46
- expect(JSON.stringify(JSON.parse<GenericTest<Vec3[]>>('{"foo":[{"x":1,"y":2,"z":3},{"x":4,"y":5,"z":6}]}'))).toBe('{"foo":[{"x":1,"y":2,"z":3},{"x":4,"y":5,"z":6}]}');
47
- expect(JSON.stringify(JSON.parse<GenericTest<i32[]>>('{"foo":[1,2,3]}'))).toBe('{"foo":[1,2,3]}');
48
- expect(JSON.stringify(JSON.parse<GenericTest<boolean[]>>('{"foo":[true,false,true]}'))).toBe('{"foo":[true,false,true]}');
49
- });