Ada*_*dam 3 java generics json jackson json-deserialization
我有一些像这样的课程:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSubTypes(
{
@JsonSubTypes.Type(value = LionCage.class, name = "LION"),
@JsonSubTypes.Type(value = TigerCage.class, name = "TIGER"),
}
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public abstract class Cage<ANIMAL> {
private AnimalType type;
private ANIMAL animal;
}
public enum AnimalType {
LION, TIGER
}
public LionCage extends Cage<Lion>{
public LionCage(){
super(AnimalType.LION);
}
}
public Lion {
private int maneLength;
}
public class Zoo {
private List<Cage<?>> cages = new LinkedList<Cage<?>>();
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试执行以下操作:
String json = "{cages: [{\"type\": \"LION\", \"animal\": {\"maneLength\" : 10}}]}";
ObjectMapper om = new ObjectMapper();
Cage cage = om.readValue(json, Zoo.class);
Run Code Online (Sandbox Code Playgroud)
笼子中的第一个值应该是类型LionCage。
问题是cages.get(0).getAnimal()返回LinkedHashMap。如何使其返回正确的Animal子类?
我正在使用Jackson 1。
谢谢!
对于您的List问题,这是您想使用原始类型的一次。将Cage类型实参声明为raw,而不是使用通配符对其进行参数化。
public class Zoo {
private List<Cage> cages = new LinkedList<Cage>();
// ... getters and setters similarly
}
Run Code Online (Sandbox Code Playgroud)
使用通配符,您告诉Jackson没关系,因此它使用其默认值LinkedHashMap。如果删除通配符并使用原始类型,则它将需要在其他地方查找提示。在这种情况下,它将查看Cage其使用的实际实现。
您的JSON应该看起来像这样
String json = "{\"type\": \"LION\", \"animal\": {\"maneLength\" : 10}}";
Run Code Online (Sandbox Code Playgroud)
杰克逊将能够确定这animal是一个Lion。
通过type它将知道它是一个,LionCage并且通过类定义
public LionCage extends Cage<Lion>{
Run Code Online (Sandbox Code Playgroud)
它将知道对类型变量的任何引用Animal实际上应该是Lion。
我在杰克逊2号上。
但它也可以与Jackson 1一起使用。下面的代码
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.codehaus.jackson.map.ObjectMapper;
public class Example {
public static void main(String[] args) throws Exception {
String json = "{\"type\": \"LION\", \"animal\": {\"maneLength\" : 10}}";
ObjectMapper om = new ObjectMapper();
Cage cage = om.readValue(json, Cage.class);
System.out.println(cage.getClass());
System.out.println(cage.getAnimal());
System.out.println(((Lion) cage.getAnimal()).getManeLength());
}
}
class Lion {
private int maneLength;
public int getManeLength() {
return maneLength;
}
public void setManeLength(int maneLength) {
this.maneLength = maneLength;
}
}
class LionCage extends Cage<Lion> {
public LionCage() {
super(AnimalType.LION);
}
}
class Tiger {
}
class TigerCage extends Cage<Tiger> {
public TigerCage() {
super(AnimalType.TIGER);
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSubTypes({ @JsonSubTypes.Type(value = LionCage.class, name = "LION"),
@JsonSubTypes.Type(value = TigerCage.class, name = "TIGER"), })
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class Cage<Animal> {
public Cage(AnimalType type) {
this.setType(type);
}
public AnimalType getType() {
return type;
}
public void setType(AnimalType type) {
this.type = type;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
private AnimalType type;
private Animal animal;
}
enum AnimalType {
LION, TIGER;
}
Run Code Online (Sandbox Code Playgroud)
版画
class com.spring.LionCage
com.spring.Lion@15d16efc
10
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3120 次 |
| 最近记录: |