[FEAT]: deserialize map of arrays
Nikita2k opened this issue · 2 comments
Nikita2k commented
Description
Hey!
I'm looking for a way to do the following, but it seems there is no way to do so. Consider data model:
@JsonObject()
export class Animal {
@JsonProperty()
breed: string;
}
@JsonObject()
export class Human {
@JsonProperty()
name: string;
@JsonProperty()
age: number;
}
@JsonObject()
export class SomeMapping {
@JsonProperty()
mapping: Map<string, Human[] | Animal[]>
}
and Json:
{
"1": [{
"breed": "foo"
}],
"2": [{
"name": "John",
"age": 42
}, {
"name": "Mike",
"age": 16
}]
}
Is there a way to correctly deserialize that map? If not, would be great to have that feature
Proposed solution
No response
GillianPerard commented
Hi, it seems there is no way today to deserialize this kind of structure.
I will see what I can do to manage this case; in my mind, you'll write something like:
@JsonObject()
export class Animal {
@JsonProperty()
breed: string;
}
@JsonObject()
export class Human {
@JsonProperty()
name: string;
@JsonProperty()
age: number;
}
@JsonObject()
export class SomeMapping {
@JsonProperty({ type: property => property?.breed !== undefined ? Animal : Human })
mapping: Map<string, Human[] | Animal[]>
}
const data = {
mapping: {
"1": [{
"breed": "foo"
}],
"2": [{
"name": "John",
"age": 42
}, {
"name": "Mike",
"age": 16
}]
}
}
const serializer = new JsonSerializer();
serializer.deserializeObject(data, Something);
GillianPerard commented
I just published the new 6.0.1 version, you can try it according to the example I sent before.