json-as 0.5.3 → 0.5.4

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/README.md CHANGED
@@ -68,9 +68,9 @@ const parsed = JSON.parse<Player>(stringified);
68
68
 
69
69
  ## Performance
70
70
 
71
- **Serialize Object (Vec2):** ~7.29m ops/s
71
+ **Serialize Object (Vec2):** ~7.20m ops/s
72
72
 
73
- **Deserialize Object (Vec2):** ~1.36m ops/s
73
+ **Deserialize Object (Vec2):** ~2.2m ops/s
74
74
 
75
75
  **Serialize Array (int[4]):** ~1.4m ops/s
76
76
 
@@ -0,0 +1,3 @@
1
+ class Dynamic {
2
+
3
+ }
package/assembly/index.ts CHANGED
@@ -1 +1 @@
1
- export { JSON } from "./json";
1
+ export { JSON } from "./src/json";
@@ -1,3 +1,4 @@
1
+ // Characters
1
2
  export const commaCode = ",".charCodeAt(0);
2
3
  export const quoteCode = '"'.charCodeAt(0);
3
4
  export const backSlashCode = "\\".charCodeAt(0);
@@ -16,3 +17,11 @@ export const aCode = "a".charCodeAt(0);
16
17
  export const lCode = "l".charCodeAt(0);
17
18
  export const sCode = "s".charCodeAt(0);
18
19
  export const nCode = "n".charCodeAt(0);
