json-as 0.4.4 → 0.4.7

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/README.md CHANGED
@@ -53,6 +53,7 @@ class Player {
53
53
  lastActive: i32[]
54
54
  age: i32
55
55
  pos: Vec2
56
+ isVerified: boolean
56
57
  }
57
58
 
58
59
  const data: Player = {
@@ -63,19 +64,21 @@ const data: Player = {
63
64
  pos: {
64
65
  x: -3.4,
65
66
  y: 1.2
66
- }
67
+ },
68
+ isVerified: true
67
69
  }
68
70
 
69
71
  const stringified = JSON.stringify<Player>(data);
70
72
  // {
71
73
  // "firstName": "Emmet",
72
74
  // "lastName": "West",
73
- // "lastActive": [8, 27, 2022],
75
+ // "lastActive": [8, 27, 2022],
74
76
  // "age": 23,
75
77
  // "pos": {
76
78
  // "x": -3.4000000953674318,
77
79
  // "y": 1.2000000476837159
78
- // }
80
+ // },
81
+ // "isVerified": true
79
82
  // }
80
83
  console.log(`Stringified: ${stringified}`);
81
84
 
@@ -88,7 +91,8 @@ const parsed = JSON.parse<Player>(stringified);
88
91
  // pos: {
89
92
  // x: -3.4000000953674318,
90
93
  // y: 1.2000000476837159
91
- // }
94
+ // },
95
+ // isVerified: true
92
96
  // }
93
97
  console.log(`Parsed: ${JSON.stringify(parsed)}`);
