sebastiandaschner blog
JSON mapping polymorphism support
saturday, march 19, 2016JSR 367 or JSONB is currently developed to enable Java object to JSON mapping. When deserializing JSON it may be neccessary in some situations to determine polymorph type information from the serialized JSON.
Consider the following example:
public class Test {
private List<Customer> customers;
}
public class Customer {
private String name;
}
public class VipCustomer {
// same properties, overridden methods
}
The serialized JSON may look like follows — note that the type information whether the list contains Customer
s or VipCustomer
s is lost.
{
"customers": [
{
"name": "John"
},{
"name": "Doe"
}
]
}
Therefore a specific type property could be included.
{
"customers": [
{
"//javaType": "a.b.c.Customer",
"name": "John"
},{
"//javaType": "a.b.c.VipCustomer",
"name": "Doe"
}
]
}
In order to include such functionality in JSONB a new @JsonbTypeInfo
annotation — with //jsonInfo
as default property name and possible overrides — could be defined.
Feedback is highly welcome.
Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: