typescript-json-serializer
typescript-json-serializer copied to clipboard
[BUG]: @JsonProperty decorator is not taking into account when serializing a generic class
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
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?
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.
After some tests, I can't manage generics. Even with reflection, javascript cannot determine which Class fits the json...