I have a class, for example, let's say a Car with the below structure.
@Serializable()
export default class Car {
@JsonProperty({
name: 'id'
})
private id!: string;
@JsonProperty({
name: 'name'
})
private name!: string;
@JsonProperty({
name: 'carOwner'
})
private carOwner!: Owner;
}
Further I have the Owner class.
@Serializable()
export default class Owner{
@JsonProperty({
name: 'id'
})
private id!: string;
@JsonProperty({
name: 'name'
})
private name!: string;
@JsonProperty({
name: 'address'
})
private address!: string;
}
I have an incoming JSON Object with below structure, let's call it newCar
{
"id": "test-id",
"name": "test-car",
"carOwner": {
"id": "owner-id",
"name": "owner-name",
"address": "owner-address",
"gender": "owner-gender",
"age": "owner-age",
}
}
What I want to do is, deserialize the newCar JSON to the Car class. That is when I deserialize newJson I should get the below object, where gender and age are filtered out.
{
"id": "test-id",
"name": "test-car",
"carOwner": {
"id": "owner-id",
"name": "owner-name",
"address": "owner-address"
}
}
But right now what I am getting is the original newJson. It seems that the library (typescript-json-serializer) which I am using does not deserialize nested objects, here which is owner.
Any input will be appreciated.
Here is the link to the package typescript-json-serializer
Serializable?Serializable. Apologies that I didn't mention it in code. I'll edit.Carclass as second argument?typescriptJsonSerializer.deserialize(newCar, Car);Ownerinside theJsonProperty()forownerinCarclass.