java 基于 Enum 的 Jackson 多态反序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15945404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson Polymorphic Deserialization based on Enum
提问by Curro
I'm working with HymansonPolymorphicDeserialization, this is my code which deserializes into the proper class based in the 'type' property:
我正在使用HymansonPolymorphicDeserialization,这是我的代码,它反序列化为基于 'type' 属性的正确类:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type",
defaultImpl = Event.class,
visible = true)
@JsonSubTypes({
@Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),
@Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),
})
public abstract class AbstractEvent {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
It's working perfectly and my json turns into the expected class according to the 'type' value.
它运行良好,我的 json 根据 'type' 值变成了预期的类。
However, I'm considering to move the 'type' property from String to Enum, this is my new code with this change:
但是,我正在考虑将 'type' 属性从 String 移动到 Enum,这是我进行了此更改的新代码:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type",
defaultImpl = Event.class,
visible = true)
@JsonSubTypes({
@Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),
@Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),
})
public abstract class AbstractEvent {
private EventType type;
public EventType getType() {
return type;
}
public void setType(EventType type) {
this.type = type;
}
}
and the Enum:
和枚举:
public enum EventType {
SPECIAL_EVENT_1,
SPECIAL_EVENT_2,
EVENT;
}
The problem is that this second approach is not working... any idea why??? can I use Enum here???
问题是这第二种方法不起作用......知道为什么吗???我可以在这里使用枚举吗???
Thanks!
谢谢!
采纳答案by Curro
Fixed!
固定的!
It works with Hymanson 2.0!!
它适用于Hyman逊 2.0!!