I have an abstract class called Fruit and I put the @JsonTypeInfo
and @JsonSubTypes
on it as follow:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "fruits")
@JsonSubTypes({
@Type(value = Apple.class, name = "sandbox.Apple"),
@Type(value = FruitGroup.class, name = "sandbox.FruitGroup")
})
public abstract class Fruit {
public abstract String getName();
@Override
public String toString() {
return "Fruit [getName()=" + getName() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
}
And my derived class looks like this
@JsonTypeName("sandbox.Apple")
public class Apple extends Fruit {
private String _name;
public void setName(String name) {
_name = name;
}
@Override
public String getName() {
return _name;
}
======[UPDATED]====== I also have class FruitGroup that extends Fruit and contains Array of Fruit.
@JsonTypeName("sandbox.FruitGroup")
public class FruitGroup extends Fruit {
private Fruit[] _Fruit;
private String _name;
private String _category;
public Fruit[] getFruit() {
return _Fruit;
}
public void setFruits(Fruit[] fruits) {
_Fruit = fruits;
}
public void setName(String name) {
_name = name;
}
@Override
public String getName() {
return _name;
}
public void setCategory(String category) {
_category = category;
}
public String getCategory() {
return _category;
}
}
When I tried to deserialize the jsontext into Fruit object, I found the following exception:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '[' as a subtype of `sandbox.FruitGroup`: known type ids = [FruitGroup, sandbox.Apple] at [Source: (String)"{"fruits":[["sandbox.Apple",{"name": "Apple"}]],"name": "Fruit Group"}"; line: 1, column: 11]
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
The jsontext [UPDATED] actually was generated by jackson version 2.10.2 and I didn't put any JSON annotations on my classes formerly. After I upgrade the jackson version to 2.11.0, I also update my abstract class to put the JSON annotations in it. Then I tried to deserialize it by using jackson version 2.11.0, but I got an error instead. Could you guys help me to solve this issue? Here's my jsontext
{
"fruit": [
[
"sandbox.Apple",
{
"name": "Apple1"
}
]
],
"name": "Group of apples",
"category": "Sweet fruit"
}