json-as 0.6.6 → 0.7.0

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
@@ -38,6 +38,7 @@ class Vec3 {
38
38
 
39
39
  @json
40
40
  class Player {
41
+ @alias("first name")
41
42
  firstName!: string;
42
43
  lastName!: string;
43
44
  lastActive!: i32[];
package/assembly/test.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { JSON } from "./src/json";
2
2
 
3
3
  // @ts-ignore
4
-
4
+ @serializable
5
5
  class Vec3 {
6
6
  x: f64 = 3.4;
7
7
  y: f64 = 1.2;
@@ -9,8 +9,9 @@ class Vec3 {
9
9
  }
10
10
 
11
11
  // @ts-ignore
12
- @json
12
+ @serializable
13
13
  class Player extends Vec3 {
14
+ @alias("first name")
14
15
  firstName: string;
15
16
  lastName: string;
16
17
  lastActive: Date;
@@ -47,7 +48,7 @@ console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));
47
48
 
48
49
  console.log("Original: " + JSON.stringify(player));
49
50
  //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
50
- console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));
51
+ console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"first name":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true,"x":5","y":4","z":3}')));
51
52
 
52
53
  @serializable
53
54
  class Wrapper<T> {
@@ -56,6 +57,7 @@ class Wrapper<T> {
56
57
 
57
58
  @serializable
58
59
  class Foo {
60
+ @alias("ur mom")
59
61
  foo!: string;
60
62
  }
61
63
 
@@ -70,7 +72,7 @@ const foo: Wrapper<Foo> = {
70
72
 
71
73
  foo.data.foo = "ha";
72
74
  console.log(JSON.stringify(foo));
73
- console.log(JSON.stringify(JSON.parse<Wrapper<Foo>>("{\"data\":{\"foo\":\"ha\"}}")))
75
+ console.log(JSON.stringify(JSON.parse<Wrapper<Foo>>("{\"data\":{\"ur mom\":\"ha\"}}")))
74
76
  /*
75
77
  // 9,325,755
76
78
  bench("Stringify Object (Vec3)", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.6.6",
3
+ "version": "0.7.0",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -21,7 +21,7 @@ class AsJSONTransform extends BaseVisitor {
21
21
  }
22
22
  visitMethodDeclaration() { }
23
23
  visitClassDeclaration(node) {
24
- var _c;
24
+ var _c, _d;
25
25
  const className = node.name.text;
26
26
  if (!((_c = node.decorators) === null || _c === void 0 ? void 0 : _c.length))
27
27
  return;
@@ -76,6 +76,12 @@ class AsJSONTransform extends BaseVisitor {
76
76
  // @ts-ignore
77
77
  let type = toString(member.type);
78
78
  const name = member.name.text;
79
+ let aliasName = name;
80
+ if (member.decorators && ((_d = member.decorators[0]) === null || _d === void 0 ? void 0 : _d.name.text) === "alias") {
81
+ if (member.decorators[0] && member.decorators[0].args[0]) {
82
+ aliasName = member.decorators[0].args[0].value;
83
+ }
84
+ }
79
85
  this.currentClass.keys.push(name);
80
86
  // @ts-ignore
81
87
  this.currentClass.types.push(type);
@@ -90,9 +96,9 @@ class AsJSONTransform extends BaseVisitor {
90
96
  "u64",
91
97
  "i64",
92
98
  ].includes(type.toLowerCase())) {
93
- this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
99
+ this.currentClass.encodeStmts.push(`"${aliasName}":\${this.${name}.toString()},`);
94
100
  // @ts-ignore
95
- this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
101
+ this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
96
102
  this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
97
103
  return;
98
104
  }
@@ -106,9 +112,9 @@ class AsJSONTransform extends BaseVisitor {
106
112
  "f32",
107
113
  "f64",
108
114
  ].includes(type.toLowerCase())) {
109
- this.currentClass.encodeStmts.push(`"${name}":\${this.${name}.toString()},`);
115
+ this.currentClass.encodeStmts.push(`"${aliasName}":\${this.${name}.toString()},`);
110
116
  // @ts-ignore
111
- this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
117
+ this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
112
118
  this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
113
119
  return;
114
120
  }
@@ -118,9 +124,9 @@ class AsJSONTransform extends BaseVisitor {
118
124
  }
119
125
  }
120
126
  else {
121
- this.currentClass.encodeStmts.push(`"${name}":\${JSON.stringify<${type}>(this.${name})},`);
127
+ this.currentClass.encodeStmts.push(`"${aliasName}":\${JSON.stringify<${type}>(this.${name})},`);
122
128
  // @ts-ignore
123
- this.currentClass.setDataStmts.push(`if (key.equals("${name}")) {
129
+ this.currentClass.setDataStmts.push(`if (key.equals("${aliasName}")) {
124
130
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
125
131
  return;
126
132
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.6.6",
3
+ "version": "0.7.0",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -88,6 +88,12 @@ class AsJSONTransform extends BaseVisitor {
88
88
  let type = toString(member.type);
89
89
 
90
90
  const name = member.name.text;
91
+ let aliasName = name;
92
+ if (member.decorators && member.decorators[0]?.name.text === "alias") {
93
+ if (member.decorators[0] && member.decorators[0].args![0]) {
94
+ aliasName = member.decorators[0].args![0].value;
95
+ }
96
+ }
91
97
  this.currentClass.keys.push(name);
92
98
  // @ts-ignore
93
99
  this.currentClass.types.push(type);
@@ -105,11 +111,11 @@ class AsJSONTransform extends BaseVisitor {
105
111
  ].includes(type.toLowerCase())
106
112
  ) {
107
113
  this.currentClass.encodeStmts.push(
108
- `"${name}":\${this.${name}.toString()},`
114
+ `"${aliasName}":\${this.${name}.toString()},`
109
115
  );
110
116
  // @ts-ignore
111
117
  this.currentClass.setDataStmts.push(
112
- `if (key.equals("${name}")) {
118
+ `if (key.equals("${aliasName}")) {
113
119
  this.${name} = __atoi_fast<${type}>(data, val_start << 1, val_end << 1);
114
120
  return;
115
121
  }
@@ -128,11 +134,11 @@ class AsJSONTransform extends BaseVisitor {
128
134
  ].includes(type.toLowerCase())
129
135
  ) {
130
136
  this.currentClass.encodeStmts.push(
131
- `"${name}":\${this.${name}.toString()},`
137
+ `"${aliasName}":\${this.${name}.toString()},`
132
138
  );
133
139
  // @ts-ignore
134
140
  this.currentClass.setDataStmts.push(
135
- `if (key.equals("${name}")) {
141
+ `if (key.equals("${aliasName}")) {
136
142
  this.${name} = __parseObjectValue<${type}>(data.slice(val_start, val_end), initializeDefaultValues);
137
143
  return;
138
144
  }
@@ -145,11 +151,11 @@ class AsJSONTransform extends BaseVisitor {
145
151
  }
146
152
  } else {
147
153
  this.currentClass.encodeStmts.push(
148
- `"${name}":\${JSON.stringify<${type}>(this.${name})},`
154
+ `"${aliasName}":\${JSON.stringify<${type}>(this.${name})},`
149
155
  );
150
156
  // @ts-ignore
151
157
  this.currentClass.setDataStmts.push(
152
- `if (key.equals("${name}")) {
158
+ `if (key.equals("${aliasName}")) {
153
159
  this.${name} = __parseObjectValue<${type}>(val_start ? data.slice(val_start, val_end) : data, initializeDefaultValues);
154
160
  return;
155
161
  }