Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for "pure object references" #178

Open
emilianodenis opened this issue Apr 8, 2021 · 1 comment
Open

Support for "pure object references" #178

emilianodenis opened this issue Apr 8, 2021 · 1 comment

Comments

@emilianodenis
Copy link

Hi,

A nice to have would be a support for pure JSON references.

I mean objects that are simply { $ref: 1 }.

An example:

@jsonObject
export class A {
  constructor() {}

  @jsonMember
  public name: string;

  @jsonMember
  public id: number;
}

@jsonObject
export class B {
  constructor() {}

  @jsonMember
  public prop1: A;

  @jsonMember
  public prop2: A;
}

    const obj: any = {
      $id: 1,
      prop1: {
        $id: 2,
        name: "test1",
        id: 100
      },
      prop2: {
        $id: 3,
        name: "test2",
        id: 101
      }
    };

    const objRef: any = {
      $id: 4,
      prop1: {
        $id: 5,
        name: "test1",
        id: 100
      },
      prop2: { $ref: 5 }
    };

    const res1 = TypedJSON.parse(obj, B);
    const res2 = TypedJSON.parse(objRef, B);

In this case, res1 is properly serialized. But for res2, prop1 is fine while prop2 is an empty object.

Stackblitz with a more complete example: https://stackblitz.com/edit/angular-ivy-yoqexv

@sumbricht
Copy link
Contributor

For anyone blocked by TypedJSON issues: my own software heavily relied on TypedJSON and had to apply numerous hacks for issues in TypedJSON that are not getting fixed. I loved TypedJSON but unfortunately had to move on as there has not been a single fix in 4 years now.

Therefore I created the similarly powerful library json-class-serializer heavily inspired by TypedJSON, which addresses all my issues (and hopefully yours). It's pretty much a drop-in replacement if you don't rely on reflect-metadata, which I'm not supporting (as this was the cause of most of my issues). If you rely on reflect-metadata, just specify the constructors of properties other than string/number/boolean and then it most likely just works.

Please feel free to give it a try if you feel the urge to move away from TypedJSON but don't want to do any heavy re-engineering. And please don't hate on me for the advertisement here as I have contributed to TypedJSON as well and opened many still-open issues :-).

You can find my library at @sumbricht/json-class-serializer and gladly open issues there if you feel something's missing / not working correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants