json-as 0.8.7 → 0.9.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.
package/CHANGELOG CHANGED
@@ -5,4 +5,5 @@ v0.8.5 - Fix #73. Support for nullable primatives with Box<T> from as-container
5
5
  v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
6
6
 
7
7
  v0.9.0 - Large update. Refactor all the code, nullable primitives, rewrite the transform, allow extensibility with @omit keywords, and fix a plethora of bugs
8
- [UNRELEASED] v0.9.1 - Port JSON.Value from the `develop` branch to allow for union types, parsing of arbitrary data, and whatever the hell you want.
8
+ v0.9.1 - Fix #71
9
+ [UNRELEASED] v0.9.x - Port JSON.Value from the `develop` branch to allow for union types, parsing of arbitrary data, and whatever the hell you want.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  ██║ ██║███████║ ╚█████╔╝███████║╚██████╔╝██║ ╚████║
8
8
  ╚═╝ ╚═╝╚══════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═══╝
9
9
 
10
- v0.9.0
10
+ v0.9.1
11
11
  </pre>
12
12
  </h3>
13
13
 
@@ -81,12 +81,50 @@ const stringified = JSON.stringify<Player>(player);
81
81
  const parsed = JSON.parse<Player>(stringified);
82
82
  ```
83
83
 
84
+ Classes can even have inheritance. Here's a nasty example
85
+
86
+ ```js
87
+ @json
88
+ class Base {}
89
+
90
+ @json
91
+ class Vec1 extends Base {
92
+ x: f32 = 1.0;
93
+ }
94
+ @json
95
+ class Vec2 extends Vec1 {
96
+ y: f32 = 2.0;
97
+ }
98
+ @json
99
+ class Vec3 extends Vec2 {
100
+ z: f32 = 3.0;
101
+ }
102
+
103
+ const arr: Base[] = [
104
+ new Vec1(),
105
+ new Vec2(),
106
+ new Vec3()
107
+ ];
108
+
109
+ const serialized = JSON.stringify(arr);
110
+ // [{"x":1.0},{"x":1.0,"y":2.0},{"y":2.0,"x":1.0,"z":3.0}]
111
+ const parsed = JSON.parse<Base[]>(serialized);
112
+ ```
113
+
84
114
  If you use this project in your codebase, consider dropping a [star](https://github.com/JairusSW/as-json). I would really appreciate it!
85
115
 
86
116
  ## Notes
87
117
 
88
118
  If you want a feature, drop an issue (and again, maybe a star). I'll likely add it in less than 7 days.
89
119
 
120
+ ## Contact
121
+
122
+ Contact me at:
123
+
124
+ Email: `me@jairus.dev`
125
+ GitHub: `JairusSW`
126
+ Discord: `jairussw`
127
+
90
128
  ## Performance
91
129
 
92
130
  Run or view the benchmarks [here](https://github.com/JairusSW/as-json/tree/master/bench)
package/assembly/test.ts CHANGED
@@ -1,41 +1,24 @@
1
1
  import { JSON } from ".";
2
2
 
3
3
  @json
4
- class Vec3 {
5
- x: f32 = 0.0;
6
- y: f32 = 0.0;
7
- z: f32 = 0.0;
4
+ class Base {}
5
+ @json
6
+ class Vec1 extends Base {
7
+ x: f32 = 1.0;
8
8
  }
9
-
10
9
  @json
11
- class Player {
12
- @alias("first name")
13
- firstName!: string;
14
- lastName!: string;
15
- lastActive!: i32[];
16
- @omitif("this.age < 18")
17
- age!: i32;
18
- @omitnull()
19
- pos!: Vec3 | null;
20
- isVerified!: boolean;
10
+ class Vec2 extends Vec1 {
11
+ y: f32 = 2.0;
12
+ }
13
+ @json
14
+ class Vec3 extends Vec2 {
15
+ z: f32 = 3.0;
21
16
  }
22
17
 
23
- const player: Player = {
24
- firstName: "Emmet",
25
- lastName: "West",
26
- lastActive: [8, 27, 2022],
27
- age: 13,
28
- pos: {
29
- x: 3.4,
30
- y: 1.2,
31
- z: 8.3
32
- },
33
- isVerified: true
34
- };
35
-
36
- const stringified = JSON.stringify<Player>(player);
37
-
38
- const parsed = JSON.parse<Player>(stringified);
18
+ const arr: Base[] = [
19
+ new Vec1(),
20
+ new Vec2(),
21
+ new Vec3()
22
+ ];
39
23
 
40
- console.log("Stringified: " + stringified);
41
- console.log("Parsed: " + JSON.stringify(parsed));
24
+ console.log(JSON.stringify(arr));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.8.7",
3
+ "version": "0.9.1",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -1,4 +1,4 @@
1
- import { FieldDeclaration, } from "assemblyscript/dist/assemblyscript.js";
1
+ import { FieldDeclaration } from "assemblyscript/dist/assemblyscript.js";
2
2
  import { toString, isStdlib } from "visitor-as/dist/utils.js";
3
3
  import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
4
4
  import { Transform } from "assemblyscript/dist/transform.js";
@@ -12,8 +12,6 @@ class JSONTransform extends BaseVisitor {
12
12
  visitClassDeclaration(node) {
13
13
  if (!node.decorators?.length)
14
14
  return;
15
- if (!node.members?.length)
16
- return;
17
15
  let found = false;
18
16
  for (const decorator of node.decorators) {
19
17
  const name = decorator.name.text;
@@ -28,7 +26,7 @@ class JSONTransform extends BaseVisitor {
28
26
  schema.node = node;
29
27
  schema.name = node.name.text;
30
28
  const members = [
31
- ...node.members
29
+ ...node.members.filter(v => v instanceof FieldDeclaration)
32
30
  ];
33
31
  if (node.extendsType) {
34
32
  schema.parent = this.schemasList.find((v) => v.name == node.extendsType?.name.identifier.text);
@@ -40,6 +38,29 @@ class JSONTransform extends BaseVisitor {
40
38
  }
41
39
  }
42
40
  }
41
+ if (!members.length) {
42
+ let SERIALIZE_RAW_EMPTY = "@inline __SERIALIZE(): string {\n return \"{}\";\n}";
43
+ //let SERIALIZE_PRETTY_EMPTY = "@inline __SERIALIZE_PRETTY(): string {\n return \"{}\";\n}";
44
+ let INITIALIZE_EMPTY = "@inline __INITIALIZE(): this {\n return this;\n}";
45
+ let DESERIALIZE_EMPTY = "@inline __DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
46
+ if (process.env["JSON_DEBUG"]) {
47
+ console.log(SERIALIZE_RAW_EMPTY);
48
+ //console.log(SERIALIZE_PRETTY_EMPTY);
49
+ console.log(INITIALIZE_EMPTY);
50
+ console.log(DESERIALIZE_EMPTY);
51
+ }
52
+ const SERIALIZE_RAW_METHOD_EMPTY = SimpleParser.parseClassMember(SERIALIZE_RAW_EMPTY, node);
53
+ //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
54
+ const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(INITIALIZE_EMPTY, node);
55
+ const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(DESERIALIZE_EMPTY, node);
56
+ if (!node.members.find(v => v.name.text == "__SERIALIZE"))
57
+ node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
58
+ if (!node.members.find(v => v.name.text == "__INITIALIZE"))
59
+ node.members.push(INITIALIZE_METHOD_EMPTY);
60
+ if (!node.members.find(v => v.name.text == "__DESERIALIZE"))
61
+ node.members.push(DESERIALIZE_METHOD_EMPTY);
62
+ this.schemasList.push(schema);
63
+ }
43
64
  for (const member of members) {
44
65
  if (!(member instanceof FieldDeclaration))
45
66
  continue;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.8.7",
3
+ "version": "0.9.1",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -5,9 +5,7 @@ import {
5
5
  NamedTypeNode,
6
6
  StringLiteralExpression,
7
7
  Parser,
8
- Source,
9
- SourceKind,
10
- Tokenizer,
8
+ Source
11
9
  } from "assemblyscript/dist/assemblyscript.js";
12
10
 
13
11
  import { toString, isStdlib } from "visitor-as/dist/utils.js";
@@ -23,7 +21,6 @@ class JSONTransform extends BaseVisitor {
23
21
  visitMethodDeclaration(): void { }
24
22
  visitClassDeclaration(node: ClassDeclaration): void {
25
23
  if (!node.decorators?.length) return;
26
- if (!node.members?.length) return;
27
24
 
28
25
  let found = false;
29
26
  for (const decorator of node.decorators) {
@@ -40,7 +37,7 @@ class JSONTransform extends BaseVisitor {
40
37
  schema.name = node.name.text;
41
38
 
42
39
  const members = [
43
- ...node.members
40
+ ...node.members.filter(v => v instanceof FieldDeclaration)
44
41
  ];
45
42
 
46
43
  if (node.extendsType) {
@@ -58,6 +55,33 @@ class JSONTransform extends BaseVisitor {
58
55
  }
59
56
  }
60
57
 
58
+ if (!members.length) {
59
+ let SERIALIZE_RAW_EMPTY = "@inline __SERIALIZE(): string {\n return \"{}\";\n}";
60
+ //let SERIALIZE_PRETTY_EMPTY = "@inline __SERIALIZE_PRETTY(): string {\n return \"{}\";\n}";
61
+
62
+ let INITIALIZE_EMPTY = "@inline __INITIALIZE(): this {\n return this;\n}";
63
+
64
+ let DESERIALIZE_EMPTY = "@inline __DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
65
+
66
+ if (process.env["JSON_DEBUG"]) {
67
+ console.log(SERIALIZE_RAW_EMPTY);
68
+ //console.log(SERIALIZE_PRETTY_EMPTY);
69
+ console.log(INITIALIZE_EMPTY);
70
+ console.log(DESERIALIZE_EMPTY);
71
+ }
72
+
73
+ const SERIALIZE_RAW_METHOD_EMPTY = SimpleParser.parseClassMember(SERIALIZE_RAW_EMPTY, node);
74
+ //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
75
+ const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(INITIALIZE_EMPTY, node);
76
+ const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(DESERIALIZE_EMPTY, node);
77
+
78
+ if (!node.members.find(v => v.name.text == "__SERIALIZE")) node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
79
+ if (!node.members.find(v => v.name.text == "__INITIALIZE")) node.members.push(INITIALIZE_METHOD_EMPTY);
80
+ if (!node.members.find(v => v.name.text == "__DESERIALIZE")) node.members.push(DESERIALIZE_METHOD_EMPTY);
81
+
82
+ this.schemasList.push(schema);
83
+ }
84
+
61
85
  for (const member of members) {
62
86
  if (!(member instanceof FieldDeclaration)) continue;
63
87
  const name = member.name;