json-as 0.4.5 → 0.4.6

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.
@@ -1,4 +1,4 @@
1
- import { JSON, parseBooleanArray, parseObject, parseStringArray } from "..";
1
+ import { JSON } from "..";
2
2
 
3
3
  @json
4
4
  class Vec2 {
@@ -11,6 +11,7 @@ const vec: Vec2 = blackbox<Vec2>({
11
11
  y: 0.0,
12
12
  });
13
13
 
14
+ /*
14
15
  bench("Stringify String", () => {
15
16
  blackbox(JSON.stringify(blackbox("Hello")));
16
17
  });
@@ -46,9 +47,9 @@ bench("Parse Float", () => {
46
47
  bench("Stringify Object (Vec2)", () => {
47
48
  blackbox(JSON.stringify(vec));
48
49
  });
49
-
50
+ */
50
51
  bench("Parse Object (Vec2)", () => {
51
- blackbox(parseObject<Vec2>(blackbox('{"x":0.0,"y":0.0}')));
52
+ const obj = JSON.parse<Vec2>(blackbox('{"x":0.0,"y":0.0}'));
52
53
  });
53
54
 
54
55
  bench("Stringify Array", () => {
@@ -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/index.ts CHANGED
@@ -166,6 +166,8 @@ function parseNumber<T>(data: string): T {
166
166
  );
167
167
  }
168
168
 
169
+ // @ts-ignore
170
+ @inline
169
171
  export function parseObject<T>(data: string): T {
170
172
  let schema!: T;
171
173
  const result = new Map<string, string>();
package/assembly/test.ts CHANGED
@@ -1,12 +1,7 @@
1
1
  import "wasi";
2
2
  import {
3
- JSON,
4
- parseArrayArray,
5
- parseNumberArray,
6
- parseObject,
7
- parseObjectArray,
3
+ JSON
8
4
  } from ".";
9
- import { removeWhitespace } from "./util";
10
5
 
11
6
  // @ts-ignore
12
7
  @json
@@ -26,7 +21,7 @@ class Player {
26
21
  isVerified: boolean;
27
22
  }
28
23
 
29
- const data: Player = {
24
+ const player: Player = {
30
25
  firstName: "Emmet",
31
26
  lastName: "West",
32
27
  lastActive: [8, 27, 2022],
@@ -38,48 +33,17 @@ const data: Player = {
38
33
  isVerified: true,
39
34
  };
40
35
 
41
- const serialized = JSON.stringify<Player>(data);
42
- console.log("Serialized: " + serialized);
43
- const deserialized = JSON.parse<Player>(serialized);
44
- console.log("Deserialized: " + JSON.stringify(deserialized));
45
- /*
46
- const parsed = JSON.parse<Player>(stringified);
47
- console.log("Vec2 Parse: " + JSON.stringify<Player>(parsed));
48
- console.log(
49
- `Parsed String Array: ${JSON.stringify(
50
- JSON.parse<string[]>(`\n[ "hello" , "world" ] `)
51
- )}`
52
- );
53
-
54
- console.log(
55
- `Parsed Boolean Array: ${JSON.stringify(
56
- JSON.parse<boolean[]>(`\n[ false , true ] `)
57
- )}`
58
- );
59
-
60
- console.log(
61
- `Parsed Number Array: ${JSON.stringify(
62
- JSON.parse<i32[]>(`[ 1 , 2\n ,3\n\t ]`)
63
- )}`
64
- );
65
-
66
- console.log(
67
- JSON.stringify<Vec2>(
68
- load<Vec2>(changetype<usize>(data), offsetof<Player>("pos"))
69
- )
70
- );
36
+ const vec: Vec2 = {
37
+ x: 0.0,
38
+ y: 0.0
39
+ }
40
+ const serializedPlayer = JSON.stringify<Player>(player);
41
+ console.log("Serialized Player: " + serializedPlayer);
42
+ const deserializedPlayer = JSON.parse<Player>(serializedPlayer);
43
+ console.log("Deserialized Player: " + JSON.stringify(deserializedPlayer));
71
44
 
72
- console.log(
73
- JSON.stringify<string[][]>(
74
- parseArrayArray<string[][]>('[["a","b","c"],["d","e","f"]]')
75
- )
76
- );
45
+ const serializedVec2 = JSON.stringify<Vec2>(vec);
46
+ console.log("Serialized Vec2: " + serializedVec2);
47
+ const deserializedVec2 = JSON.parse<Vec2>(serializedVec2);
77
48
 
78
- console.log(
79
- JSON.stringify<Player[][]>(
80
- parseObjectArray<Player[][]>(
81
- '[{"firstName":"Emmet","lastName":"West","lastActive":[8,7],"age":23,"pos":{"x":-3.4000000953674318,"y":1.2000000476837159}}]'
82
- )
83
- )
84
- );
85
- */
49
+ console.log("Deserialized: " + JSON.stringify(deserializedVec2));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -8,7 +8,7 @@ class AsJSONTransform extends ClassDecorator {
8
8
  this.encodeStmts = [];
9
9
  this.decodeStmts = [];
10
10
  }
11
- visitMethodDeclaration(node) { }
11
+ visitMethodDeclaration() { }
12
12
  visitFieldDeclaration(node) {
13
13
  const name = getName(node);
14
14
  if (!node.type) {
@@ -18,7 +18,7 @@ class AsJSONTransform extends ClassDecorator {
18
18
  // @ts-ignore
19
19
  this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
20
20
  // @ts-ignore
21
- this.decodeStmts.push(`${name}: JSON.parse<${type}>(values.get("${name}")),\n`);
21
+ this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`);
22
22
  }
23
23
  visitClassDeclaration(node) {
24
24
  if (!node.members) {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  ClassDeclaration,
3
3
  FieldDeclaration,
4
- MethodDeclaration,
5
4
  Source,
6
5
  } from "assemblyscript/dist/assemblyscript";
7
6
  import {
@@ -17,7 +16,7 @@ class AsJSONTransform extends ClassDecorator {
17
16
  public encodeStmts: string[] = [];
18
17
  public decodeStmts: string[] = [];
19
18
 
20
- visitMethodDeclaration(node: MethodDeclaration): void {}
19
+ visitMethodDeclaration(): void {}
21
20
  visitFieldDeclaration(node: FieldDeclaration): void {
22
21
  const name = getName(node);
23
22
  if (!node.type) {
@@ -33,7 +32,7 @@ class AsJSONTransform extends ClassDecorator {
33
32
 
34
33
  // @ts-ignore
35
34
  this.decodeStmts.push(
36
- `${name}: JSON.parse<${type}>(values.get("${name}")),\n`
35
+ `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
37
36
  );
38
37
  }
39
38
  visitClassDeclaration(node: ClassDeclaration): void {