json-as 0.4.9 → 0.5.0

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/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -27,25 +27,17 @@ Or, add it to `asconfig.json`
27
27
  }
28
28
  ```
29
29
 
30
- ## Support
31
-
32
- - ✅ Objects (Supported)
33
- - ✅ Arrays (Supported)
34
- - ✅ Numbers (Supported)
35
- - ✅ Integers (Supported)
36
- - ✅ Null (Supported)
37
- - ❌ Dynamic Variants (Not supported)
38
-
39
30
  ## Usage
40
31
 
41
32
  ```js
42
- import { JSON } from "json-as";
33
+ import { JSON } from "json-as/assembly";
43
34
 
44
35
  @json
45
36
  class Vec2 {
46
37
  x: f32
47
38
  y: f32
48
39
  }
40
+
49
41
  @json
50
42
  class Player {
51
43
  firstName: string
@@ -69,46 +61,24 @@ const data: Player = {
69
61
  }
70
62
 
71
63
  const stringified = JSON.stringify<Player>(data);
72
- // {
73
- // "firstName": "Emmet",
74
- // "lastName": "West",
75
- // "lastActive": [8, 27, 2022],
76
- // "age": 23,
77
- // "pos": {
78
- // "x": -3.4000000953674318,
79
- // "y": 1.2000000476837159
80
- // },
81
- // "isVerified": true
82
- // }
83
- console.log(`Stringified: ${stringified}`);
84
64
 
85
65
  const parsed = JSON.parse<Player>(stringified);
86
- // Player {
87
- // firstName: "Emmet",
88
- // lastName: "West",
89
- // lastActive: [8, 27, 2022],
90
- // age: 23,
91
- // pos: {
92
- // x: -3.4000000953674318,
93
- // y: 1.2000000476837159
94
- // },
95
- // isVerified: true
96
- // }
97
- console.log(`Parsed: ${JSON.stringify(parsed)}`);
98
66
  ```
99
67
 
100
- ## FAQ
101
-
102
- - Does it support the full JSON spec?
103
- Yes, as-json supports the full JSON specification and trys to mimic the JSON API as much as possible
104
- - What are the differences between as-json and the JSON API?
105
- The main difference between as-json and the JSON API is the ability to create new fields in objects during runtime. Since classes are static, Map ser/de support will be added soon allowing the user to parse and stringify dynamic objects and their properties
106
- - Does this support nested structures?
107
- Yes, as-json supports nested structures
108
- - Does this support whitespace?
109
- Yes, as-json supports whitespace!
110
- - How fast is it?
111
- Really fast. For example, here are some benchmarks for ser/de a Vec2 with as-json
68
+ ## Performance
69
+
70
+ **Serialize Object (Vec2):** ~7.29m ops/s
71
+
72
+ **Deserialize Object (Vec2):** ~1.36m ops/s
73
+
74
+ **Serialize Array (int[4]):** ~1.4m ops/s
75
+
76
+ **Deserialize Array (int[4]):** ~2.8m ops/s
77
+
78
+ **Serialize String (5):** ~5.2m ops/sw
79
+
80
+ **Deserialize String (5):** ~1.36m ops/s
81
+
112
82
  ## Issues
113
83
 
114
84
  Please submit an issue to https://github.com/JairusSW/as-json/issues if you find anything wrong with this library
File without changes
package/as-pect.config.js CHANGED
File without changes
package/asconfig.json CHANGED
File without changes
@@ -1,5 +1,4 @@
1
1
  import { JSON } from "..";
2
- import { rainbow } from "as-rainbow/assembly"
3
2
 
4
3
  @json
5
4
  class Vec2 {
@@ -12,8 +11,6 @@ const vec: Vec2 = blackbox<Vec2>({
12
11
  y: 0.0,
13
12
  });
14
13
 
15
- //console.log(rainbow.bgBlue("Running benchmark for as-json"));
16
-
17
14
  bench("Stringify Object (Vec2)", () => {
18
15
  blackbox(JSON.stringify(vec));
19
16
  });
File without changes
File without changes
File without changes
package/assembly/chars.ts CHANGED
File without changes
package/assembly/index.ts CHANGED
@@ -36,6 +36,8 @@ export class JSON {
36
36
  return parseArray<T>(data);
37
37
  // @ts-ignore
38
38
  } else if (isDefined(type.__JSON_Deserialize)) {
39
+ // @ts-ignore
40
+ if (isNullable<T>()) return null;
39
41
  return parseObject<T>(data);
40
42
  } else {
41
43
  // @ts-ignore
@@ -68,6 +70,8 @@ export class JSON {
68
70
  // Class-Based serialization
69
71
  // @ts-ignore
70
72
  else if (isDefined(data.__JSON_Serialize)) {
73
+ // @ts-ignore
74
+ if (isNullable<T>()) return null;
71
75
  // @ts-ignore
72
76
  return data.__JSON_Serialize();
73
77
  }
@@ -79,8 +83,7 @@ export class JSON {
79
83
  // @ts-ignore
80
84
  for (let i = 0; i < data.length - 1; i++) {
81
85
  // @ts-ignore
82
- result.write(JSON.stringify(unchecked(data[i])));
83
- result.write(",");
86
+ result.write(JSON.stringify(unchecked(data[i])) + ",");
84
87
  }
85
88
  // @ts-ignore
86
89
  result.write(JSON.stringify(unchecked(data[data.length - 1])));
package/assembly/test.ts CHANGED
@@ -6,7 +6,10 @@ import {
6
6
  // @ts-ignore
7
7
  @json
8
8
  class Vec2 {
9
- x: f32;
9
+ /**
10
+ * x > 4 && x < 100
11
+ */
12
+ private x: f32 = 0;
10
13
  y: f32;
11
14
  }
12
15
 
@@ -27,14 +30,12 @@ const player: Player = {
27
30
  lastActive: [8, 27, 2022],
28
31
  age: 23,
29
32
  pos: {
30
- x: -3.4,
31
33
  y: 1.2,
32
34
  },
33
35
  isVerified: true,
34
36
  };
35
37
 
36
38
  const vec: Vec2 = {
37
- x: 0.0,
38
39
  y: 0.0
39
40
  }
40
41
  const serializedPlayer = JSON.stringify<Player>(player);
File without changes
package/assembly/util.ts CHANGED
File without changes
package/index.ts CHANGED
File without changes
package/package.json CHANGED
@@ -1,51 +1,51 @@
1
- {
2
- "name": "json-as",
3
- "version": "0.4.9",
4
- "description": "JSON encoder/decoder for AssemblyScript",
5
- "types": "assembly/index.ts",
6
- "author": "Jairus Tanaka",
7
- "contributors": [
8
- "DogWhich"
9
- ],
10
- "license": "MIT",
11
- "scripts": {
12
- "bench:astral": "astral",
13
- "build:test": "asc assembly/test.ts --target test",
14
- "build:transform": "tsc -p ./transform",
15
- "test:wasmtime": "wasmtime ./build/test.wasm",
16
- "test:lunatic": "lunatic ./build/test.wasm",
17
- "test:wasm3": "wasm3 ./build/test.wasm",
18
- "prettier": "as-prettier -w ."
19
- },
20
- "devDependencies": {
21
- "@as-pect/cli": "^7.0.7",
22
- "@as-tral/cli": "^1.1.1",
23
- "@serial-as/json": "^1.0.2",
24
- "as-console": "^6.0.2",
25
- "as-rainbow": "^0.1.0",
26
- "assemblyscript": "^0.20.7",
27
- "assemblyscript-prettier": "^1.0.2",
28
- "typescript": "^4.7.2"
29
- },
30
- "dependencies": {
31
- "as-string-sink": "^0.5.0",
32
- "as-variant": "^0.4.0"
33
- },
34
- "repository": {
35
- "type": "git",
36
- "url": "git+https://github.com/JairusSW/as-json.git"
37
- },
38
- "keywords": [
39
- "assemblyscript",
40
- "json",
41
- "serialize",
42
- "deserialize",
43
- "dynamic",
44
- "serde"
45
- ],
46
- "bugs": {
47
- "url": "https://github.com/JairusSW/as-json/issues"
48
- },
49
- "homepage": "https://github.com/JairusSW/as-json#readme",
50
- "type": "module"
51
- }
1
+ {
2
+ "name": "json-as",
3
+ "version": "0.5.0",
4
+ "description": "JSON encoder/decoder for AssemblyScript",
5
+ "types": "assembly/index.ts",
6
+ "author": "Jairus Tanaka",
7
+ "contributors": [
8
+ "DogWhich"
9
+ ],
10
+ "license": "MIT",
11
+ "scripts": {
12
+ "aspect": "asp",
13
+ "bench:astral": "astral",
14
+ "build:test": "asc assembly/test.ts --target test",
15
+ "build:transform": "tsc -p ./transform",
16
+ "test:wasmtime": "wasmtime ./build/test.wasm",
17
+ "test:lunatic": "lunatic ./build/test.wasm",
18
+ "test:wasm3": "wasm3 ./build/test.wasm",
19
+ "prettier": "as-prettier -w ."
20
+ },
21
+ "devDependencies": {
22
+ "@as-pect/cli": "^7.0.7",
23
+ "@as-tral/cli": "^1.1.1",
24
+ "@assemblyscript/loader": "^0.21.3",
25
+ "@serial-as/json": "^2.0.0",
26
+ "assemblyscript": "^0.20.7",
27
+ "assemblyscript-prettier": "^1.0.2",
28
+ "typescript": "^4.7.2"
29
+ },
30
+ "dependencies": {
31
+ "as-string-sink": "^0.5.0",
32
+ "as-variant": "^0.4.0"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/JairusSW/as-json.git"
37
+ },
38
+ "keywords": [
39
+ "assemblyscript",
40
+ "json",
41
+ "serialize",
42
+ "deserialize",
43
+ "dynamic",
44
+ "serde"
45
+ ],
46
+ "bugs": {
47
+ "url": "https://github.com/JairusSW/as-json/issues"
48
+ },
49
+ "homepage": "https://github.com/JairusSW/as-json#readme",
50
+ "type": "module"
51
+ }
package/tests/index.js CHANGED
File without changes
package/tests/test.js CHANGED
File without changes
@@ -1,5 +1,5 @@
1
1
  import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
2
- import { getName } from "visitor-as/dist/utils.js";
2
+ import { getName, toString } from "visitor-as/dist/utils.js";
3
3
  import { SimpleParser } from "visitor-as/dist/index.js";
4
4
  class AsJSONTransform extends ClassDecorator {
5
5
  constructor() {
@@ -10,6 +10,9 @@ class AsJSONTransform extends ClassDecorator {
10
10
  }
11
11
  visitMethodDeclaration() { }
12
12
  visitFieldDeclaration(node) {
13
+ const lineText = toString(node);
14
+ if (lineText.startsWith("private"))
15
+ return;
13
16
  const name = getName(node);
14
17
  if (!node.type) {
15
18
  throw new Error(`Field ${name} is missing a type declaration`);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.9",
3
+ "version": "0.5.0",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
File without changes
@@ -7,7 +7,7 @@ import {
7
7
  ClassDecorator,
8
8
  registerDecorator,
9
9
  } from "visitor-as/dist/decorator.js";
10
- import { getName } from "visitor-as/dist/utils.js";
10
+ import { getName, toString } from "visitor-as/dist/utils.js";
11
11
  import { SimpleParser } from "visitor-as/dist/index.js";
12
12
 
13
13
  class AsJSONTransform extends ClassDecorator {
@@ -18,6 +18,8 @@ class AsJSONTransform extends ClassDecorator {
18
18
 
19
19
  visitMethodDeclaration(): void {}
20
20
  visitFieldDeclaration(node: FieldDeclaration): void {
21
+ const lineText = toString(node);
22
+ if (lineText.startsWith("private")) return;
21
23
  const name = getName(node);
22
24
  if (!node.type) {
23
25
  throw new Error(`Field ${name} is missing a type declaration`);
File without changes