Java Jackson多态反序列化基元类型
我正在使用Jackson 1.9.13,并有以下简单的类进行序列化/反序列化Java Jackson多态反序列化基元类型,java,jackson,deserialization,json-deserialization,Java,Jackson,Deserialization,Json Deserialization,我正在使用Jackson 1.9.13,并有以下简单的类进行序列化/反序列化 public interface Comparable { //interfaces definition } public class ComparableString implements Comparable { protected String member; //Constructor and interface implementation etc. @JsonValu
public interface Comparable {
//interfaces definition
}
public class ComparableString implements Comparable {
protected String member;
//Constructor and interface implementation etc.
@JsonValue
public String getString() { return this.member; }
}
//Many other comparables
public class Checker {
private Comparable left;
private Comparable right;
//Getters & Setters
public Checker(Comparable left, Comparable right) {
}
}
现在,我用两个可比较的字符串(其值为“A”和“B”)构建了这样一个检查器,并希望对其进行序列化,它工作得非常好,并且给了我,例如
{"left":"A", "right":"B"}
但现在我想反序列化它,它告诉我:
无法构造可比较的实例,问题:抽象类型只能使用其他类型信息进行实例化
我理解,当它看到可比性时,它无法确定应该使用哪种具体类型。因此,我额外注释了类似的界面,如下所示:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, defaultImpl = ComparableString.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = ComparableString.class)
//other comparable types
})
public interface Comparable {
//interfaces definition
}
{
"left":["ComparableString","A"],
"right":["ComparableString","B"]
}
但现在,在对原语进行反序列化时,它需要额外的类型字段(原语周围显然没有任何额外的类型信息),并且序列化也发生了变化,它变成如下所示:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, defaultImpl = ComparableString.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = ComparableString.class)
//other comparable types
})
public interface Comparable {
//interfaces definition
}
{
"left":["ComparableString","A"],
"right":["ComparableString","B"]
}
我试图指定use=JsonTypeInfo.Id.NONE
,然后返回到无法识别错误类型。显然,如果没有类型信息,它就无法确定使用哪种类型来反序列化原语
我的问题是:是否有可能以某种方式手动将原语反序列化映射到一个类似的类中?在不牺牲原语简单性的情况下(JSON正文中没有额外的类型字段)