json-as 0.9.19 → 0.9.20

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
@@ -27,6 +27,7 @@ v0.9.16 - JSON.Raw should be completely untouched
27
27
  v0.9.17 - A schema's parent's fields should be included properly
28
28
  v0.9.18 - Should be able to use @alias and @omit*** or JSON.Raw
29
29
  v0.9.19 - Fix arguments in @omitif declarations not working properly
30
+ v0.9.20 - Strings were being received with quotes attached via the toString functionality. Removed that.
30
31
 
31
32
  [UNRELEASED] v1.0.0
32
33
  - Allow nullable primitives
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.19
6
+ v0.9.20
7
7
  </pre>
8
8
  </h5>
9
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.9.19",
3
+ "version": "0.9.20",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -1,512 +1,534 @@
1
- import { FieldDeclaration } from "assemblyscript/dist/assemblyscript.js";
1
+ import { FieldDeclaration, IdentifierExpression, StringLiteralExpression, IntegerLiteralExpression, FloatLiteralExpression, NullExpression, TrueExpression, FalseExpression, } 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";
5
5
  class JSONTransform extends BaseVisitor {
6
- constructor() {
7
- super(...arguments);
8
- this.schemasList = [];
9
- this.sources = new Set();
10
- }
11
- visitMethodDeclaration() {}
12
- visitClassDeclaration(node) {
13
- if (!node.decorators?.length) return;
14
- let found = false;
15
- for (const decorator of node.decorators) {
16
- const name = decorator.name.text;
17
- if (name === "json" || name === "serializable") {
18
- found = true;
19
- break;
20
- }
6
+ constructor() {
7
+ super(...arguments);
8
+ this.schemasList = [];
9
+ this.sources = new Set();
21
10
  }
22
- if (!found) return;
23
- const schema = new SchemaData();
24
- schema.node = node;
25
- schema.name = node.name.text;
26
- const members = [
27
- ...node.members.filter(
28
- (v) => v.kind === 54 /* NodeKind.FieldDeclaration */,
29
- ),
30
- ];
31
- if (node.extendsType) {
32
- schema.parent = this.schemasList.find(
33
- (v) => v.name == node.extendsType?.name.identifier.text,
34
- );
35
- if (schema.parent?.members) {
36
- for (let i = schema.parent.members.length - 1; i >= 0; i--) {
37
- const replace = schema.members.find(
38
- (v) => v.name == schema.parent?.members[i]?.name,
39
- );
40
- if (!replace) {
41
- members.unshift(schema.parent?.members[i].node);
42
- }
11
+ visitMethodDeclaration() { }
12
+ visitClassDeclaration(node) {
13
+ if (!node.decorators?.length)
14
+ return;
15
+ let found = false;
16
+ for (const decorator of node.decorators) {
17
+ const name = decorator.name.text;
18
+ if (name === "json" || name === "serializable") {
19
+ found = true;
20
+ break;
21
+ }
43
22
  }
44
- }
45
- }
46
- if (!members.length) {
47
- let SERIALIZE_RAW_EMPTY = '__SERIALIZE(): string {\n return "{}";\n}';
48
- //let SERIALIZE_PRETTY_EMPTY = "__SERIALIZE_PRETTY(): string {\n return \"{}\";\n}";
49
- let INITIALIZE_EMPTY = "__INITIALIZE(): this {\n return this;\n}";
50
- let DESERIALIZE_EMPTY =
51
- "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
52
- if (process.env["JSON_DEBUG"]) {
53
- console.log(SERIALIZE_RAW_EMPTY);
54
- //console.log(SERIALIZE_PRETTY_EMPTY);
55
- console.log(INITIALIZE_EMPTY);
56
- console.log(DESERIALIZE_EMPTY);
57
- }
58
- const SERIALIZE_RAW_METHOD_EMPTY = SimpleParser.parseClassMember(
59
- SERIALIZE_RAW_EMPTY,
60
- node,
61
- );
62
- //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
63
- const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(
64
- INITIALIZE_EMPTY,
65
- node,
66
- );
67
- const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(
68
- DESERIALIZE_EMPTY,
69
- node,
70
- );
71
- if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
72
- node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
73
- if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
74
- node.members.push(INITIALIZE_METHOD_EMPTY);
75
- if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
76
- node.members.push(DESERIALIZE_METHOD_EMPTY);
77
- this.schemasList.push(schema);
78
- }
79
- for (const member of members) {
80
- const name = member.name;
81
- if (!(member instanceof FieldDeclaration)) continue;
82
- if (!member.type) {
83
- throw new Error(
84
- "Fields must be strongly typed! Found " +
85
- toString(member) +
86
- " at " +
87
- node.range.source.normalizedPath,
88
- );
89
- }
90
- const type = toString(member.type);
91
- if (type.startsWith("(") && type.includes("=>")) continue;
92
- const value = member.initializer ? toString(member.initializer) : null;
93
- if (member.flags == 32 /* CommonFlags.Static */) continue;
94
- if (member.flags === 512 /* CommonFlags.Private */) continue;
95
- if (member.flags === 1024 /* CommonFlags.Protected */) continue;
96
- const mem = new Property();
97
- mem.name = name.text;
98
- mem.type = type;
99
- mem.value = value;
100
- mem.node = member;
101
- if (type == "JSON.Raw") {
102
- mem.flags.set(PropertyFlags.JSON_Raw, []);
103
- }
104
- if (member.decorators) {
105
- for (const decorator of member.decorators) {
106
- const decoratorName = decorator.name.text;
107
- const args = getArgs(decorator.args);
108
- switch (decoratorName) {
109
- case "alias": {
110
- if (!args.length)
111
- throw new Error(
112
- "Expected 1 argument but got zero at @alias in " +
113
- node.range.source.normalizedPath,
114
- );
115
- mem.alias = args[0];
116
- mem.flags.set(PropertyFlags.Alias, args);
117
- break;
118
- }
119
- case "omit": {
120
- mem.flags.set(PropertyFlags.Omit, args);
121
- break;
122
- }
123
- case "omitif": {
124
- if (!decorator.args?.length)
125
- throw new Error(
126
- "Expected 1 argument but got zero at @omitif in " +
127
- node.range.source.normalizedPath,
128
- );
129
- mem.flags.set(PropertyFlags.OmitIf, args);
130
- break;
131
- }
132
- case "omitnull": {
133
- mem.flags.set(PropertyFlags.OmitNull, args);
134
- break;
135
- }
136
- }
23
+ if (!found)
24
+ return;
25
+ const schema = new SchemaData();
26
+ schema.node = node;
27
+ schema.name = node.name.text;
28
+ const members = [
29
+ ...node.members.filter((v) => v.kind === 54 /* NodeKind.FieldDeclaration */),
30
+ ];
31
+ if (node.extendsType) {
32
+ schema.parent = this.schemasList.find((v) => v.name == node.extendsType?.name.identifier.text);
33
+ if (schema.parent?.members) {
34
+ for (let i = schema.parent.members.length - 1; i >= 0; i--) {
35
+ const replace = schema.members.find((v) => v.name == schema.parent?.members[i]?.name);
36
+ if (!replace) {
37
+ members.unshift(schema.parent?.members[i].node);
38
+ }
39
+ }
40
+ }
137
41
  }
138
- }
139
- mem.generate();
140
- if (this.schemasList.find((v) => v.name == type)) {
141
- mem.initialize =
142
- "this." +
143
- name.text +
144
- " = changetype<nonnull<" +
145
- mem.type +
146
- ">>(__new(offsetof<nonnull<" +
147
- mem.type +
148
- ">>(), idof<nonnull<" +
149
- mem.type +
150
- ">>()));\n changetype<nonnull<" +
151
- mem.type +
152
- ">>(this." +
153
- name.text +
154
- ").__INITIALIZE()";
155
- } else if (mem.value) {
156
- mem.initialize = "this." + name.text + " = " + mem.value;
157
- } else if (type === "Map") {
158
- mem.initialize = "this." + name.text + " = new " + mem.type + "()";
159
- } else if (type === "string") {
160
- mem.initialize = "this." + name.text + ' = ""';
161
- } else if (type === "Array") {
162
- mem.initialize =
163
- "this." + name.text + " = instantiate<" + mem.type + ">()";
164
- } else if (type === "bool" || type === "boolean") {
165
- mem.initialize = "this." + name.text + " = false";
166
- } else if (type === "JSON.Raw") {
167
- mem.initialize = "this." + name.text + ' = ""';
168
- } else if (
169
- type === "u8" ||
170
- type === "u16" ||
171
- type === "u32" ||
172
- type === "u64" ||
173
- type === "i8" ||
174
- type === "i16" ||
175
- type === "i32" ||
176
- type === "i64"
177
- ) {
178
- mem.initialize = "this." + name.text + " = 0";
179
- } else if (type === "f32" || type === "f64") {
180
- mem.initialize = "this." + name.text + " = 0.0";
181
- }
182
- schema.members.push(mem);
183
- }
184
- let SERIALIZE_RAW = "__SERIALIZE(): string {\n let out = `{";
185
- let SERIALIZE_PRETTY = "__SERIALIZE_PRETTY(): string {\n let out = `{";
186
- let INITIALIZE = "__INITIALIZE(): this {\n";
187
- let DESERIALIZE =
188
- "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n const len = key_end - key_start;\n";
189
- let indent = " ";
190
- if (!schema.members.length) return;
191
- found = false;
192
- if (
193
- schema.members[0]?.flags.has(PropertyFlags.OmitNull) ||
194
- schema.members[0]?.flags.has(PropertyFlags.OmitIf)
195
- ) {
196
- SERIALIZE_RAW += schema.members[0]?.serialize;
197
- SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize;
198
- } else {
199
- SERIALIZE_RAW += schema.members[0]?.serialize + ",";
200
- SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize + ",\\n";
201
- found = true;
202
- }
203
- if (schema.members[0]?.initialize)
204
- INITIALIZE += " " + schema.members[0]?.initialize + ";\n";
205
- for (let i = 1; i < schema.members.length; i++) {
206
- const member = schema.members[i];
207
- if (member.initialize) INITIALIZE += " " + member.initialize + ";\n";
208
- if (
209
- member.flags.has(PropertyFlags.OmitNull) ||
210
- member.flags.has(PropertyFlags.OmitIf)
211
- ) {
212
- SERIALIZE_RAW += member.serialize;
213
- SERIALIZE_PRETTY += member.serialize;
214
- } else {
215
- SERIALIZE_RAW += member.serialize + ",";
216
- SERIALIZE_PRETTY += indent + member.serialize + ",\\n";
217
- found = true;
218
- }
219
- }
220
- if (found) {
221
- SERIALIZE_RAW +=
222
- "`;\n store<u16>(changetype<usize>(out) + ((out.length - 1) << 1), 125);\n return out;\n}";
223
- SERIALIZE_PRETTY +=
224
- "`;\n store<u32>(changetype<usize>(out) + ((out.length - 2) << 1), 8192010);\n return out;\n}";
225
- } else {
226
- SERIALIZE_RAW += "`;\n};";
227
- SERIALIZE_PRETTY += "`;\n};";
228
- }
229
- INITIALIZE += " return this;\n}";
230
- const sortedMembers = [];
231
- const _sorted = schema.members.sort(
232
- (a, b) => a.name.length - b.name.length,
233
- );
234
- let len = 0;
235
- let offset = 0;
236
- sortedMembers.push([_sorted[0]]);
237
- len = _sorted[0]?.name.length;
238
- for (let i = 1; i < _sorted.length; i++) {
239
- const member = _sorted[i];
240
- if (member.alias?.length || member.name.length > len) {
241
- sortedMembers.push([member]);
242
- len = member.alias?.length || member.name.length;
243
- offset++;
244
- } else {
245
- sortedMembers[offset].push(member);
246
- }
247
- }
248
- let first = true;
249
- for (const memberSet of sortedMembers) {
250
- const firstMember = memberSet[0];
251
- const name = encodeKey(firstMember.alias || firstMember.name);
252
- if (name.length === 1) {
253
- if (first) {
254
- DESERIALIZE +=
255
- " if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
256
- first = false;
257
- } else {
258
- DESERIALIZE +=
259
- "else if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
42
+ if (!members.length) {
43
+ let SERIALIZE_RAW_EMPTY = '__SERIALIZE(): string {\n return "{}";\n}';
44
+ //let SERIALIZE_PRETTY_EMPTY = "__SERIALIZE_PRETTY(): string {\n return \"{}\";\n}";
45
+ let INITIALIZE_EMPTY = "__INITIALIZE(): this {\n return this;\n}";
46
+ let DESERIALIZE_EMPTY = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
47
+ if (process.env["JSON_DEBUG"]) {
48
+ console.log(SERIALIZE_RAW_EMPTY);
49
+ //console.log(SERIALIZE_PRETTY_EMPTY);
50
+ console.log(INITIALIZE_EMPTY);
51
+ console.log(DESERIALIZE_EMPTY);
52
+ }
53
+ const SERIALIZE_RAW_METHOD_EMPTY = SimpleParser.parseClassMember(SERIALIZE_RAW_EMPTY, node);
54
+ //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
55
+ const INITIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(INITIALIZE_EMPTY, node);
56
+ const DESERIALIZE_METHOD_EMPTY = SimpleParser.parseClassMember(DESERIALIZE_EMPTY, node);
57
+ if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
58
+ node.members.push(SERIALIZE_RAW_METHOD_EMPTY);
59
+ if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
60
+ node.members.push(INITIALIZE_METHOD_EMPTY);
61
+ if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
62
+ node.members.push(DESERIALIZE_METHOD_EMPTY);
63
+ this.schemasList.push(schema);
64
+ }
65
+ for (const member of members) {
66
+ const name = member.name;
67
+ if (!(member instanceof FieldDeclaration))
68
+ continue;
69
+ if (!member.type) {
70
+ throw new Error("Fields must be strongly typed! Found " +
71
+ toString(member) +
72
+ " at " +
73
+ node.range.source.normalizedPath);
74
+ }
75
+ const type = toString(member.type);
76
+ if (type.startsWith("(") && type.includes("=>"))
77
+ continue;
78
+ const value = member.initializer ? toString(member.initializer) : null;
79
+ if (member.flags == 32 /* CommonFlags.Static */)
80
+ continue;
81
+ if (member.flags === 512 /* CommonFlags.Private */)
82
+ continue;
83
+ if (member.flags === 1024 /* CommonFlags.Protected */)
84
+ continue;
85
+ const mem = new Property();
86
+ mem.name = name.text;
87
+ mem.type = type;
88
+ mem.value = value;
89
+ mem.node = member;
90
+ if (type == "JSON.Raw") {
91
+ mem.flags.set(PropertyFlags.JSON_Raw, []);
92
+ }
93
+ if (member.decorators) {
94
+ for (const decorator of member.decorators) {
95
+ const decoratorName = decorator.name.text;
96
+ const args = getArgs(decorator.args);
97
+ switch (decoratorName) {
98
+ case "alias": {
99
+ if (!args.length)
100
+ throw new Error("Expected 1 argument but got zero at @alias in " +
101
+ node.range.source.normalizedPath);
102
+ mem.alias = args[0];
103
+ mem.flags.set(PropertyFlags.Alias, args);
104
+ break;
105
+ }
106
+ case "omit": {
107
+ mem.flags.set(PropertyFlags.Omit, args);
108
+ break;
109
+ }
110
+ case "omitif": {
111
+ if (!decorator.args?.length)
112
+ throw new Error("Expected 1 argument but got zero at @omitif in " +
113
+ node.range.source.normalizedPath);
114
+ mem.flags.set(PropertyFlags.OmitIf, args);
115
+ break;
116
+ }
117
+ case "omitnull": {
118
+ mem.flags.set(PropertyFlags.OmitNull, args);
119
+ break;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ mem.generate();
125
+ if (this.schemasList.find((v) => v.name == type)) {
126
+ mem.initialize =
127
+ "this." +
128
+ name.text +
129
+ " = changetype<nonnull<" +
130
+ mem.type +
131
+ ">>(__new(offsetof<nonnull<" +
132
+ mem.type +
133
+ ">>(), idof<nonnull<" +
134
+ mem.type +
135
+ ">>()));\n changetype<nonnull<" +
136
+ mem.type +
137
+ ">>(this." +
138
+ name.text +
139
+ ").__INITIALIZE()";
140
+ }
141
+ else if (mem.value) {
142
+ mem.initialize = "this." + name.text + " = " + mem.value;
143
+ }
144
+ else if (type === "Map") {
145
+ mem.initialize = "this." + name.text + " = new " + mem.type + "()";
146
+ }
147
+ else if (type === "string") {
148
+ mem.initialize = "this." + name.text + ' = ""';
149
+ }
150
+ else if (type === "Array") {
151
+ mem.initialize =
152
+ "this." + name.text + " = instantiate<" + mem.type + ">()";
153
+ }
154
+ else if (type === "bool" || type === "boolean") {
155
+ mem.initialize = "this." + name.text + " = false";
156
+ }
157
+ else if (type === "JSON.Raw") {
158
+ mem.initialize = "this." + name.text + ' = ""';
159
+ }
160
+ else if (type === "u8" ||
161
+ type === "u16" ||
162
+ type === "u32" ||
163
+ type === "u64" ||
164
+ type === "i8" ||
165
+ type === "i16" ||
166
+ type === "i32" ||
167
+ type === "i64") {
168
+ mem.initialize = "this." + name.text + " = 0";
169
+ }
170
+ else if (type === "f32" || type === "f64") {
171
+ mem.initialize = "this." + name.text + " = 0.0";
172
+ }
173
+ schema.members.push(mem);
174
+ }
175
+ let SERIALIZE_RAW = "__SERIALIZE(): string {\n let out = `{";
176
+ let SERIALIZE_PRETTY = "__SERIALIZE_PRETTY(): string {\n let out = `{";
177
+ let INITIALIZE = "__INITIALIZE(): this {\n";
178
+ let DESERIALIZE = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n const len = key_end - key_start;\n";
179
+ let indent = " ";
180
+ if (!schema.members.length)
181
+ return;
182
+ found = false;
183
+ if (schema.members[0]?.flags.has(PropertyFlags.OmitNull) ||
184
+ schema.members[0]?.flags.has(PropertyFlags.OmitIf)) {
185
+ SERIALIZE_RAW += schema.members[0]?.serialize;
186
+ SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize;
260
187
  }
261
- } else if (name.length === 2) {
262
- if (first) {
263
- DESERIALIZE +=
264
- " if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
265
- first = false;
266
- } else {
267
- DESERIALIZE +=
268
- "else if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
188
+ else {
189
+ SERIALIZE_RAW += schema.members[0]?.serialize + ",";
190
+ SERIALIZE_PRETTY += "\\n" + schema.members[0]?.serialize + ",\\n";
191
+ found = true;
269
192
  }
270
- } else if (name.length === 4) {
271
- if (first) {
272
- DESERIALIZE +=
273
- " if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
274
- first = false;
275
- } else {
276
- DESERIALIZE +=
277
- "else if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
193
+ if (schema.members[0]?.initialize)
194
+ INITIALIZE += " " + schema.members[0]?.initialize + ";\n";
195
+ for (let i = 1; i < schema.members.length; i++) {
196
+ const member = schema.members[i];
197
+ if (member.initialize)
198
+ INITIALIZE += " " + member.initialize + ";\n";
199
+ if (member.flags.has(PropertyFlags.OmitNull) ||
200
+ member.flags.has(PropertyFlags.OmitIf)) {
201
+ SERIALIZE_RAW += member.serialize;
202
+ SERIALIZE_PRETTY += member.serialize;
203
+ }
204
+ else {
205
+ SERIALIZE_RAW += member.serialize + ",";
206
+ SERIALIZE_PRETTY += indent + member.serialize + ",\\n";
207
+ found = true;
208
+ }
278
209
  }
279
- } else {
280
- if (first) {
281
- DESERIALIZE += " if (" + name.length + " === len) {\n";
282
- first = false;
283
- } else {
284
- DESERIALIZE += "else if (" + name.length + " === len) {\n";
210
+ if (found) {
211
+ SERIALIZE_RAW +=
212
+ "`;\n store<u16>(changetype<usize>(out) + ((out.length - 1) << 1), 125);\n return out;\n}";
213
+ SERIALIZE_PRETTY +=
214
+ "`;\n store<u32>(changetype<usize>(out) + ((out.length - 2) << 1), 8192010);\n return out;\n}";
285
215
  }
286
- }
287
- let f = true;
288
- for (let i = 0; i < memberSet.length; i++) {
289
- const member = memberSet[i];
290
- if (!member.deserialize) continue;
291
- const name = encodeKey(member.alias || member.name);
292
- if (name.length === 1) {
293
- DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
294
- } else if (name.length === 2) {
295
- DESERIALIZE += ` case ${charCodeAt32(name, 0)}: {\n ${member.deserialize}\n return true;\n }\n`;
296
- } else if (name.length === 4) {
297
- if (f) {
298
- f = false;
299
- DESERIALIZE += ` if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
300
- } else {
301
- DESERIALIZE =
302
- DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
303
- `else if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
304
- }
305
- } else {
306
- if (f) {
307
- f = false;
308
- DESERIALIZE += ` if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
309
- } else {
310
- DESERIALIZE =
311
- DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
312
- ` else if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
313
- }
216
+ else {
217
+ SERIALIZE_RAW += "`;\n};";
218
+ SERIALIZE_PRETTY += "`;\n};";
314
219
  }
315
- }
316
- if (name.length < 3) {
317
- DESERIALIZE += ` default: {\n return false;\n }\n }\n`;
318
- } else if (name.length == 4) {
319
- DESERIALIZE =
320
- DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
321
- ` else {\n return false;\n }\n`;
322
- } else {
323
- DESERIALIZE =
324
- DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
325
- ` else {\n return false;\n }\n`;
326
- }
327
- DESERIALIZE += " } ";
328
- }
329
- DESERIALIZE += "\n return false;\n}";
330
- //console.log(sortedMembers);
331
- if (process.env["JSON_DEBUG"]) {
332
- console.log(SERIALIZE_RAW);
333
- //console.log(SERIALIZE_PRETTY);
334
- console.log(INITIALIZE);
335
- console.log(DESERIALIZE);
220
+ INITIALIZE += " return this;\n}";
221
+ const sortedMembers = [];
222
+ const _sorted = schema.members.sort((a, b) => a.name.length - b.name.length);
223
+ let len = 0;
224
+ let offset = 0;
225
+ sortedMembers.push([_sorted[0]]);
226
+ len = _sorted[0]?.name.length;
227
+ for (let i = 1; i < _sorted.length; i++) {
228
+ const member = _sorted[i];
229
+ if (member.alias?.length || member.name.length > len) {
230
+ sortedMembers.push([member]);
231
+ len = member.alias?.length || member.name.length;
232
+ offset++;
233
+ }
234
+ else {
235
+ sortedMembers[offset].push(member);
236
+ }
237
+ }
238
+ let first = true;
239
+ for (const memberSet of sortedMembers) {
240
+ const firstMember = memberSet[0];
241
+ const name = encodeKey(firstMember.alias || firstMember.name);
242
+ if (name.length === 1) {
243
+ if (first) {
244
+ DESERIALIZE +=
245
+ " if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
246
+ first = false;
247
+ }
248
+ else {
249
+ DESERIALIZE +=
250
+ "else if (1 === len) {\n switch (load<u16>(changetype<usize>(data) + (key_start << 1))) {\n";
251
+ }
252
+ }
253
+ else if (name.length === 2) {
254
+ if (first) {
255
+ DESERIALIZE +=
256
+ " if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
257
+ first = false;
258
+ }
259
+ else {
260
+ DESERIALIZE +=
261
+ "else if (2 === len) {\n switch (load<u32>(changetype<usize>(data) + (key_start << 1))) {\n";
262
+ }
263
+ }
264
+ else if (name.length === 4) {
265
+ if (first) {
266
+ DESERIALIZE +=
267
+ " if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
268
+ first = false;
269
+ }
270
+ else {
271
+ DESERIALIZE +=
272
+ "else if (4 === len) {\n const code = load<u64>(changetype<usize>(data) + (key_start << 1));\n";
273
+ }
274
+ }
275
+ else {
276
+ if (first) {
277
+ DESERIALIZE += " if (" + name.length + " === len) {\n";
278
+ first = false;
279
+ }
280
+ else {
281
+ DESERIALIZE += "else if (" + name.length + " === len) {\n";
282
+ }
283
+ }
284
+ let f = true;
285
+ for (let i = 0; i < memberSet.length; i++) {
286
+ const member = memberSet[i];
287
+ if (!member.deserialize)
288
+ continue;
289
+ const name = encodeKey(member.alias || member.name);
290
+ if (name.length === 1) {
291
+ DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
292
+ }
293
+ else if (name.length === 2) {
294
+ DESERIALIZE += ` case ${charCodeAt32(name, 0)}: {\n ${member.deserialize}\n return true;\n }\n`;
295
+ }
296
+ else if (name.length === 4) {
297
+ if (f) {
298
+ f = false;
299
+ DESERIALIZE += ` if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
300
+ }
301
+ else {
302
+ DESERIALIZE =
303
+ DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
304
+ `else if (${charCodeAt64(name, 0)} === code) {\n ${member.deserialize}\n return true;\n }\n`;
305
+ }
306
+ }
307
+ else {
308
+ if (f) {
309
+ f = false;
310
+ DESERIALIZE += ` if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
311
+ }
312
+ else {
313
+ DESERIALIZE =
314
+ DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
315
+ ` else if (0 == memory.compare(changetype<usize>("${escapeQuote(escapeSlash(name))}"), changetype<usize>(data) + (key_start << 1), ${name.length << 1})) {\n ${member.deserialize}\n return true;\n }\n`;
316
+ }
317
+ }
318
+ }
319
+ if (name.length < 3) {
320
+ DESERIALIZE += ` default: {\n return false;\n }\n }\n`;
321
+ }
322
+ else if (name.length == 4) {
323
+ DESERIALIZE =
324
+ DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
325
+ ` else {\n return false;\n }\n`;
326
+ }
327
+ else {
328
+ DESERIALIZE =
329
+ DESERIALIZE.slice(0, DESERIALIZE.length - 1) +
330
+ ` else {\n return false;\n }\n`;
331
+ }
332
+ DESERIALIZE += " } ";
333
+ }
334
+ DESERIALIZE += "\n return false;\n}";
335
+ //console.log(sortedMembers);
336
+ if (process.env["JSON_DEBUG"]) {
337
+ console.log(SERIALIZE_RAW);
338
+ //console.log(SERIALIZE_PRETTY);
339
+ console.log(INITIALIZE);
340
+ console.log(DESERIALIZE);
341
+ }
342
+ const SERIALIZE_RAW_METHOD = SimpleParser.parseClassMember(SERIALIZE_RAW, node);
343
+ //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
344
+ const INITIALIZE_METHOD = SimpleParser.parseClassMember(INITIALIZE, node);
345
+ const DESERIALIZE_METHOD = SimpleParser.parseClassMember(DESERIALIZE, node);
346
+ if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
347
+ node.members.push(SERIALIZE_RAW_METHOD);
348
+ if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
349
+ node.members.push(INITIALIZE_METHOD);
350
+ if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
351
+ node.members.push(DESERIALIZE_METHOD);
352
+ this.schemasList.push(schema);
336
353
  }
337
- const SERIALIZE_RAW_METHOD = SimpleParser.parseClassMember(
338
- SERIALIZE_RAW,
339
- node,
340
- );
341
- //const SERIALIZE_PRETTY_METHOD = SimpleParser.parseClassMember(SERIALIZE_PRETTY, node);
342
- const INITIALIZE_METHOD = SimpleParser.parseClassMember(INITIALIZE, node);
343
- const DESERIALIZE_METHOD = SimpleParser.parseClassMember(DESERIALIZE, node);
344
- if (!node.members.find((v) => v.name.text == "__SERIALIZE"))
345
- node.members.push(SERIALIZE_RAW_METHOD);
346
- if (!node.members.find((v) => v.name.text == "__INITIALIZE"))
347
- node.members.push(INITIALIZE_METHOD);
348
- if (!node.members.find((v) => v.name.text == "__DESERIALIZE"))
349
- node.members.push(DESERIALIZE_METHOD);
350
- this.schemasList.push(schema);
351
- }
352
- visitSource(node) {
353
- super.visitSource(node);
354
- // Only add the import statement to sources that have JSON decorated classes.
355
- if (!this.sources.has(node)) {
356
- return;
354
+ visitSource(node) {
355
+ super.visitSource(node);
356
+ // Only add the import statement to sources that have JSON decorated classes.
357
+ if (!this.sources.has(node)) {
358
+ return;
359
+ }
357
360
  }
358
- }
359
361
  }
360
362
  export default class Transformer extends Transform {
361
- // Trigger the transform after parse.
362
- afterParse(parser) {
363
- // Create new transform
364
- const transformer = new JSONTransform();
365
- // Sort the sources so that user scripts are visited last
366
- const sources = parser.sources
367
- .filter((source) => !isStdlib(source))
368
- .sort((_a, _b) => {
369
- const a = _a.internalPath;
370
- const b = _b.internalPath;
371
- if (a[0] === "~" && b[0] !== "~") {
372
- return -1;
373
- } else if (a[0] !== "~" && b[0] === "~") {
374
- return 1;
375
- } else {
376
- return 0;
363
+ // Trigger the transform after parse.
364
+ afterParse(parser) {
365
+ // Create new transform
366
+ const transformer = new JSONTransform();
367
+ // Sort the sources so that user scripts are visited last
368
+ const sources = parser.sources
369
+ .filter((source) => !isStdlib(source))
370
+ .sort((_a, _b) => {
371
+ const a = _a.internalPath;
372
+ const b = _b.internalPath;
373
+ if (a[0] === "~" && b[0] !== "~") {
374
+ return -1;
375
+ }
376
+ else if (a[0] !== "~" && b[0] === "~") {
377
+ return 1;
378
+ }
379
+ else {
380
+ return 0;
381
+ }
382
+ });
383
+ // Loop over every source
384
+ for (const source of sources) {
385
+ // Ignore all lib and std. Visit everything else.
386
+ if (!isStdlib(source)) {
387
+ transformer.visit(source);
388
+ }
389
+ }
390
+ // Check that every parent and child class is hooked up correctly
391
+ const schemas = transformer.schemasList;
392
+ for (const schema of schemas) {
393
+ if (schema.parent) {
394
+ const parent = schemas.find((v) => v.name === schema.parent?.name);
395
+ if (!parent)
396
+ throw new Error(`Class ${schema.name} extends its parent class ${schema.parent}, but ${schema.parent} does not include a @json or @serializable decorator! Add the decorator and rebuild.`);
397
+ }
377
398
  }
378
- });
379
- // Loop over every source
380
- for (const source of sources) {
381
- // Ignore all lib and std. Visit everything else.
382
- if (!isStdlib(source)) {
383
- transformer.visit(source);
384
- }
385
- }
386
- // Check that every parent and child class is hooked up correctly
387
- const schemas = transformer.schemasList;
388
- for (const schema of schemas) {
389
- if (schema.parent) {
390
- const parent = schemas.find((v) => v.name === schema.parent?.name);
391
- if (!parent)
392
- throw new Error(
393
- `Class ${schema.name} extends its parent class ${schema.parent}, but ${schema.parent} does not include a @json or @serializable decorator! Add the decorator and rebuild.`,
394
- );
395
- }
396
399
  }
397
- }
398
400
  }
399
401
  var PropertyFlags;
400
402
  (function (PropertyFlags) {
401
- PropertyFlags[(PropertyFlags["Omit"] = 0)] = "Omit";
402
- PropertyFlags[(PropertyFlags["OmitNull"] = 1)] = "OmitNull";
403
- PropertyFlags[(PropertyFlags["OmitIf"] = 2)] = "OmitIf";
404
- PropertyFlags[(PropertyFlags["Alias"] = 3)] = "Alias";
405
- PropertyFlags[(PropertyFlags["JSON_Raw"] = 4)] = "JSON_Raw";
403
+ PropertyFlags[PropertyFlags["Omit"] = 0] = "Omit";
404
+ PropertyFlags[PropertyFlags["OmitNull"] = 1] = "OmitNull";
405
+ PropertyFlags[PropertyFlags["OmitIf"] = 2] = "OmitIf";
406
+ PropertyFlags[PropertyFlags["Alias"] = 3] = "Alias";
407
+ PropertyFlags[PropertyFlags["JSON_Raw"] = 4] = "JSON_Raw";
406
408
  })(PropertyFlags || (PropertyFlags = {}));
407
409
  class Property {
408
- constructor() {
409
- this.name = "";
410
- this.alias = null;
411
- this.type = "";
412
- this.value = null;
413
- this.flags = new Map();
414
- this.serialize = null;
415
- this.deserialize = null;
416
- this.initialize = null;
417
- this.right_s = "";
418
- this.right_d = "";
419
- }
420
- generate() {
421
- const name = this.name;
422
- const escapedName = escapeString(JSON.stringify(this.alias || this.name));
423
- const type = this.type;
424
- if (this.flags.has(PropertyFlags.Omit)) return;
425
- if (this.flags.has(PropertyFlags.JSON_Raw)) {
426
- this.right_s = "this." + name;
427
- this.right_d = "data.substring(value_start, value_end);";
428
- } else {
429
- this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
430
- this.right_d =
431
- "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end))";
410
+ constructor() {
411
+ this.name = "";
412
+ this.alias = null;
413
+ this.type = "";
414
+ this.value = null;
415
+ this.flags = new Map();
416
+ this.serialize = null;
417
+ this.deserialize = null;
418
+ this.initialize = null;
419
+ this.right_s = "";
420
+ this.right_d = "";
432
421
  }
433
- if (this.flags.has(PropertyFlags.OmitIf)) {
434
- const condition = this.flags.get(PropertyFlags.OmitIf)[0];
435
- if (!condition)
436
- throw new Error(
437
- "Could not find condition when using decorator @omitif! Provide at least one condition",
438
- );
439
- this.serialize =
440
- "${" +
441
- condition +
442
- ' ? "" : \'' +
443
- escapedName +
444
- ":' + " +
445
- this.right_s +
446
- ' + ","}';
447
- this.deserialize = "this." + name + " = " + this.right_d + ";";
448
- } else if (this.flags.has(PropertyFlags.OmitNull)) {
449
- this.serialize =
450
- "${changetype<usize>(this." +
451
- name +
452
- ") == <usize>0" +
453
- ' ? "" : \'' +
454
- escapedName +
455
- ":' + " +
456
- this.right_s +
457
- ' + ","}';
458
- this.deserialize = "this." + name + " = " + this.right_d + ";";
459
- } else {
460
- this.serialize = escapedName + ":${" + this.right_s + "}";
461
- this.deserialize = "this." + name + " = " + this.right_d + ";";
422
+ generate() {
423
+ const name = this.name;
424
+ const escapedName = escapeString(JSON.stringify(this.alias || this.name));
425
+ const type = this.type;
426
+ if (this.flags.has(PropertyFlags.Omit))
427
+ return;
428
+ if (this.flags.has(PropertyFlags.JSON_Raw)) {
429
+ this.right_s = "this." + name;
430
+ this.right_d = "data.substring(value_start, value_end);";
431
+ }
432
+ else {
433
+ this.right_s = "__SERIALIZE<" + type + ">(this." + name + ")";
434
+ this.right_d =
435
+ "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end))";
436
+ }
437
+ if (this.flags.has(PropertyFlags.OmitIf)) {
438
+ const condition = this.flags.get(PropertyFlags.OmitIf)[0];
439
+ if (!condition)
440
+ throw new Error("Could not find condition when using decorator @omitif! Provide at least one condition");
441
+ this.serialize =
442
+ "${" +
443
+ condition +
444
+ ' ? "" : \'' +
445
+ escapedName +
446
+ ":' + " +
447
+ this.right_s +
448
+ ' + ","}';
449
+ this.deserialize = "this." + name + " = " + this.right_d + ";";
450
+ }
451
+ else if (this.flags.has(PropertyFlags.OmitNull)) {
452
+ this.serialize =
453
+ "${changetype<usize>(this." +
454
+ name +
455
+ ") == <usize>0" +
456
+ ' ? "" : \'' +
457
+ escapedName +
458
+ ":' + " +
459
+ this.right_s +
460
+ ' + ","}';
461
+ this.deserialize = "this." + name + " = " + this.right_d + ";";
462
+ }
463
+ else {
464
+ this.serialize = escapedName + ":${" + this.right_s + "}";
465
+ this.deserialize = "this." + name + " = " + this.right_d + ";";
466
+ }
462
467
  }
463
- }
464
468
  }
465
469
  class SchemaData {
466
- constructor() {
467
- this.name = "";
468
- this.members = [];
469
- this.parent = null;
470
- }
470
+ constructor() {
471
+ this.name = "";
472
+ this.members = [];
473
+ this.parent = null;
474
+ }
471
475
  }
472
476
  function charCodeAt32(data, offset) {
473
- return (data.charCodeAt(offset + 1) << 16) | data.charCodeAt(offset);
477
+ return (data.charCodeAt(offset + 1) << 16) | data.charCodeAt(offset);
474
478
  }
475
479
  function charCodeAt64(data, offset) {
476
- if (offset + 3 >= data.length) {
477
- throw new Error(
478
- "The string must have at least 4 characters from the specified offset.",
479
- );
480
- }
481
- const firstCharCode = BigInt(data.charCodeAt(offset));
482
- const secondCharCode = BigInt(data.charCodeAt(offset + 1));
483
- const thirdCharCode = BigInt(data.charCodeAt(offset + 2));
484
- const fourthCharCode = BigInt(data.charCodeAt(offset + 3));
485
- const u64Value =
486
- (fourthCharCode << 48n) |
487
- (thirdCharCode << 32n) |
488
- (secondCharCode << 16n) |
489
- firstCharCode;
490
- return u64Value;
480
+ if (offset + 3 >= data.length) {
481
+ throw new Error("The string must have at least 4 characters from the specified offset.");
482
+ }
483
+ const firstCharCode = BigInt(data.charCodeAt(offset));
484
+ const secondCharCode = BigInt(data.charCodeAt(offset + 1));
485
+ const thirdCharCode = BigInt(data.charCodeAt(offset + 2));
486
+ const fourthCharCode = BigInt(data.charCodeAt(offset + 3));
487
+ const u64Value = (fourthCharCode << 48n) |
488
+ (thirdCharCode << 32n) |
489
+ (secondCharCode << 16n) |
490
+ firstCharCode;
491
+ return u64Value;
491
492
  }
492
493
  function encodeKey(key) {
493
- const data = JSON.stringify(key);
494
- return data.slice(1, data.length - 1);
494
+ const data = JSON.stringify(key);
495
+ return data.slice(1, data.length - 1);
495
496
  }
496
497
  function escapeString(data) {
497
- return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
498
+ return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
498
499
  }
499
500
  function escapeSlash(data) {
500
- return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
501
+ return data.replace(/\\/g, "\\\\").replace(/\`/g, "\\`");
501
502
  }
502
503
  function escapeQuote(data) {
503
- return data.replace(/\"/g, '\\"');
504
+ return data.replace(/\"/g, '\\"');
504
505
  }
505
506
  function getArgs(args) {
506
- if (!args) return [];
507
- let out = [];
508
- for (const arg of args) {
509
- out.push(toString(arg));
510
- }
511
- return out;
507
+ if (!args)
508
+ return [];
509
+ let out = [];
510
+ for (const arg of args) {
511
+ if (arg instanceof StringLiteralExpression) {
512
+ out.push(arg.value);
513
+ }
514
+ else if (arg instanceof IntegerLiteralExpression) {
515
+ out.push(i64_to_string(arg.value));
516
+ }
517
+ else if (arg instanceof FloatLiteralExpression) {
518
+ out.push(arg.value.toString());
519
+ }
520
+ else if (arg instanceof NullExpression) {
521
+ out.push(arg.text);
522
+ }
523
+ else if (arg instanceof TrueExpression) {
524
+ out.push(arg.text);
525
+ }
526
+ else if (arg instanceof FalseExpression) {
527
+ out.push(arg.text);
528
+ }
529
+ else if (arg instanceof IdentifierExpression) {
530
+ out.push(arg.text);
531
+ }
532
+ }
533
+ return out;
512
534
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.9.19",
3
+ "version": "0.9.20",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -7,6 +7,12 @@ import {
7
7
  NodeKind,
8
8
  Expression,
9
9
  CommonFlags,
10
+ StringLiteralExpression,
11
+ IntegerLiteralExpression,
12
+ FloatLiteralExpression,
13
+ NullExpression,
14
+ TrueExpression,
15
+ FalseExpression,
10
16
  } from "assemblyscript/dist/assemblyscript.js";
11
17
 
12
18
  import { toString, isStdlib } from "visitor-as/dist/utils.js";
@@ -577,7 +583,21 @@ function getArgs(args: Expression[] | null): string[] {
577
583
  if (!args) return [];
578
584
  let out: string[] = [];
579
585
  for (const arg of args) {
580
- out.push(toString(arg));
586
+ if (arg instanceof StringLiteralExpression) {
587
+ out.push(arg.value);
588
+ } else if (arg instanceof IntegerLiteralExpression) {
589
+ out.push(i64_to_string(arg.value));
590
+ } else if (arg instanceof FloatLiteralExpression) {
591
+ out.push(arg.value.toString());
592
+ } else if (arg instanceof NullExpression) {
593
+ out.push(arg.text);
594
+ } else if (arg instanceof TrueExpression) {
595
+ out.push(arg.text);
596
+ } else if (arg instanceof FalseExpression) {
597
+ out.push(arg.text);
598
+ } else if (arg instanceof IdentifierExpression) {
599
+ out.push(arg.text);
600
+ }
581
601
  }
582
602
  return out;
583
603
  }