json-as 0.8.3 → 0.8.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/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
1
  v0.8.2 - Properties starting with `static` or `private` would be ignored
2
- v0.8.3 - Dirty fix to issue #68. Add __JSON_Stringify callable to global scope.
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.
@@ -14,7 +14,32 @@ function canSer<T>(data: T, toBe: string): void {
14
14
  const serialized = JSON.stringify<T>(data);
15
15
  expect(serialized).toBe(toBe);
16
16
  }
17
+ @json
18
+ class BaseObject {
19
+ a: string;
20
+
21
+ constructor(a: string) {
22
+ this.a = a;
23
+ }
24
+ }
25
+
26
+ @json
27
+ class DerivedObject extends BaseObject {
28
+ b: string;
29
+
30
+ constructor(a: string, b: string) {
31
+ super(a);
32
+ this.b = b;
33
+ }
34
+ }
17
35
 
36
+ describe("Ser/de object hierarchies", () => {
37
+ it("should ser/de objects derived from base objects", () => {
38
+ const o = new DerivedObject("1", "2");
39
+ const s = '{"a":"1","b":"2"}';
40
+ canSerde(o, s);
41
+ });
42
+ });
18
43
  @json
19
44
  class Map4 {
20
45
  a: string;
@@ -126,11 +126,11 @@ export namespace JSON {
126
126
  result.write(serializeString(unchecked(keys[end]).toString()));
127
127
  result.writeCodePoint(colonCode);
128
128
  result.write(__JSON_Stringify(unchecked(values[end])));
129
-
129
+
130
130
  result.writeCodePoint(rightBraceCode);
131
131
  return result.toString();
132
132
  } else {
133
- throw new Error(
133
+ throw abort(
134
134
  `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
135
135
  );
136
136
  }
@@ -211,7 +211,7 @@ export namespace JSON {
211
211
  return;
212
212
  }
213
213
  } else {
214
- throw new Error(
214
+ throw abort(
215
215
  `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
216
216
  );
217
217
  }
@@ -251,7 +251,7 @@ export namespace JSON {
251
251
  // @ts-ignore
252
252
  return parseDate(data);
253
253
  } else {
254
- throw new Error(
254
+ throw abort(
255
255
  `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
256
256
  );
257
257
  }
@@ -284,7 +284,7 @@ export namespace JSON {
284
284
  // @ts-ignore
285
285
  return parseDate(data);
286
286
  } else {
287
- throw new Error(
287
+ throw abort(
288
288
  `Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
289
289
  );
290
290
  }
@@ -411,7 +411,7 @@ export namespace JSON {
411
411
  break;
412
412
  }
413
413
  default: {
414
- throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
414
+ throw abort(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
415
415
  }
416
416
  }
417
417
  }
@@ -425,7 +425,7 @@ export namespace JSON {
425
425
  @inline function parseBoolean<T extends boolean>(data: string): T {
426
426
  if (data.length > 3 && data.startsWith(trueWord)) return <T>true;
427
427
  else if (data.length > 4 && data.startsWith(falseWord)) return <T>false;
428
- else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
428
+ else throw abort(`JSON: Cannot parse "${data}" as boolean`);
429
429
  }
430
430
 
431
431
  // @ts-ignore: Decorator
@@ -584,7 +584,7 @@ export namespace JSON {
584
584
  );
585
585
 
586
586
  if (!isDefined(map.set)) {
587
- return unreachable();
587
+ throw abort("Tried to parse a map, but the types did not match!")
588
588
  }
589
589
 
590
590
  const key = Virtual.createEmpty<string>();
@@ -729,7 +729,7 @@ export namespace JSON {
729
729
  return parseNumber<T>(k);
730
730
  }
731
731
 
732
- throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
732
+ throw abort(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
733
733
  }
734
734
 
735
735
  // @ts-ignore: Decorator
@@ -758,7 +758,7 @@ export namespace JSON {
758
758
  }
759
759
  }
760
760
 
761
- return unreachable();
761
+ throw abort("Tried to parse array, but failed!")
762
762
  }
763
763
 
764
764
  // @ts-ignore: Decorator
@@ -892,7 +892,7 @@ function parseDate(dateTimeString: string): Date {
892
892
  }
893
893
 
894
894
  // Dirty fix
895
- // @ts-ignore: Decorator
895
+ // @ts-ignore: Decorator
896
896
  @global @inline function __JSON_Stringify<T>(data: T): string {
897
897
  // String
898
898
  if (isString<T>() && data != null) {
@@ -963,11 +963,11 @@ function parseDate(dateTimeString: string): Date {
963
963
  result.write(serializeString(unchecked(keys[end]).toString()));
964
964
  result.writeCodePoint(colonCode);
965
965
  result.write(__JSON_Stringify(unchecked(values[end])));
966
-
966
+
967
967
  result.writeCodePoint(rightBraceCode);
968
968
  return result.toString();
969
969
  } else {
970
- throw new Error(
970
+ throw abort(
971
971
  `Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
972
972
  );
973
973
  }
package/assembly/test.ts CHANGED
@@ -1,7 +1,24 @@
1
1
  import { JSON } from "./src/json";
2
2
 
3
- console.log(JSON.stringify(JSON.parse<f64[]>(`[
4
- 1,
5
- 2,
6
- 3
7
- ]`)));
3
+ @json
4
+ class BaseObject {
5
+ a: string;
6
+
7
+ constructor(a: string) {
8
+ this.a = a;
9
+ }
10
+ }
11
+
12
+ @json
13
+ class DerivedObject extends BaseObject {
14
+ b: string;
15
+
16
+ constructor(a: string, b: string) {
17
+ super(a);
18
+ this.b = b;
19
+ }
20
+ }
21
+
22
+ const o = new DerivedObject("1", "2");
23
+
24
+ console.log(JSON.stringify(o))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.8.3",
3
+ "version": "0.8.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -53,18 +53,27 @@ class AsJSONTransform extends BaseVisitor {
53
53
  setDataStmts: [],
54
54
  initializeStmts: []
55
55
  };
56
- if (this.currentClass.parent.length > 0) {
56
+ if (this.currentClass.parent.length) {
57
57
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
58
58
  if (parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts) {
59
59
  parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.push((parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.pop()) + ",");
60
- this.currentClass.encodeStmts.push(...parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts);
60
+ for (let i = 0; i < parentSchema.keys.length; i++) {
61
+ const key = parentSchema.keys[i];
62
+ if (node.members.filter(v => v.name.text == key) == undefined)
63
+ this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]);
64
+ }
61
65
  }
62
66
  }
63
67
  const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
64
68
  const members = [
65
- ...node.members,
66
- ...(parentSchema ? parentSchema.node.members : []),
69
+ ...node.members
67
70
  ];
71
+ if (parentSchema) {
72
+ for (const mem of parentSchema.node.members) {
73
+ if (members.find(v => v.name === mem.name) == undefined)
74
+ members.unshift(mem);
75
+ }
76
+ }
68
77
  for (const mem of members) {
69
78
  // @ts-ignore
70
79
  if (mem.type && mem.type.name && mem.type.name.identifier.text) {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.8.3",
3
+ "version": "0.8.4",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -61,24 +61,33 @@ class AsJSONTransform extends BaseVisitor {
61
61
  initializeStmts: []
62
62
  };
63
63
 
64
- if (this.currentClass.parent.length > 0) {
64
+ if (this.currentClass.parent.length) {
65
65
  const parentSchema = this.schemasList.find(
66
66
  (v) => v.name == this.currentClass.parent
67
67
  );
68
68
  if (parentSchema?.encodeStmts) {
69
69
  parentSchema?.encodeStmts.push(parentSchema?.encodeStmts.pop() + ",");
70
- this.currentClass.encodeStmts.push(...parentSchema?.encodeStmts);
70
+ for (let i = 0; i < parentSchema.keys.length; i++) {
71
+ const key = parentSchema.keys[i];
72
+ if (node.members.filter(v => v.name.text == key) == undefined) this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]!);
73
+ }
71
74
  }
72
75
  }
73
76
 
74
77
  const parentSchema = this.schemasList.find(
75
78
  (v) => v.name == this.currentClass.parent
76
79
  );
80
+
77
81
  const members = [
78
- ...node.members,
79
- ...(parentSchema ? parentSchema.node.members : []),
82
+ ...node.members
80
83
  ];
81
84
 
85
+ if (parentSchema) {
86
+ for (const mem of parentSchema.node.members) {
87
+ if (members.find(v => v.name === mem.name) == undefined) members.unshift(mem);
88
+ }
89
+ }
90
+
82
91
  for (const mem of members) {
83
92
  // @ts-ignore
84
93
  if (mem.type && mem.type.name && mem.type.name.identifier.text) {