JairusSW / as-json

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

Bug: Classes derived from base classes do not serialize correctly #71

Closed mattjohnsonpint closed 5 months ago

mattjohnsonpint commented 5 months ago

json-as version 0.8.3 has a bug. Consider:

@json
class BaseObject {
  a: string;

  constructor(a: string) {
    this.a = a;
  }
}

@json
class DerivedObject extends BaseObject {
  b: string;

  constructor(a: string, b: string) {
    super(a);
    this.b = b;
  }
}

describe("Ser/de object hierarchies", () => {
  it("should ser/de objects derived from base objects", () => {
    const o = new DerivedObject("1", "2");
    const s = '{"a":"1","b":"2"}';
    canSerde(o, s);
  });
});

The test fails with:

[Actual]  : "{\"a\":\"1\",\"b\":\"2\",\"a\":\"1\"}"
[Expected]: "{\"a\":\"1\",\"b\":\"2\"}"

Workarounds attempted:

Removing @json on only the derived object gives:

[Actual]  : "\"\\\""
[Expected]: "{\"a\":\"1\",\"b\":\"2\"}"

Removing @json on only the base object gives a compile error:

FAILURE Class DerivedObject extends its parent class BaseObject, but BaseObject does not include a @json or @serializable decorator! Add the decorator and rebuild.

I would expect either to give a compiler error, actually.

JairusSW commented 5 months ago

I'll push out a new release of the transform to implement this. Thanks, Matt!

JairusSW commented 5 months ago

Transform getting pretty nasty looking. I'll refactor it later