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 +3 -1
- package/README.md +1 -1
- package/assembly/__tests__/as-json.spec.ts +2 -0
- package/package.json +1 -1
- package/transform/lib/index.js +52 -72
- package/transform/package.json +1 -1
- package/transform/src/index.ts +76 -98
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
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -22,7 +22,7 @@ class AsJSONTransform extends BaseVisitor {
|
|
|
22
22
|
}
|
|
23
23
|
visitMethodDeclaration() { }
|
|
24
24
|
visitClassDeclaration(node) {
|
|
25
|
-
var _c, _d
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
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
|
-
|
|
125
|
-
|
|
115
|
+
if (member.initializer) {
|
|
116
|
+
this.currentClass.initializeStmts.push(`this.${name} = ${toString(member.initializer)}`);
|
|
117
|
+
}
|
|
126
118
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
144
|
-
|
|
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(
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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[
|
|
172
|
-
this.currentClass.encodeStmts[
|
|
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
|
}
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -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 "))
|
|
98
|
+
if (!lineText.startsWith("private ") && !lineText.startsWith("static ")) {
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
108
|
-
|
|
103
|
+
const name = member.name.text;
|
|
104
|
+
let aliasName = name;
|
|
109
105
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
130
|
+
`${encodeKey(aliasName)}:\${this.${name}},`
|
|
140
131
|
);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
// @ts-ignore
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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[
|
|
217
|
-
|
|
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
|
+
}
|