json-as 0.8.5 → 0.8.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.
package/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
1
  v0.8.2 - Properties starting with `static` or `private` would be ignored
2
2
  v0.8.3 - Dirty fix to issue #68. Add __JSON_Stringify callable to global scope.
3
- v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice.
3
+ v0.8.4 - Fix #71. Classes with the extending class overriding a property cause the property to be serialized twice.
4
+ v0.8.5 - Fix #73. Support for nullable primatives with Box<T> from as-container
5
+ v0.8.6 - Fix. Forgot to stash before publishing. Stash and push what should have been v0.8.5
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  ██║ ██║███████║ ╚█████╔╝███████║╚██████╔╝██║ ╚████║
8
8
  ╚═╝ ╚═╝╚══════╝ ╚════╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═══╝
9
9
 
10
- v0.8.5
10
+ v0.8.6
11
11
  </pre>
12
12
  </h3>
13
13
 
@@ -53,6 +53,8 @@ class Vec3 {
53
53
  x: f64;
54
54
  y: f64;
55
55
  z: f64;
56
+
57
+ static shouldIgnore: string = "should not be serialized";
56
58
  }
57
59
 
58
60
  @json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -22,7 +22,7 @@ class AsJSONTransform extends BaseVisitor {
22
22
  }
23
23
  visitMethodDeclaration() { }
24
24
  visitClassDeclaration(node) {
25
- var _c, _d, _e, _f, _g, _h, _j, _k, _l;
25
+ var _c, _d;
26
26
  const className = node.name.text;
27
27
  if (!((_c = node.decorators) === null || _c === void 0 ? void 0 : _c.length))
28
28
  return;
@@ -80,96 +80,76 @@ class AsJSONTransform extends BaseVisitor {
80
80
  const member = mem;
81
81
  const lineText = toString(member);
82
82
  //console.log("Member: " + lineText)
83
- if (lineText.startsWith("private ") && lineText.startsWith("static "))
84
- continue;
85
- const omitnull = (_d = member.decorators) === null || _d === void 0 ? void 0 : _d.find(v => v.name.text == "omitnull");
86
- console.log((_e = member.decorators) === null || _e === void 0 ? void 0 : _e.find(v => v.name.text === "omitif"));
87
- const omitif = (_k = (_j = (_h = (_g = (_f = member.decorators) === null || _f === void 0 ? void 0 : _f.find(v => v.name.text === "omitif")) === null || _g === void 0 ? void 0 : _g.args) === null || _h === void 0 ? void 0 : _h[0]) === null || _j === void 0 ? void 0 : _j.value) !== null && _k !== void 0 ? _k : null;
88
- // @ts-ignore
89
- let type = toString(member.type);
90
- const name = member.name.text;
91
- let aliasName = name;
92
- // @ts-ignore
93
- if (member.decorators && ((_l = member.decorators[0]) === null || _l === void 0 ? void 0 : _l.name.text) === "alias") {
94
- if (member.decorators[0] && member.decorators[0].args[0]) {
95
- // @ts-ignore
96
- aliasName = member.decorators[0].args[0].value;
97
- }
98
- }
99
- this.currentClass.keys.push(name);
100
- // @ts-ignore
101
- this.currentClass.types.push(type);
102
- // @ts-ignore
103
- if ([
104
- "u8",
105
- "i8",
106
- "u16",
107
- "i16",
108
- "u32",
109
- "i32",
110
- "u64",
111
- "i64",
112
- ].includes(type.toLowerCase())) {
113
- if (omitif) {
114
- this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`);
115
- }
116
- else {
117
- this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${this.${name}}`);
83
+ if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) {
84
+ // @ts-ignore
85
+ let type = toString(member.type);
86
+ const name = member.name.text;
87
+ let aliasName = name;
88
+ // @ts-ignore
89
+ if (member.decorators && ((_d = member.decorators[0]) === null || _d === void 0 ? void 0 : _d.name.text) === "alias") {
90
+ if (member.decorators[0] && member.decorators[0].args[0]) {
91
+ // @ts-ignore
92
+ aliasName = member.decorators[0].args[0].value;
93
+ }
118
94
  }
95
+ this.currentClass.keys.push(name);
96
+ // @ts-ignore
97
+ this.currentClass.types.push(type);
119
98
  // @ts-ignore
120
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
99
+ if ([
100
+ "u8",
101
+ "i8",
102
+ "u16",
103
+ "i16",
104
+ "u32",
105
+ "i32",
106
+ "u64",
107
+ "i64",
108
+ ].includes(type.toLowerCase())) {
109
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
110
+ // @ts-ignore
111
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
121
112
  this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
122
113
  return;
123
114
  }`);
124
- if (member.initializer) {
125
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
115
+ if (member.initializer) {
116
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
117
+ }
126
118
  }
127
- }
128
- else if ([
129
- "f32",
130
- "f64",
131
- ].includes(type.toLowerCase())) {
132
- if (omitif) {
133
- this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`);
134
- }
135
- else {
136
- this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${this.${name}}`);
137
- }
138
- // @ts-ignore
139
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
119
+ else // @ts-ignore
120
+ if ([
121
+ "f32",
122
+ "f64",
123
+ ].includes(type.toLowerCase())) {
124
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${this.${name}},`);
125
+ // @ts-ignore
126
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
140
127
  this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
141
128
  return;
142
129
  }`);
143
- if (member.initializer) {
144
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
145
- }
146
- }
147
- else {
148
- if (omitnull) {
149
- this.currentClass.encodeStmts.push(`c\${changetype<usize>(this.${name}) !== <usize>0 ? "" : \`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}\`}`);
150
- console.log(`\${changetype<usize>(this.${name}) != <usize>0 ? \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\` : ""}`);
151
- }
152
- else if (omitif) {
153
- this.currentClass.encodeStmts.push(`\${${omitif} ? "" : \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\`}`);
130
+ if (member.initializer) {
131
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
132
+ }
154
133
  }
155
134
  else {
156
- this.currentClass.encodeStmts.push(`,${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}`);
157
- }
158
- // @ts-ignore
159
- this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
135
+ this.currentClass.encodeStmts.push(`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},`);
136
+ // @ts-ignore
137
+ this.currentClass.setDataStmts.push(`if (key.equals(${JSON.stringify(aliasName)})) {
160
138
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
161
139
  return;
162
140
  }`);
163
- if (member.initializer) {
164
- this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
141
+ if (member.initializer) {
142
+ this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
143
+ }
165
144
  }
166
145
  }
167
146
  }
168
147
  }
169
148
  let serializeFunc = "";
170
149
  if (this.currentClass.encodeStmts.length > 0) {
171
- const stmt = this.currentClass.encodeStmts[0];
172
- this.currentClass.encodeStmts[0] = stmt.slice(1);
150
+ const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];
151
+ this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
152
+ stmt.slice(0, stmt.length - 1);
173
153
  serializeFunc = `
174
154
  __JSON_Serialize(): string {
175
155
  return \`{${this.currentClass.encodeStmts.join("")}}\`;
@@ -208,7 +188,7 @@ class AsJSONTransform extends BaseVisitor {
208
188
  this.schemasList.push(this.currentClass);
209
189
  this.sources.add(node.name.range.source);
210
190
  // Uncomment to see the generated code for debugging.
211
- console.log(serializeFunc);
191
+ //console.log(serializeFunc);
212
192
  //console.log(setKeyFunc);
213
193
  //console.log(initializeFunc);
214
194
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -95,116 +95,91 @@ class AsJSONTransform extends BaseVisitor {
95
95
  const lineText = toString(member);
96
96
  //console.log("Member: " + lineText)
97
97
 
98
- if (lineText.startsWith("private ") && lineText.startsWith("static ")) continue;
98
+ if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) {
99
99
 
100
- const omitnull = member.decorators?.find(v => v.name.text == "omitnull");
101
- console.log(member.decorators?.find(v => v.name.text === "omitif"))
102
- const omitif = member.decorators?.find(v => v.name.text === "omitif")?.args?.[0]?.value ?? null;
103
-
104
- // @ts-ignore
105
- let type = toString(member.type);
100
+ // @ts-ignore
101
+ let type = toString(member.type);
106
102
 
107
- const name = member.name.text;
108
- let aliasName = name;
103
+ const name = member.name.text;
104
+ let aliasName = name;
109
105
 
110
- // @ts-ignore
111
- if (member.decorators && member.decorators[0]?.name.text === "alias") {
112
- if (member.decorators[0] && member.decorators[0].args![0]) {
113
- // @ts-ignore
114
- aliasName = member.decorators[0].args![0].value;
106
+ // @ts-ignore
107
+ if (member.decorators && member.decorators[0]?.name.text === "alias") {
108
+ if (member.decorators[0] && member.decorators[0].args![0]) {
109
+ // @ts-ignore
110
+ aliasName = member.decorators[0].args![0].value;
111
+ }
115
112
  }
116
- }
117
- this.currentClass.keys.push(name);
118
- // @ts-ignore
119
- this.currentClass.types.push(type);
120
- // @ts-ignore
121
- if (
122
- [
123
- "u8",
124
- "i8",
125
- "u16",
126
- "i16",
127
- "u32",
128
- "i32",
129
- "u64",
130
- "i64",
131
- ].includes(type.toLowerCase())
132
- ) {
133
- if (omitif) {
134
- this.currentClass.encodeStmts.push(
135
- `\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`
136
- );
137
- } else {
113
+ this.currentClass.keys.push(name);
114
+ // @ts-ignore
115
+ this.currentClass.types.push(type);
116
+ // @ts-ignore
117
+ if (
118
+ [
119
+ "u8",
120
+ "i8",
121
+ "u16",
122
+ "i16",
123
+ "u32",
124
+ "i32",
125
+ "u64",
126
+ "i64",
127
+ ].includes(type.toLowerCase())
128
+ ) {
138
129
  this.currentClass.encodeStmts.push(
139
- `,${encodeKey(aliasName)}:\${this.${name}}`
130
+ `${encodeKey(aliasName)}:\${this.${name}},`
140
131
  );
141
- }
142
- // @ts-ignore
143
- this.currentClass.setDataStmts.push(
144
- `if (key.equals(${JSON.stringify(aliasName)})) {
132
+ // @ts-ignore
133
+ this.currentClass.setDataStmts.push(
134
+ `if (key.equals(${JSON.stringify(aliasName)})) {
145
135
  this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
146
136
  return;
147
137
  }`
148
- );
149
- if (member.initializer) {
150
- this.currentClass.initializeStmts.push(
151
- `this.${name} = ${toString(member.initializer)}`
152
- );
153
- }
154
- } else if (
155
- [
156
- "f32",
157
- "f64",
158
- ].includes(type.toLowerCase())
159
- ) {
160
- if (omitif) {
161
- this.currentClass.encodeStmts.push(
162
- `\${${omitif} ? "" : \`,${encodeKey(aliasName)}:\${this.${name}}\`}`
163
138
  );
164
- } else {
165
- this.currentClass.encodeStmts.push(
166
- `,${encodeKey(aliasName)}:\${this.${name}}`
167
- );
168
- }
169
- // @ts-ignore
170
- this.currentClass.setDataStmts.push(
171
- `if (key.equals(${JSON.stringify(aliasName)})) {
139
+ if (member.initializer) {
140
+ this.currentClass.initializeStmts.push(
141
+ `this.${name} = ${toString(member.initializer)}`
142
+ );
143
+ }
144
+ } else // @ts-ignore
145
+ if (
146
+ [
147
+ "f32",
148
+ "f64",
149
+ ].includes(type.toLowerCase())
150
+ ) {
151
+ this.currentClass.encodeStmts.push(
152
+ `${encodeKey(aliasName)}:\${this.${name}},`
153
+ );
154
+ // @ts-ignore
155
+ this.currentClass.setDataStmts.push(
156
+ `if (key.equals(${JSON.stringify(aliasName)})) {
172
157
  this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
173
158
  return;
174
159
  }`
175
- );
176
- if (member.initializer) {
177
- this.currentClass.initializeStmts.push(
178
- `this.${name} = ${toString(member.initializer)}`
179
- );
180
- }
181
- } else {
182
- if (omitnull) {
183
- this.currentClass.encodeStmts.push(
184
- `${comma}\${changetype<usize>(this.${name}) !== <usize>0 ? "" : \`${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}\`}`
185
- );
186
- console.log(`\${changetype<usize>(this.${name}) != <usize>0 ? \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\` : ""}`)
187
- } else if (omitif) {
188
- this.currentClass.encodeStmts.push(
189
- `\${${omitif} ? "" : \`${encodeKey(aliasName)}:,\${__JSON_Stringify<${type}>(this.${name})}\`}`
190
- );
191
- } else {
192
- this.currentClass.encodeStmts.push(
193
- `,${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})}`
194
- );
195
- }
196
- // @ts-ignore
197
- this.currentClass.setDataStmts.push(
198
- `if (key.equals(${JSON.stringify(aliasName)})) {
160
+ );
161
+ if (member.initializer) {
162
+ this.currentClass.initializeStmts.push(
163
+ `this.${name} = ${toString(member.initializer)}`
164
+ );
165
+ }
166
+ } else {
167
+ this.currentClass.encodeStmts.push(
168
+ `${encodeKey(aliasName)}:\${__JSON_Stringify<${type}>(this.${name})},`
169
+ );
170
+ // @ts-ignore
171
+ this.currentClass.setDataStmts.push(
172
+ `if (key.equals(${JSON.stringify(aliasName)})) {
199
173
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
200
174
  return;
201
175
  }`
202
- );
203
- if (member.initializer) {
204
- this.currentClass.initializeStmts.push(
205
- `this.${name} = ${toString(member.initializer)}`
206
- );
207
- }
176
+ );
177
+ if (member.initializer) {
178
+ this.currentClass.initializeStmts.push(
179
+ `this.${name} = ${toString(member.initializer)}`
180
+ );
181
+ }
182
+ }
208
183
  }
209
184
  }
210
185
  }
@@ -213,8 +188,11 @@ class AsJSONTransform extends BaseVisitor {
213
188
 
214
189
  if (this.currentClass.encodeStmts.length > 0) {
215
190
  const stmt =
216
- this.currentClass.encodeStmts[0]!;
217
- this.currentClass.encodeStmts[0] = stmt!.slice(1);
191
+ this.currentClass.encodeStmts[
192
+ this.currentClass.encodeStmts.length - 1
193
+ ]!;
194
+ this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1] =
195
+ stmt!.slice(0, stmt.length - 1);
218
196
  serializeFunc = `
219
197
  __JSON_Serialize(): string {
220
198
  return \`{${this.currentClass.encodeStmts.join("")}}\`;
@@ -258,7 +236,7 @@ class AsJSONTransform extends BaseVisitor {
258
236
  this.sources.add(node.name.range.source);
259
237
 
260
238
  // Uncomment to see the generated code for debugging.
261
- console.log(serializeFunc);
239
+ //console.log(serializeFunc);
262
240
  //console.log(setKeyFunc);
263
241
  //console.log(initializeFunc);
264
242
  }
@@ -331,4 +309,4 @@ export default class Transformer extends Transform {
331
309
  }
332
310
  }
333
311
  }
334
- }
312
+ }