Skip to content
Joe Eagar edited this page May 29, 2026 · 2 revisions

⚠️ DEPRECATED — This wiki is no longer maintained. The current documentation lives in /documentation. See the equivalent page: documentation/JSON.md.

STRUCT can save in JSON in addition to binary.

= Example =


class AbstractClass {
    constructor() {
        this.value = 1;
    }
}
AbstractClass.STRUCT = `
AbstractClass {
    value : int;
}
`;
nstructjs.register(AbstractClass);

class A extends AbstractClass {
}
A.STRUCT = nstructjs.inherit(A, AbstractClass) + "}";
nstructjs.register(A);

class B extends AbstractClass {
}
B.STRUCT = nstructjs.inherit(B, AbstractClass) + "}";
nstructjs.register(B);

class C extends AbstractClass {
}
C.STRUCT = nstructjs.inherit(C, AbstractClass) + "}";
nstructjs.register(C);

class Test {
    constructor() {
        this.test = new C();
    }
}
Test.STRUCT = `
Test {
    test : abstract(AbstractClass, "type");
}
`;
nstructjs.register(Test);

Note the "type" parameter in the abstract keyword, it controls which field in the JSON object will store the object type.

To save, use nstructjs.writeJSON:

nstructjs.writeJSON(new Test());

It will produce the JSON:

{
  "test": {
    "value": 1,
    "type": "C"
  }
}

Clone this wiki locally