typescript-json-serializer icon indicating copy to clipboard operation
typescript-json-serializer copied to clipboard

[BUG]: @JsonProperty decorator is not taking into account when serializing a generic class

Open jossErnesto opened this issue 2 years ago • 2 comments

Version

4.0.1

Description

I am using "typescript-json-serializer": "^4.3.0" and I want to serialize a generic class. It seems to me that in that case the @JsonProperty decorator is not taking into account.

Error

I got 
{"value":{"_name":"Item A","_version":1}}
It should be:
{"value":{"name":"Item A","version":1}}

Reproduction

@JsonObject()
class Value<R> {
  @JsonProperty()
  value: R;
  constructor(value: R) {
    this.value = value;
  }
}

@JsonObject()
export class Item {
  @JsonProperty({ name: 'name' })
  private _name: string;

  @JsonProperty({ name: 'version' })
  private _version: number;

  constructor(name: string, version: number) {
    this._name = name;
    this._version = version;
  }

  public get name(): string {
    return this._name;
  }

  public set name(value: string) {
    this._name = value;
  }

  public get version(): number {
    return this._version;
  }

  public set version(value: number) {
    this._version = value;
  }
}

describe('serialize generic class', () => {
  it('serialize simple generic class', () => {
    const mapper = new JsonSerializer();
    const value = new Value<Item>(new Item('Item A', 1));
    const data = mapper.serialize(value);
    console.log(JSON.stringify(data));
  });
});

On which OS the bug appears?

Windows 11

What is your project type?

nodeJs

On which build mode the bug appears?

both

Anything else?

No response

jossErnesto avatar Aug 31 '22 11:08 jossErnesto

Hi! Thank you for using my lib!

In your example you didnt decorate with @JsonObject the class Item. Is it a mistake in your example?

GillianPerard avatar Sep 04 '22 05:09 GillianPerard

Hello, it was an mistake when copying and pasting from my editor. So, even if you use @JsonObject() in class Item, @JsonProperty does not work.

jossErnesto avatar Sep 05 '22 12:09 jossErnesto

After some tests, I can't manage generics. Even with reflection, javascript cannot determine which Class fits the json...

GillianPerard avatar Oct 07 '22 06:10 GillianPerard