20
+ // Strings
21
+ export const trueWord = "true";
22
+ export const falseWord = "false";
23
+ export const nullWord = "null";
24
+ export const leftBracketWord = "[";
25
+ export const emptyArrayWord = "[]";
26
+ export const commaWord = ",";
27
+ export const rightBracketWord = "]";
@@ -0,0 +1,455 @@
1
+ import { StringSink } from "as-string-sink/assembly";
2
+ import { isSpace } from "util/string";
3
+ import {
4
+ backSlashCode,
5
+ commaCode,
6
+ commaWord,
7
+ eCode,
8
+ fCode,
9
+ leftBraceCode,
10
+ leftBracketCode,
11
+ leftBracketWord,
12
+ nCode,
13
+ nullWord,
14
+ quoteCode,
15
+ rCode,
16
+ rightBraceCode,
17
+ rightBracketCode,
18
+ rightBracketWord,
19
+ tCode,
20
+ trueWord,
21
+ uCode,
22
+ emptyArrayWord
23
+ } from "./chars";
24
+ import { unsafeCharCodeAt } from "./util";
25
+
26
+ /**
27
+ * JSON Encoder/Decoder for AssemblyScript
28
+ */
29
+ export class JSON {
30
+ /**
31
+ * Stringifies valid JSON data.
32
+ * ```js
33
+ * JSON.stringify<T>(data)
34
+ * ```
35
+ * @param data T
36
+ * @returns string
37
+ */
38
+ static stringify<T>(data: T): string {
39
+ // String
40
+ if (isString<T>()) {
41
+ return '"' + (<string>data).replaceAll('"', '\\"') + '"';
42
+ }
43
+ // Boolean
44
+ else if (isBoolean<T>()) {
45
+ return data ? "true" : "false";
46
+ }
47
+ // Nullable
48
+ else if (isNullable<T>() && data == null) {
49
+ return "null";
50
+ }
51
+ // Integers/Floats
52
+ // @ts-ignore
53
+ else if ((isInteger<T>() || isFloat<T>()) && isFinite(data)) {
54
+ // @ts-ignore
55
+ return data.toString();
56
+ }
57
+ // Class-Based serialization
58
+ // @ts-ignore
59
+ else if (isDefined(data.__JSON_Serialize)) {
60
+ // @ts-ignore
61
+ //if (isNullable<T>()) return "null";
62
+ // @ts-ignore
63
+ return data.__JSON_Serialize();
64
+ }
65
+ // ArrayLike
66
+ else if (isArrayLike<T>()) {
67
+ let result = new StringSink(leftBracketWord);
68
+ // @ts-ignore
69
+ if (data.length == 0) return emptyArrayWord;
70
+ // @ts-ignore
71
+ for (let i = 0; i < data.length - 1; i++) {
72
+ // @ts-ignore
73
+ result.write(JSON.stringify(unchecked(data[i])));
74
+ result.write(commaWord);
75
+ }
76
+ // @ts-ignore
77
+ result.write(JSON.stringify(unchecked(data[data.length - 1])));
78
+ result.write(rightBracketWord);
79
+ return result.toString();
80
+ } else {
81
+ throw new Error(`Could not serialize data of type ${nameof<T>()}. Invalid data provided.`);
82
+ }
83
+ }
84
+ /**
85
+ * Parses valid JSON strings into their original format.
86
+ * ```js
87
+ * JSON.parse<T>(data)
88
+ * ```
89
+ * @param data string
90
+ * @returns T
91
+ */
92
+ static parse<T>(data: string): T {
93
+ let type!: T;
94
+ if (isString<T>()) {
95
+ // @ts-ignore
96
+ return parseString(data);
97
+ } else if (isBoolean<T>()) {
98
+ // @ts-ignore
99
+ return parseBoolean<T>(data);
100
+ } else if (isFloat<T>() || isInteger<T>()) {
101
+ return parseNumber<T>(data);
102
+ } else if (isArrayLike<T>()) {
103
+ // @ts-ignore
104
+ return parseArray<T>(data.trimStart());
105
+ // @ts-ignore
106
+ } else if (isNullable<T>() && data == "null") {
107
+ // @ts-ignore
108
+ return null;
109
+ // @ts-ignore
110
+ } else if (isDefined(type.__JSON_Set_Key)) {
111
+ return parseObject<T>(data.trimStart());
112
+ } else {
113
+ // @ts-ignore
114
+ throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`);
115
+ }
116
+ }
117
+ private static parseObjectValue<T>(data: string): T {
118
+ let type!: T;
119
+ if (isString<T>()) {
120
+ // @ts-ignore
121
+ return data.replaceAll('\\"', '"');
122
+ } else if (isBoolean<T>()) {
123
+ // @ts-ignore
124
+ return parseBoolean<T>(data);
125
+ } else if (isFloat<T>() || isInteger<T>()) {
126
+ return parseNumber<T>(data);
127
+ } else if (isArrayLike<T>()) {
128
+ // @ts-ignore
129
+ return parseArray<T>(data);
130
+ // @ts-ignore
131
+ } else if (isNullable<T>() && data == "null") {
132
+ // @ts-ignore
133
+ return null;
134
+ // @ts-ignore
135
+ } else if (isDefined(type.__JSON_Set_Key)) {
136
+ // @ts-ignore
137
+ //if (isNullable<T>()) return null;
138
+ return parseObject<T>(data);
139
+ } else {
140
+ // @ts-ignore
141
+ //return null;
142
+ throw new Error(`Could not deserialize data ${data} to type ${nameof<T>()}. Invalide data provided.`)
143
+ }
144
+ }
145
+ }
146
+
147
+ // @ts-ignore
148
+ @inline
149
+ function parseString(data: string): string {
150
+ return data.slice(1, data.length - 1).replaceAll('\\"', '"');
151
+ }
152
+
153
+ // @ts-ignore
154
+ @inline
155
+ function parseBoolean<T extends boolean>(data: string): T {
156
+ if (data.length > 3 && data.startsWith("true")) return <T>true;
157
+ else if (data.length > 4 && data.startsWith("false")) return <T>false;
158
+ else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
159
+ }
160
+
161
+ // @ts-ignore
162
+ @inline
163
+ function parseNumber<T>(data: string): T {
164
+ let type: T;
165
+ // @ts-ignore
166
+ if (type instanceof f64) return F64.parseFloat(data);
167
+ // @ts-ignore
168
+ else if (type instanceof f32) return F32.parseFloat(data);
169
+ // @ts-ignore
170
+ else if (type instanceof u64) return U64.parseInt(data);
171
+ // @ts-ignore
172
+ else if (type instanceof u32) return U32.parseInt(data);
173
+ // @ts-ignore
174
+ else if (type instanceof u8) return U8.parseInt(data);
175
+ // @ts-ignore
176
+ else if (type instanceof u16) return U16.parseInt(data);
177
+ // @ts-ignore
178
+ else if (type instanceof i64) return I64.parseInt(data);
179
+ // @ts-ignore
180
+ else if (type instanceof i32) return I32.parseInt(data);
181
+ // @ts-ignore
182
+ else if (type instanceof i16) return I16.parseInt(data);
183
+ // @ts-ignore
184
+ else if (type instanceof i8) return I8.parseInt(data);
185
+ else
186
+ throw new Error(
187
+ `JSON: Cannot parse invalid data into a number. Either "${data}" is not a valid number, or <${nameof<T>()}> is an invald number type.`
188
+ );
189
+ }
190
+
191
+ // @ts-ignore
192
+ @inline
193
+ export function parseObject<T>(data: string): T {
194
+ let schema: nonnull<T> = changetype<nonnull<T>>(__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()));
195
+ let key = "";
196
+ let isKey = false;
197
+ let depth = 1;
198
+ let char = 0;
199
+ let outerLoopIndex = 1;
200
+ for (; outerLoopIndex < data.length - 1; outerLoopIndex++) {
201
+ char = unsafeCharCodeAt(data, outerLoopIndex);
202
+ if (char === leftBracketCode) {
203
+ for (
204
+ let arrayValueIndex = outerLoopIndex;
205
+ arrayValueIndex < data.length - 1;
206
+ arrayValueIndex++
207
+ ) {
208
+ char = unsafeCharCodeAt(data, arrayValueIndex);
209
+ if (char === leftBracketCode) {
210
+ depth = depth << 1;
211
+ } else if (char === rightBracketCode) {
212
+ depth = depth >> 1;
213
+ if (depth === 1) {
214
+ ++arrayValueIndex;
215
+ schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, arrayValueIndex));
216
+ outerLoopIndex = arrayValueIndex;
217
+ isKey = false;
218
+ break;
219
+ }
220
+ }
221
+ }
222
+ } else if (char === leftBraceCode) {
223
+ for (
224
+ let objectValueIndex = outerLoopIndex;
225
+ objectValueIndex < data.length - 1;
226
+ objectValueIndex++
227
+ ) {
228
+ char = unsafeCharCodeAt(data, objectValueIndex);
229
+ if (char === leftBraceCode) {
230
+ depth = depth << 1;
231
+ } else if (char === rightBraceCode) {
232
+ depth = depth >> 1;
233
+ if (depth === 1) {
234
+ ++objectValueIndex;
235
+ schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, objectValueIndex));
236
+ outerLoopIndex = objectValueIndex;
237
+ isKey = false;
238
+ break;
239
+ }
240
+ }
241
+ }
242
+ } else if (char === quoteCode) {
243
+ for (
244
+ let stringValueIndex = ++outerLoopIndex;
245
+ stringValueIndex < data.length - 1;
246
+ stringValueIndex++
247
+ ) {
248
+ char = unsafeCharCodeAt(data, stringValueIndex);
249
+ if (
250
+ char === quoteCode &&
251
+ unsafeCharCodeAt(data, stringValueIndex - 1) !== backSlashCode
252
+ ) {
253
+ if (isKey === false) {
254
+ key = data.slice(outerLoopIndex, stringValueIndex);
255
+ isKey = true;
256
+ } else {
257
+ schema.__JSON_Set_Key(key, data.slice(outerLoopIndex, stringValueIndex));
258
+ isKey = false;
259
+ }
260
+ outerLoopIndex = ++stringValueIndex;
261
+ break;
262
+ }
263
+ }
264
+ } else if (char == nCode) {
265
+ schema.__JSON_Set_Key(key, nullWord);
266
+ isKey = false;
267
+ } else if (
268
+ char === tCode &&
269
+ unsafeCharCodeAt(data, ++outerLoopIndex) === rCode &&
270
+ unsafeCharCodeAt(data, ++outerLoopIndex) === uCode &&
271
+ unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
272
+ ) {
273
+ schema.__JSON_Set_Key(key, trueWord);
274
+ isKey = false;
275
+ } else if (
276
+ char === fCode &&
277
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "a".charCodeAt(0) &&
278
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "l".charCodeAt(0) &&
279
+ unsafeCharCodeAt(data, ++outerLoopIndex) === "s".charCodeAt(0) &&
280
+ unsafeCharCodeAt(data, ++outerLoopIndex) === eCode
281
+ ) {
282
+ schema.__JSON_Set_Key(key, "false");
283
+ isKey = false;
284
+ } else if ((char >= 48 && char <= 57) || char === 45) {
285
+ let numberValueIndex = ++outerLoopIndex;
286
+ for (; numberValueIndex < data.length; numberValueIndex++) {
287
+ char = unsafeCharCodeAt(data, numberValueIndex);
288
+ if (char === commaCode || char === rightBraceCode || isSpace(char)) {
289
+ schema.__JSON_Set_Key(key, data.slice(outerLoopIndex - 1, numberValueIndex));
290
+ outerLoopIndex = numberValueIndex;
291
+ isKey = false;
292
+ break;
293
+ }
294
+ }
295
+ }
296
+ }
297
+ return schema;
298
+ }
299
+
300
+ // @ts-ignore
301
+ @inline
302
+ // @ts-ignore
303
+ export function parseArray<T extends unknown[]>(data: string): T {
304
+ let type!: valueof<T>;
305
+ if (type instanceof String) {
306
+ return <T>parseStringArray(data);
307
+ } else if (isBoolean<valueof<T>>()) {
308
+ // @ts-ignore
309
+ return parseBooleanArray<T>(data);
310
+ } else if (isFloat<valueof<T>>() || isInteger<valueof<T>>()) {
311
+ // @ts-ignore
312
+ return parseNumberArray<T>(data);
313
+ } else if (isArrayLike<valueof<T>>()) {
314
+ // @ts-ignore
315
+ return parseArrayArray<T>(data);
316
+ // @ts-ignore
317
+ } else if (isDefined(type.__JSON_Set_Key)) {
318
+ // @ts-ignore
319
+ return parseObjectArray<T>(data);
320
+ }
321
+ }
322
+
323
+ // @ts-ignore
324
+ @inline
325
+ export function parseStringArray(data: string): string[] {
326
+ const result: string[] = [];
327
+ let lastPos = 0;
328
+ let instr = false;
329
+ for (let i = 1; i < data.length - 1; i++) {
330
+ if (unsafeCharCodeAt(data, i) === quoteCode) {
331
+ if (instr === false) {
332
+ instr = true;
333
+ lastPos = i;
334
+ } else if (unsafeCharCodeAt(data, i - 1) !== backSlashCode) {
335
+ instr = false;
336
+ result.push(data.slice(lastPos + 1, i).replaceAll('\\"', '"'));
337
+ }
338
+ }
339
+ }
340
+ return result;
341
+ }
342
+
343
+ // @ts-ignore
344
+ @inline
345
+ export function parseBooleanArray<T extends boolean[]>(data: string): T {
346
+ const result = instantiate<T>();
347
+ let lastPos = 1;
348
+ let char = 0;
349
+ for (let i = 1; i < data.length - 1; i++) {
350
+ char = unsafeCharCodeAt(data, i);
351
+ /*// if char == "t" && i+3 == "e"
352
+ if (char === tCode && data.charCodeAt(i + 3) === eCode) {
353
+ //i += 3;
354
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+2)));
355
+ //i++;
356
+ } else if (char === fCode && data.charCodeAt(i + 4) === eCode) {
357
+ //i += 4;
358
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i+3)));
359
+ //i++;
360
+ }*/
361
+ if (char === tCode || char === fCode) {
362
+ lastPos = i;
363
+ } else if (char === eCode) {
364
+ i++;
365
+ result.push(parseBoolean<valueof<T>>(data.slice(lastPos, i)));
366
+ }
367
+ }
368
+ return result;
369
+ }
370
+
371
+ // @ts-ignore
372
+ @inline
373
+ export function parseNumberArray<T extends number[]>(data: string): T {
374
+ const result = instantiate<T>();
375
+ let lastPos = 0;
376
+ let char = 0;
377
+ let i = 1;
378
+ for (; i < data.length - 1; i++) {
379
+ char = unsafeCharCodeAt(data, i);
380
+ if ((lastPos === 0 && char >= 48 && char <= 57) || char === 45) {
381
+ lastPos = i;
382
+ } else if ((isSpace(char) || char == commaCode) && lastPos > 0) {
383
+ result.push(parseNumber<valueof<T>>(data.slice(lastPos, i)));
384
+ lastPos = 0;
385
+ }
386
+ }
387
+ for (; i > lastPos - 1; i--) {
388
+ char = unsafeCharCodeAt(data, i);
389
+ if (char !== rightBracketCode) {
390
+ result.push(parseNumber<valueof<T>>(data.slice(lastPos, i + 1)));
391
+ break;
392
+ }
393
+ }
394
+ return result;
395
+ }
396
+
397
+ // @ts-ignore
398
+ @inline
399
+ export function parseArrayArray<T extends unknown[][]>(data: string): T {
400
+ const result = instantiate<T>();
401
+ let char = 0;
402
+ let lastPos = 0;
403
+ let depth = 1;
404
+ let i = 1;
405
+ // Find start of bracket
406
+ //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) {}
407
+ //i++;
408
+ for (; i < data.length - 1; i++) {
409
+ char = unsafeCharCodeAt(data, i);
410
+ if (char === leftBracketCode) {
411
+ if (depth === 1) {
412
+ lastPos = i;
413
+ }
414
+ // Shifting is 6% faster than incrementing
415
+ depth = depth << 1;
416
+ } else if (char === rightBracketCode) {
417
+ depth = depth >> 1;
418
+ if (depth === 1) {
419
+ i++;
420
+ result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
421
+ }
422
+ }
423
+ }
424
+ return result;
425
+ }
426
+
427
+ // @ts-ignore
428
+ @inline
429
+ export function parseObjectArray<T extends unknown[][]>(data: string): T {
430
+ const result = instantiate<T>();
431
+ let char = 0;
432
+ let lastPos = 1;
433
+ let depth = 1;
434
+ let i = 1;
435
+ // Find start of bracket
436
+ //for (; unsafeCharCodeAt(data, i) !== leftBracketCode; i++) { }
437
+ //i++;
438
+ for (; i < data.length - 1; i++) {
439
+ char = unsafeCharCodeAt(data, i);
440
+ if (char === leftBraceCode) {
441
+ if (depth === 1) {
442
+ lastPos = i;
443
+ }
444
+ // Shifting is 6% faster than incrementing
445
+ depth = depth << 1;
446
+ } else if (char === rightBraceCode) {
447
+ depth = depth >> 1;
448
+ if (depth === 1) {
449
+ i++;
450
+ result.push(JSON.parse<valueof<T>>(data.slice(lastPos, i)));
451
+ }
452
+ }
453
+ }
454
+ return result;
455
+ }
File without changes
package/index.ts CHANGED
@@ -1 +1 @@
1
- export { JSON } from "./assembly/json";
1
+ export { JSON } from "./assembly/src/json";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -29,7 +29,8 @@
29
29
  "typescript": "^4.7.2"
30
30
  },
31
31
  "dependencies": {
32
- "as-string-sink": "^0.5.0"
32
+ "as-string-sink": "^0.5.0",
33
+ "as-variant": "^0.4.0"
33
34
  },
34
35
  "repository": {
35
36
  "type": "git",
@@ -6,9 +6,10 @@ class AsJSONTransform extends ClassDecorator {
6
6
  super(...arguments);
7
7
  this.sources = [];
8
8
  this.encodeStmts = [];
9
- this.decodeStmts = [];
10
- this.checkDecodeStmts = [];
9
+ //public decodeStmts: string[] = [];
10
+ this.setDataStmts = [];
11
11
  }
12
+ //public checkDecodeStmts: string[] = [];
12
13
  visitMethodDeclaration() { }
13
14
  visitFieldDeclaration(node) {
14
15
  const lineText = toString(node);
@@ -22,9 +23,19 @@ class AsJSONTransform extends ClassDecorator {
22
23
  // @ts-ignore
23
24
  this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
24
25
  // @ts-ignore
25
- this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`);
26
+ //this.decodeStmts.push(
27
+ // `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
28
+ //);
26
29
  // @ts-ignore
27
- this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n');
30
+ this.setDataStmts.push(`if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) {
31
+ this.${name} = JSON.parseObjectValue<${type}>(value);
32
+ return;
33
+ }
34
+ `);
35
+ // @ts-ignore
36
+ //this.checkDecodeStmts.push(
37
+ // ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
38
+ //);
28
39
  }
29
40
  visitClassDeclaration(node) {
30
41
  if (!node.members) {
@@ -38,7 +49,6 @@ class AsJSONTransform extends ClassDecorator {
38
49
  const stmt = this.encodeStmts[this.encodeStmts.length - 1];
39
50
  this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
40
51
  serializeFunc = `
41
- @inline
42
52
  __JSON_Serialize(): string {
43
53
  return \`{${this.encodeStmts.join("")}}\`;
44
54
  }
@@ -46,31 +56,50 @@ class AsJSONTransform extends ClassDecorator {
46
56
  }
47
57
  else {
48
58
  serializeFunc = `
49
- @inline
50
59
  __JSON_Serialize(): string {
51
60
  return "{}";
52
61
  }
53
62
  `;
54
63
  }
55
- const deserializeFunc = `
56
- @inline
57
- __JSON_Deserialize<T>(values: Map<string, string>): T {
58
- ${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""}
59
- return {
60
- ${
61
- // @ts-ignore
62
- this.decodeStmts.join("")}
64
+ /*const deserializeFunc = `
65
+ __JSON_Deserialize<T>(values: Map<string, string>): T {
66
+ ${
67
+ process.argv.includes("--debugJSON")
68
+ ? this.checkDecodeStmts.join("else")
69
+ : ""
70
+ }
71
+ return {
72
+ ${
73
+ // @ts-ignore
74
+ this.decodeStmts.join("")
75
+ }
76
+ }
63
77
  }
64
- }
78
+ `;*/
79
+ const setKeyFunc = `
80
+ __JSON_Set_Key(key: string, value: string): void {
81
+ ${
82
+ // @ts-ignore
83
+ this.setDataStmts.join("")}
84
+ throw new Error("Cannot find key: " + key);
85
+ }
65
86
  `;
87
+ //console.log(setKeyFunc, deserializeFunc, serializeFunc)
66
88
  this.encodeStmts = [];
67
- this.decodeStmts = [];
89
+ //this.decodeStmts = [];
90
+ this.setDataStmts = [];
91
+ //this.checkDecodeStmts = [];
68
92
  const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
69
93
  node.members.push(serializedProperty);
70
94
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
71
95
  node.members.push(serializeMethod);
72
- const deserializeMethod = SimpleParser.parseClassMember(deserializeFunc, node);
73
- node.members.push(deserializeMethod);
96
+ //const deserializeMethod = SimpleParser.parseClassMember(
97
+ // deserializeFunc,
98
+ // node
99
+ //);
100
+ //node.members.push(deserializeMethod);
101
+ const setDataMethod = SimpleParser.parseClassMember(setKeyFunc, node);
102
+ node.members.push(setDataMethod);
74
103
  }
75
104
  get name() {
76
105
  return "json";
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -14,8 +14,9 @@ class AsJSONTransform extends ClassDecorator {
14
14
  public currentClass!: ClassDeclaration;
15
15
  public sources: Source[] = [];
16
16
  public encodeStmts: string[] = [];
17
- public decodeStmts: string[] = [];
18
- public checkDecodeStmts: string[] = [];
17
+ //public decodeStmts: string[] = [];
18
+ public setDataStmts: string[] = [];
19
+ //public checkDecodeStmts: string[] = [];
19
20
 
20
21
  visitMethodDeclaration(): void {}
21
22
  visitFieldDeclaration(node: FieldDeclaration): void {
@@ -33,14 +34,23 @@ class AsJSONTransform extends ClassDecorator {
33
34
  );
34
35
 
35
36
  // @ts-ignore
36
- this.decodeStmts.push(
37
- `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
38
- );
37
+ //this.decodeStmts.push(
38
+ // `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
39
+ //);
39
40
 
40
41
  // @ts-ignore
41
- this.checkDecodeStmts.push(
42
- ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
42
+ this.setDataStmts.push(
43
+ `if (key.length === ${name.length} && (memory.compare(changetype<usize>("${name}"), changetype<usize>(key), ${name.length}) == 0)) {
44
+ this.${name} = JSON.parseObjectValue<${type}>(value);
45
+ return;
46
+ }
47
+ `
43
48
  );
49
+
50
+ // @ts-ignore
51
+ //this.checkDecodeStmts.push(
52
+ // ' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n'
53
+ //);
44
54
  }
45
55
  visitClassDeclaration(node: ClassDeclaration): void {
46
56
  if (!node.members) {
@@ -62,21 +72,18 @@ class AsJSONTransform extends ClassDecorator {
62
72
  stmt.length - 1
63
73
  );
64
74
  serializeFunc = `
65
- @inline
66
75
  __JSON_Serialize(): string {
67
76
  return \`{${this.encodeStmts.join("")}}\`;
68
77
  }
69
78
  `;
70
79
  } else {
71
80
  serializeFunc = `
72
- @inline
73
81
  __JSON_Serialize(): string {
74
82
  return "{}";
75
83
  }
76
84
  `;
77
85
  }
78
- const deserializeFunc = `
79
- @inline
86
+ /*const deserializeFunc = `
80
87
  __JSON_Deserialize<T>(values: Map<string, string>): T {
81
88
  ${
82
89
  process.argv.includes("--debugJSON")
@@ -90,9 +97,21 @@ class AsJSONTransform extends ClassDecorator {
90
97
  }
91
98
  }
92
99
  }
93
- `;
100
+ `;*/
101
+ const setKeyFunc = `
102
+ __JSON_Set_Key(key: string, value: string): void {
103
+ ${
104
+ // @ts-ignore
105
+ this.setDataStmts.join("")
106
+ }
107
+ throw new Error("Cannot find key: " + key);
108
+ }
109
+ `
110
+ //console.log(setKeyFunc, deserializeFunc, serializeFunc)
94
111
  this.encodeStmts = [];
95
- this.decodeStmts = [];
112
+ //this.decodeStmts = [];
113
+ this.setDataStmts = [];
114
+ //this.checkDecodeStmts = [];
96
115
  const serializedProperty = SimpleParser.parseClassMember(
97
116
  serializedProp,
98
117
  node
@@ -102,11 +121,16 @@ class AsJSONTransform extends ClassDecorator {
102
121
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
103
122
  node.members.push(serializeMethod);
104
123
 
105
- const deserializeMethod = SimpleParser.parseClassMember(
106
- deserializeFunc,
124
+ //const deserializeMethod = SimpleParser.parseClassMember(
125
+ // deserializeFunc,
126
+ // node
127
+ //);
128
+ //node.members.push(deserializeMethod);
129
+ const setDataMethod = SimpleParser.parseClassMember(
130
+ setKeyFunc,
107
131
  node
108
132
  );
109
- node.members.push(deserializeMethod);
133
+ node.members.push(setDataMethod);
110
134
  }
111
135
  get name(): string {
112
136
  return "json";