json-as 0.4.9 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
2
- import { getName } from "visitor-as/dist/utils.js";
2
+ import { getName, toString } from "visitor-as/dist/utils.js";
3
3
  import { SimpleParser } from "visitor-as/dist/index.js";
4
4
  class AsJSONTransform extends ClassDecorator {
5
5
  constructor() {
@@ -7,28 +7,33 @@ class AsJSONTransform extends ClassDecorator {
7
7
  this.sources = [];
8
8
  this.encodeStmts = [];
9
9
  this.decodeStmts = [];
10
+ this.checkDecodeStmts = [];
10
11
  }
11
12
  visitMethodDeclaration() { }
12
13
  visitFieldDeclaration(node) {
14
+ const lineText = toString(node);
15
+ if (lineText.startsWith("private"))
16
+ return;
13
17
  const name = getName(node);
14
18
  if (!node.type) {
15
19
  throw new Error(`Field ${name} is missing a type declaration`);
16
20
  }
17
- const type = getName(node.type);
21
+ let type = getName(node.type);
18
22
  // @ts-ignore
19
23
  this.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
20
24
  // @ts-ignore
21
25
  this.decodeStmts.push(`${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`);
26
+ // @ts-ignore
27
+ this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n');
22
28
  }
23
29
  visitClassDeclaration(node) {
24
30
  if (!node.members) {
25
31
  return;
26
32
  }
27
33
  this.currentClass = node;
28
- const name = getName(node);
29
34
  this.visit(node.members);
30
- const serializedProp = `__JSON_Serialized: string = "";`;
31
- let serializeFunc = ``;
35
+ const serializedProp = '__JSON_Serialized: string = "";';
36
+ let serializeFunc = "";
32
37
  if (this.encodeStmts.length > 0) {
33
38
  const stmt = this.encodeStmts[this.encodeStmts.length - 1];
34
39
  this.encodeStmts[this.encodeStmts.length - 1] = stmt.slice(0, stmt.length - 1);
@@ -49,7 +54,8 @@ class AsJSONTransform extends ClassDecorator {
49
54
  }
50
55
  const deserializeFunc = `
51
56
  @inline
52
- __JSON_Deserialize(values: Map<string, string>): ${name} {
57
+ __JSON_Deserialize<T>(values: Map<string, string>): T {
58
+ ${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""}
53
59
  return {
54
60
  ${
55
61
  // @ts-ignore
@@ -59,7 +65,6 @@ class AsJSONTransform extends ClassDecorator {
59
65
  `;
60
66
  this.encodeStmts = [];
61
67
  this.decodeStmts = [];
62
- //console.log(serializeFunc, deserializeFunc)
63
68
  const serializedProperty = SimpleParser.parseClassMember(serializedProp, node);
64
69
  node.members.push(serializedProperty);
65
70
  const serializeMethod = SimpleParser.parseClassMember(serializeFunc, node);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.4.9",
3
+ "version": "0.5.1",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -7,7 +7,7 @@ import {
7
7
  ClassDecorator,
8
8
  registerDecorator,
9
9
  } from "visitor-as/dist/decorator.js";
10
- import { getName } from "visitor-as/dist/utils.js";
10
+ import { getName, toString } from "visitor-as/dist/utils.js";
11
11
  import { SimpleParser } from "visitor-as/dist/index.js";
12
12
 
13
13
  class AsJSONTransform extends ClassDecorator {
@@ -15,16 +15,18 @@ class AsJSONTransform extends ClassDecorator {
15
15
  public sources: Source[] = [];
16
16
  public encodeStmts: string[] = [];
17
17
  public decodeStmts: string[] = [];
18
+ public checkDecodeStmts: string[] = [];
18
19
 
19
20
  visitMethodDeclaration(): void {}
20
21
  visitFieldDeclaration(node: FieldDeclaration): void {
22
+ const lineText = toString(node);
23
+ if (lineText.startsWith("private")) return;
21
24
  const name = getName(node);
22
25
  if (!node.type) {
23
26
  throw new Error(`Field ${name} is missing a type declaration`);
24
27
  }
25
28
 
26
- const type = getName(node.type);
27
-
29
+ let type = getName(node.type);
28
30
  // @ts-ignore
29
31
  this.encodeStmts.push(
30
32
  `"${name}":\${JSON.stringify<${type}>(this.${name})},`
@@ -34,6 +36,9 @@ class AsJSONTransform extends ClassDecorator {
34
36
  this.decodeStmts.push(
35
37
  `${name}: JSON.parseObjectValue<${type}>(values.get("${name}")),\n`
36
38
  );
39
+
40
+ // @ts-ignore
41
+ this.checkDecodeStmts.push(' if (!values.has("${name}")) throw new Error("Key "${name}" was not found. Cannot instantiate object.");\n')
37
42
  }
38
43
  visitClassDeclaration(node: ClassDeclaration): void {
39
44
  if (!node.members) {
@@ -42,13 +47,11 @@ class AsJSONTransform extends ClassDecorator {
42
47
 
43
48
  this.currentClass = node;
44
49
 
45
- const name = getName(node);
46
-
47
50
  this.visit(node.members);
48
51
 
49
- const serializedProp = `__JSON_Serialized: string = "";`;
52
+ const serializedProp = '__JSON_Serialized: string = "";';
50
53
 
51
- let serializeFunc = ``;
54
+ let serializeFunc = "";
52
55
 
53
56
  if (this.encodeStmts.length > 0) {
54
57
  const stmt = this.encodeStmts[this.encodeStmts.length - 1]!;
@@ -70,21 +73,20 @@ class AsJSONTransform extends ClassDecorator {
70
73
  }
71
74
  `;
72
75
  }
73
-
74
76
  const deserializeFunc = `
75
77
  @inline
76
- __JSON_Deserialize(values: Map<string, string>): ${name} {
78
+ __JSON_Deserialize<T>(values: Map<string, string>): T {
79
+ ${process.argv.includes("--debugJSON") ? this.checkDecodeStmts.join("else") : ""}
77
80
  return {
78
81
  ${
79
- // @ts-ignore
80
- this.decodeStmts.join("")
82
+ // @ts-ignore
83
+ this.decodeStmts.join("")
81
84
  }
82
85
  }
83
86
  }
84
87
  `;
85
88
  this.encodeStmts = [];
86
89
  this.decodeStmts = [];
87
- //console.log(serializeFunc, deserializeFunc)
88
90
  const serializedProperty = SimpleParser.parseClassMember(
89
91
  serializedProp,
90
92
  node
File without changes