94
98
  ```
@@ -102,7 +106,7 @@ console.log(`Parsed: ${JSON.stringify(parsed)}`);
102
106
  - Does this support nested structures?
103
107
  Yes, as-json supports nested structures
104
108
  - Does this support whitespace?
105
- Yes, as-json supports whitespace although the current implementation is deathly slow
109
+ Yes, as-json supports whitespace!
106
110
  - How fast is it?
107
111
  Really fast. For example, here are some benchmarks for ser/de a Vec2 with as-json
108
112
  ## Issues
@@ -1,24 +1,24 @@
1
- {
2
- "targets": {
3
- "coverage": {
4
- "lib": ["./node_modules/@as-covers/assembly/index.ts"],
5
- "transform": ["@as-covers/transform", "@as-pect/transform"]
6
- },
7
- "noCoverage": {
8
- "transform": ["@as-pect/transform"]
9
- }
10
- },
11
- "options": {
12
- "exportMemory": true,
13
- "outFile": "output.wasm",
14
- "textFile": "output.wat",
15
- "bindings": "raw",
16
- "exportStart": "_start",
17
- "exportRuntime": true,
18
- "use": ["RTRACE=1"],
19
- "debug": true,
20
- "exportTable": true
21
- },
22
- "extends": "./asconfig.json",
23
- "entries": ["./@as-pect/assembly/assembly/index.ts"]
1
+ {
2
+ "targets": {
3
+ "coverage": {
4
+ "lib": ["./node_modules/@as-covers/assembly/index.ts"],
5
+ "transform": ["@as-covers/transform", "@as-pect/transform"]
6
+ },
7
+ "noCoverage": {
8
+ "transform": ["@as-pect/transform"]
9
+ }
10
+ },
11
+ "options": {
12
+ "exportMemory": true,
13
+ "outFile": "output.wasm",
14
+ "textFile": "output.wat",
15
+ "bindings": "raw",
16
+ "exportStart": "_start",
17
+ "exportRuntime": true,
18
+ "use": ["RTRACE=1"],
19
+ "debug": true,
20
+ "exportTable": true
21
+ },
22
+ "extends": "./asconfig.json",
23
+ "entries": ["./@as-pect/assembly/assembly/index.ts"]
24
24
  }
package/asconfig.json CHANGED
@@ -1,23 +1,8 @@
1
1
  {
2
2
  "targets": {
3
- "debug": {
4
- "outFile": "build/debug.wasm",
5
- "textFile": "build/debug.wat",
6
- "sourceMap": true,
7
- "debug": true
8
- },
9
- "release": {
10
- "outFile": "build/release.wasm",
11
- "textFile": "build/release.wat",
12
- "sourceMap": true,
13
- "optimizeLevel": 3,
14
- "shrinkLevel": 0,
15
- "converge": false,
16
- "noAssert": false
17
- },
18
3
  "test": {
19
4
  "outFile": "build/test.wasm",
20
- "sourceMap": true,
5
+ "sourceMap": false,
21
6
  "optimizeLevel": 0,
22
7
  "shrinkLevel": 0,
23
8
  "converge": false,
@@ -1,4 +1,5 @@
1
1
  import { JSON } from "..";
2
+
2
3
  @json
3
4
  class Vec2 {
4
5
  x: f32;
@@ -10,60 +11,65 @@ const vec: Vec2 = blackbox<Vec2>({
10
11
  y: 0.0,
11
12
  });
12
13
 
14
+ /*
13
15
  bench("Stringify String", () => {
14
16
  blackbox(JSON.stringify(blackbox("Hello")));
15
17
  });
16
18
 
19
+ bench("Parse String", () => {
20
+ blackbox(JSON.parse<string>(blackbox('"Hello"')));
21
+ });
22
+
17
23
  bench("Stringify Boolean", () => {
18
24
  blackbox(JSON.stringify(blackbox(true)));
19
25
  });
20
26
 
27
+ bench("Parse Boolean", () => {
28
+ blackbox(JSON.parse<boolean>(blackbox("true")));
29
+ });
30
+
21
31
  bench("Stringify Integer", () => {
22
32
  blackbox(JSON.stringify(blackbox(314)));
23
33
  });
24
34
 
35
+ bench("Parse Integer", () => {
36
+ blackbox(JSON.parse<i32>(blackbox("314")));
37
+ });
38
+
25
39
  bench("Stringify Float", () => {
26
40
  blackbox(JSON.stringify(blackbox(3.14)));
27
41
  });
28
42
 
43
+ bench("Parse Float", () => {
44
+ blackbox(JSON.parse<f32>(blackbox("3.14")));
45
+ });
46
+
29
47
  bench("Stringify Object (Vec2)", () => {
30
48
  blackbox(JSON.stringify(vec));
31
49
  });
50
+ */
51
+ bench("Parse Object (Vec2)", () => {
52
+ const obj = JSON.parse<Vec2>(blackbox('{"x":0.0,"y":0.0}'));
53
+ });
32
54
 
33
55
  bench("Stringify Array", () => {
34
56
  blackbox(JSON.stringify(blackbox([1, 2, 3, 4, 5])));
35
57
  });
36
58
 
37
- bench("Parse String", () => {
38
- blackbox(JSON.parse<string>(blackbox('"Hello"')));
39
- });
40
-
41
- bench("Parse Boolean", () => {
42
- blackbox(JSON.parse<boolean>(blackbox("true")));
59
+ bench("Parse Array", () => {
60
+ blackbox(JSON.parse<i32[]>(blackbox("[1,2,3,4]")));
43
61
  });
44
62
 
45
- bench("Parse Integer", () => {
46
- blackbox(JSON.parse<i32>(blackbox("314")));
47
- });
48
-
49
- bench("Parse Float", () => {
50
- blackbox(JSON.parse<f32>(blackbox("3.14")));
51
- });
52
-
53
- bench("Parse Object (Vec2)", () => {
54
- blackbox(JSON.parse<Vec2>(blackbox('{"x":0.0,"y":0.0}')));
55
- });
56
-
57
- bench("Parse Boolean Array", () => {
63
+ bench("Stringify Nested Array", () => {
58
64
  blackbox(
59
- JSON.parse<boolean[]>(blackbox("[true,false,true,false,true]"))
65
+ JSON.stringify<string[][]>(
66
+ blackbox([
67
+ ["a", "b", "c"],
68
+ ["d", "e", "f"],
69
+ ])
70
+ )
60
71
  );
61
72
  });
62
-
63
- bench("Parse Integer Array", () => {
64
- blackbox(JSON.parse<u32[]>(blackbox("[1,2,3,4,5]")));
65
- });
66
-
67
- bench("Parse Float Array", () => {
68
- blackbox(JSON.parse<f32[]>(blackbox("[1.0,2.0,3.0,4.0,5.0]")));
73
+ bench("Parse Nested Array", () => {
74
+ blackbox(JSON.parse<string[][]>(blackbox('[["a","b","c"],["d","e","f"]]')));
69
75
  });
@@ -0,0 +1,84 @@
1
+ import { JSON } from ".."
2
+ function canSerde<T>(data: T): void {
3
+ const serialized = JSON.stringify<T>(data);
4
+ const deserialized = JSON.stringify<T>(JSON.parse<T>(serialized));
5
+ expect(serialized).toStrictEqual(deserialized)
6
+ }
7
+ describe("Ser/de Numbers", () => {
8
+ it("should ser/de integers", () => {
9
+ canSerde<i32>(0)
10
+
11
+ canSerde<u32>(100)
12
+ canSerde<u64>(101)
13
+ canSerde<i32>(-100)
14
+ canSerde<i64>(-101)
15
+ });
16
+
17
+ it("should ser/de floats", () => {
18
+ canSerde<f64>(7.23)
19
+ canSerde<f64>(10e2)
20
+ canSerde<f64>(10E2)
21
+
22
+ canSerde<f64>(123456e-5)
23
+
24
+ canSerde<f64>(123456E-5)
25
+
26
+ canSerde<f64>(0.0)
27
+ canSerde<f64>(7.23)
28
+ });
29
+
30
+ it("should ser/de booleans", () => {
31
+ canSerde<bool>(true)
32
+ canSerde<bool>(false)
33
+ canSerde<boolean>(true)
34
+ canSerde<boolean>(false)
35
+ });
36
+
37
+ it("should ser/de strings", () => {
38
+ canSerde<string>('abcdefg')
39
+ canSerde<string>('st"ring" w""ith quotes"')
40
+ canSerde<string>('string \t\r\\"with ran\tdom spa\nces and \nnewlines\n\n\n')
41
+ canSerde<string>('string with colon : comma , brace [ ] bracket { } and quote " and other quote \\"')
42
+ });
43
+
44
+ })
45
+
46
+ describe("Ser/de Array", () => {
47
+ it("should ser/de integer arrays", () => {
48
+ canSerde<u32[]>([0, 100, 101])
49
+ canSerde<u64[]>([0, 100, 101])
50
+
51
+ canSerde<i32[]>([0, 100, 101, -100, -101])
52
+ canSerde<i64[]>([0, 100, 101, -100, -101])
53
+ });
54
+
55
+ it("should ser/de float arrays", () => {
56
+ canSerde<f64[]>([7.23, 10e2, 10E2, 123456e-5, 123456E-5, 0.0, 7.23])
57
+ })
58
+
59
+ it("should ser/de boolean arrays", () => {
60
+ canSerde<bool[]>([true, false])
61
+ canSerde<boolean[]>([true, false])
62
+ })
63
+
64
+ it("should ser/de string arrays", () => {
65
+ canSerde<string[]>(["abcdefg", "st\"ring\" w\"\"ith quotes\"", "string \t\r\\\"with ran\tdom spa\nces and \nnewlines\n\n\n", "string with colon : comma , brace [ ] bracket { } and quote \" and other quote \\\""])
66
+ });
67
+
68
+ it("should ser/de nested integer arrays", () => {
69
+ canSerde<i64[][]>([[100, 101], [-100, -101], [0]])
70
+ });
71
+
72
+ it("should ser/de float arrays", () => {
73
+ canSerde<f64[][]>([[7.23], [10e2], [10E2], [123456e-5], [123456E-5], [0.0], [7.23]])
74
+ })
75
+
76
+ it("should ser/de boolean arrays", () => {
77
+ canSerde<bool[][]>([[true], [false]])
78
+ canSerde<boolean[][]>([[true], [false]])
79
+ })
80
+
81
+ it("should ser/de string arrays", () => {
82
+ canSerde<string[][]>([["abcdefg"], ["st\"ring\" w\"\"ith quotes\""], ["string \t\r\\\"with ran\tdom spa\nces and \nnewlines\n\n\n"], ["string with colon : comma , brace [ ] bracket { } and quote \" and other quote \\\""]])
83
+ });
84
+ });
package/assembly/chars.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export const commaCode = ",".charCodeAt(0);
2
- export const quoteCode = "\"".charCodeAt(0);
2
+ export const quoteCode = '"'.charCodeAt(0);
3
3
  export const backSlashCode = "\\".charCodeAt(0);
4
+ export const forwardSlashCode = "/".charCodeAt(0);
4
5
  export const leftBraceCode = "{".charCodeAt(0);
5
6
  export const rightBraceCode = "}".charCodeAt(0);
6
7
  export const leftBracketCode = "[".charCodeAt(0);