JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
79 stars 15 forks source link

Classes which extend are missing parent encode statements #40

Closed derekbar90 closed 1 year ago

derekbar90 commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

When extending a class the parent class fields are not added to the decode statements (just encode).

Here is the diff that solved my problem:

diff --git a/node_modules/json-as/transform/lib/index.js b/node_modules/json-as/transform/lib/index.js
index 12837b0..e151df8 100755
--- a/node_modules/json-as/transform/lib/index.js
+++ b/node_modules/json-as/transform/lib/index.js
@@ -85,7 +85,9 @@ class AsJSONTransform extends BaseVisitor {
                 console.error("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?");
             }
         }
-        this.visit(node.members);
+        const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent);
+        const members = [...node.members, ...(parentSchema ? parentSchema.node.members : [])]
+        this.visit(members);
         let serializeFunc = "";
         if (this.currentClass.encodeStmts.length > 0) {
             const stmt = this.currentClass.encodeStmts[this.currentClass.encodeStmts.length - 1];