json-as 0.7.3 → 0.8.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/assembly/test.ts CHANGED
@@ -1,88 +1,21 @@
1
1
  import { JSON } from "./src/json";
2
-
3
- @serializable
4
- class Vec3 {
5
- x: f64 = 3.4;
6
- y: f64 = 1.2;
7
- z: f64 = 8.3;
8
- }
9
-
10
- @serializable
11
- class Player extends Vec3 {
12
- @alias("first name")
13
- firstName: string;
14
- lastName: string;
15
- lastActive: Date;
16
- age: i32;
17
- pos: Vec3 | null;
18
- isVerified: boolean;
19
- }
20
-
21
- const vec = new Vec3();
22
-
23
- const player: Player = {
24
- firstName: "Emmet",
25
- lastName: "West",
26
- lastActive: new Date(0),
27
- age: 23,
28
- pos: {
29
- x: 3.4,
30
- y: 1.2,
31
- z: 8.3,
32
- },
33
- isVerified: true,
34
- x: 1,
35
- y: 3,
36
- z: 3
37
- }
38
-
39
- let out = "";
40
-
41
- JSON.stringifyTo(vec, out);
42
-
43
- console.log("Original: " + out);
44
- //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
45
- console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));
46
-
47
- console.log("Original: " + JSON.stringify(player));
48
- //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
49
- console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"first name":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));
50
-
51
- @serializable
52
- class Wrapper<T> {
53
- data!: T;
54
- }
55
-
56
- @serializable
57
- class Foo {
58
- @alias("hello")
59
- foo!: string;
60
- }
61
-
62
- @serializable
63
- class Bar {
64
- bar!: string;
65
- }
66
-
67
- const foo: Wrapper<Foo> = {
68
- data: new Foo()
2
+ @json
3
+ class Person {
4
+ private prng: i32 = 23;
5
+ public country: string = '';
6
+
7
+ constructor(id: u32) {
8
+ this.prng = 321;
9
+ const seed = id.toString();
10
+ this.country = this.getCountry();
11
+ }
12
+
13
+ // temp method, returns hard-coded string for now
14
+ private getCountry(): string {
15
+ return "USA";
16
+ }
69
17
  }
70
18
 
71
- foo.data.foo = "ha";
72
- console.log(JSON.stringify(foo));
73
- console.log(JSON.stringify(JSON.parse<Wrapper<Foo>>("{\"data\":{\"hello\":\"ha\"}}")))
74
- /*
75
- // 9,325,755
76
- bench("Stringify Object (Vec3)", () => {
77
- blackbox<string>(vec.__JSON_Serialize());
78
- });
79
-
80
- // 17,747,531 -> 55,517,015
81
- bench("New Parse Object (Vec3)", () => {
82
- blackbox<Vec3>(vec.__JSON_Deserialize(blackbox<string>('{"x":0,"y":0,"z":0}')));
83
- });
84
-
85
- // 17,747,531
86
- bench("Old Parse Object (Vec3)", () => {
87
- blackbox<Vec3>(JSON.parse<Vec3>(blackbox<string>('{"x":3.4,"y":1.2,"z":8.3}')));
88
- });*/
19
+ const person = new Person(1);
20
+ let result = JSON.stringify<Person>(person);
21
+ console.log(result);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.7.3",
3
+ "version": "0.8.1",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -69,70 +69,69 @@ class AsJSONTransform extends BaseVisitor {
69
69
  // @ts-ignore
70
70
  if (mem.type && mem.type.name && mem.type.name.identifier.text) {
71
71
  const member = mem;
72
- if (toString(member).startsWith("static"))
73
- return;
74
72
  const lineText = toString(member);
75
- if (lineText.startsWith("private"))
76
- return;
77
- // @ts-ignore
78
- let type = toString(member.type);
79
- const name = member.name.text;
80
- let aliasName = name;
81
- // @ts-ignore
82
- if (member.decorators && ((_d = member.decorators[0]) === null || _d === void 0 ? void 0 : _d.name.text) === "alias") {
83
- if (member.decorators[0] && member.decorators[0].args[0]) {
84
- // @ts-ignore
85
- aliasName = member.decorators[0].args[0].value;
73
+ //console.log("Member: " + lineText)
74
+ if (!lineText.startsWith("private") && !lineText.startsWith("static")) {
75
+ // @ts-ignore
76
+ let type = toString(member.type);
77
+ const name = member.name.text;
78
+ let aliasName = name;
79
+ // @ts-ignore
80
+ if (member.decorators && ((_d = member.decorators[0]) === null || _d === void 0 ? void 0 : _d.name.text) === "alias") {
81
+ if (member.decorators[0] && member.decorators[0].args[0]) {
82
+ // @ts-ignore
83
+ aliasName = member.decorators[0].args[0].value;
84
+ }
86
85
  }
87
- }
88
- this.currentClass.keys.push(name);
89
- // @ts-ignore
90
- this.currentClass.types.push(type);
91
- // @ts-ignore
92
- if ([
93
- "u8",
94
- "i8",
95
- "u16",
96
- "i16",
97
- "u32",
98
- "i32",
99
- "u64",
100
- "i64",
101
- ].includes(type.toLowerCase())) {
102
- this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
86
+ this.currentClass.keys.push(name);
103
87
  // @ts-ignore
104
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
88
+ this.currentClass.types.push(type);
89
+ // @ts-ignore
90
+ if ([
91
+ "u8",
92
+ "i8",
93
+ "u16",
94
+ "i16",
95
+ "u32",
96
+ "i32",
97
+ "u64",
98
+ "i64",
99
+ ].includes(type.toLowerCase())) {
100
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
101
+ // @ts-ignore
102
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
105
103
  this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
106
104
  return;
107
105
  }`);
108
- if (member.initializer) {
109
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
106
+ if (member.initializer) {
107
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
108
+ }
110
109
  }
111
- }
112
- else // @ts-ignore
113
- if ([
114
- "f32",
115
- "f64",
116
- ].includes(type.toLowerCase())) {
117
- this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
118
- // @ts-ignore
119
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
110
+ else // @ts-ignore
111
+ if ([
112
+ "f32",
113
+ "f64",
114
+ ].includes(type.toLowerCase())) {
115
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
116
+ // @ts-ignore
117
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
120
118
  this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
121
119
  return;
122
120
  }`);
123
- if (member.initializer) {
124
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
121
+ if (member.initializer) {
122
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
123
+ }
125
124
  }
126
- }
127
- else {
128
- this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${JSON.stringify<${type}>(this.${name})},`);
129
- // @ts-ignore
130
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
125
+ else {
126
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${JSON.stringify<${type}>(this.${name})},`);
127
+ // @ts-ignore
128
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
131
129
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
132
130
  return;
133
131
  }`);
134
- if (member.initializer) {
135
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
132
+ if (member.initializer) {
133
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
134
+ }
136
135
  }
137
136
  }
138
137
  }
@@ -180,9 +179,9 @@ class AsJSONTransform extends BaseVisitor {
180
179
  this.schemasList.push(this.currentClass);
181
180
  this.sources.add(node.name.range.source);
182
181
  // Uncomment to see the generated code for debugging.
183
- // console.log(serializeFunc);
184
- // console.log(setKeyFunc);
185
- // console.log(initializeFunc);
182
+ //console.log(serializeFunc);
183
+ //console.log(setKeyFunc);
184
+ //console.log(initializeFunc);
186
185
  }
187
186
  visitSource(node) {
188
187
  super.visitSource(node);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.7.3",
3
+ "version": "0.8.1",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -83,59 +83,38 @@ class AsJSONTransform extends BaseVisitor {
83
83
  // @ts-ignore
84
84
  if (mem.type && mem.type.name && mem.type.name.identifier.text) {
85
85
  const member = mem as FieldDeclaration;
86
- if (toString(member).startsWith("static")) return;
87
86
  const lineText = toString(member);
88
- if (lineText.startsWith("private")) return;
87
+ //console.log("Member: " + lineText)
89
88
 
90
- // @ts-ignore
91
- let type = toString(member.type);
89
+ if (!lineText.startsWith("private") && !lineText.startsWith("static")) {
92
90
 
93
- const name = member.name.text;
94
- let aliasName = name;
91
+ // @ts-ignore
92
+ let type = toString(member.type);
93
+
94
+ const name = member.name.text;
95
+ let aliasName = name;
95
96
 
96
- // @ts-ignore
97
- if (member.decorators && member.decorators[0]?.name.text === "alias") {
98
- if (member.decorators[0] && member.decorators[0].args![0]) {
99
- // @ts-ignore
100
- aliasName = member.decorators[0].args![0].value;
101
- }
102
- }
103
- this.currentClass.keys.push(name);
104
- // @ts-ignore
105
- this.currentClass.types.push(type);
106
- // @ts-ignore
107
- if (
108
- [
109
- "u8",
110
- "i8",
111
- "u16",
112
- "i16",
113
- "u32",
114
- "i32",
115
- "u64",
116
- "i64",
117
- ].includes(type.toLowerCase())
118
- ) {
119
- this.currentClass.encodeStmts.push(
120
- `${encodeKey(aliasName)}:\${this.${name}},`
121
- );
122
97
  // @ts-ignore
123
- this.currentClass.setDataStmts.push(
124
- `if (key.equals(${JSON.stringify(aliasName)})) {
125
- this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
126
- return;
127
- }`
128
- );
129
- if (member.initializer) {
130
- this.currentClass.initializeStmts.push(
131
- `this.${name} = ${toString(member.initializer)}`
132
- );
98
+ if (member.decorators && member.decorators[0]?.name.text === "alias") {
99
+ if (member.decorators[0] && member.decorators[0].args![0]) {
100
+ // @ts-ignore
101
+ aliasName = member.decorators[0].args![0].value;
102
+ }
133
103
  }
134
- } else // @ts-ignore
104
+ this.currentClass.keys.push(name);
105
+ // @ts-ignore
106
+ this.currentClass.types.push(type);
107
+ // @ts-ignore
135
108
  if (
136
109
  [
137
- "f32",
138
- "f64",
110
+ "u8",
111
+ "i8",
112
+ "u16",
113
+ "i16",
114
+ "u32",
115
+ "i32",
116
+ "u64",
117
+ "i64",
139
118
  ].includes(type.toLowerCase())
140
119
  ) {
141
120
  this.currentClass.encodeStmts.push(
@@ -144,32 +123,55 @@ class AsJSONTransform extends BaseVisitor {
144
123
  // @ts-ignore
145
124
  this.currentClass.setDataStmts.push(
146
125
  `if (key.equals(${JSON.stringify(aliasName)})) {
147
- this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
148
- return;
149
- }`
126
+ this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
127
+ return;
128
+ }`
150
129
  );
151
130
  if (member.initializer) {
152
131
  this.currentClass.initializeStmts.push(
153
132
  `this.${name} = ${toString(member.initializer)}`
154
133
  );
155
134
  }
156
- } else {
157
- this.currentClass.encodeStmts.push(
158
- `${encodeKey(aliasName)}:\${JSON.stringify<${type}>(this.${name})},`
159
- );
160
- // @ts-ignore
161
- this.currentClass.setDataStmts.push(
162
- `if (key.equals(${JSON.stringify(aliasName)})) {
135
+ } else // @ts-ignore
136
+ if (
137
+ [
138
+ "f32",
139
+ "f64",
140
+ ].includes(type.toLowerCase())
141
+ ) {
142
+ this.currentClass.encodeStmts.push(
143
+ `${encodeKey(aliasName)}:\${this.${name}},`
144
+ );
145
+ // @ts-ignore
146
+ this.currentClass.setDataStmts.push(
147
+ `if (key.equals(${JSON.stringify(aliasName)})) {
148
+ this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
149
+ return;
150
+ }`
151
+ );
152
+ if (member.initializer) {
153
+ this.currentClass.initializeStmts.push(
154
+ `this.${name} = ${toString(member.initializer)}`
155
+ );
156
+ }
157
+ } else {
158
+ this.currentClass.encodeStmts.push(
159
+ `${encodeKey(aliasName)}:\${JSON.stringify<${type}>(this.${name})},`
160
+ );
161
+ // @ts-ignore
162
+ this.currentClass.setDataStmts.push(
163
+ `if (key.equals(${JSON.stringify(aliasName)})) {
163
164
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
164
165
  return;
165
166
  }`
166
- );
167
- if (member.initializer) {
168
- this.currentClass.initializeStmts.push(
169
- `this.${name} = ${toString(member.initializer)}`
170
167
  );
168
+ if (member.initializer) {
169
+ this.currentClass.initializeStmts.push(
170
+ `this.${name} = ${toString(member.initializer)}`
171
+ );
172
+ }
171
173
  }
172
- }
174
+ }
173
175
  }
174
176
  }
175
177
 
@@ -225,9 +227,9 @@ class AsJSONTransform extends BaseVisitor {
225
227
  this.sources.add(node.name.range.source);
226
228
 
227
229
  // Uncomment to see the generated code for debugging.
228
- // console.log(serializeFunc);
229
- // console.log(setKeyFunc);
230
- // console.log(initializeFunc);
230
+ //console.log(serializeFunc);
231
+ //console.log(setKeyFunc);
232
+ //console.log(initializeFunc);
231
233
  }
232
234
 
233
235
  visitSource(node: Source): void {