[英]Jackson deserialise sub-type sets enum field to null
Update: superfluous code removed 更新:删除了多余的代码
I need to deserialised a JSON object which can be one of many sub-types of a parent class. 我需要反序列化JSON对象,它可以是父类的许多子类型之一。 Which specific sub-type it is is determined by a specific enum field in the object.
它由哪种特定的子类型由对象中的特定枚举字段确定。 I've added
@JsonTypeInfo
and @JsonSubTypes
annotations to the parent class and as far as I can tell they're all correct. 我已经在父类中添加了
@JsonTypeInfo
和@JsonSubTypes
批注,据我所知它们都是正确的。
However, the enum value isn't exactly the same as the enum's name in the Java class. 但是,枚举值与Java类中的枚举名称不完全相同。 So, in the enum class I've annotated a static method with
@JsonCreator
so Jackson knows how to deserialise the value to a proper Java enum. 因此,在枚举类中,我使用
@JsonCreator
注释了静态方法,以便杰克逊知道如何将值反序列化为适当的Java枚举。
The issue I have is that once I deserialise the JSON string, the field holding the enum doesn't contain the actual enum value - it's left as null
. 我遇到的问题是,一旦我反序列化JSON字符串,保存枚举的字段就不包含实际的枚举值-保留为
null
。
Why is the day
field left as null
and how can I set it as expected during deserialisation? 为什么将
day
字段保留为null
并且在反序列化期间如何按预期设置它?
Code below. 下面的代码。
Parent Calendar class 家长日历班
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "day")
// For the sake of correct deserialisation, we need to map from values of type to child model classes explicitly
@JsonSubTypes({ @JsonSubTypes.Type(value = Monday.class, name = "monday"),
@JsonSubTypes.Type(value = Monday.class, name = "mon"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tuesday"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tues")})
public class Calendar {
protected String calendarName;
protected DaysOfWeek day;
@JsonProperty("calendarName")
public String getCalendarName() {
return calendarName;
}
public void setCalendarName(String calendarName) {
this.calendarName = calendarName;
}
@JsonProperty("day")
public DaysOfWeek getDay() {
return day;
}
public void setDay(DaysOfWeek day) {
this.day = day;
}
}
Child classes Monday and Tuesday Monday 周一和周二的儿童班
public class Monday extends Calendar {
private NegativeFeelings object;
public Monday() {
}
public Monday(NegativeFeelings object) {
this.object = object;
}
@JsonProperty("object")
public NegativeFeelings getObject() {
return object;
}
public void setObject(NegativeFeelings object) {
this.object = object;
}
}
Tuesday 星期二
public class Tuesday extends Calendar {
private Meetings object;
public Tuesday() {
}
public Tuesday(Meetings object) {
this.object = object;
}
@JsonProperty("object")
public Meetings getObject() {
return object;
}
public void setObject(Meetings object) {
this.object = object;
}
}
DaysOfWeek enum class DaysOfWeek枚举类
public enum DaysOfWeek {
MONDAY("monday"),
MON("mon"),
TUESDAY("tuesday"),
TUES("tues");
private String value;
DaysOfWeek(String value) {
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static DaysOfWeek fromEventString(@JsonProperty("value") String eventString) {
return Arrays.stream(DaysOfWeek.values()).filter(e -> e.value.equals(eventString)).findFirst().get();
}
}
JSON source string JSON源字符串
{
"calendarName":"My Tuesdays",
"day":"tuesday",
"object":{
"meetings":[
"team",
"company",
"client"]
}
}
Deserialised object (deserialised from above string) 反序列化的对象(从字符串上方反序列化)
Tuesday {
object=Meetings {
meetings=[team, company, client]
},
calendarName=My Tuesdays,
day=null //I want this to be "tuesday", not null
}
Found the answer via colleague. 通过同事找到了答案。
I needed to set the visible
property of the @JsonTypeInfo
annotation to true
: 我需要将
@JsonTypeInfo
批注的visible
属性设置为true
:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "day", visible = true)
// For the sake of correct deserialisation, we need to map from values of type to child model classes explicitly
@JsonSubTypes({ @JsonSubTypes.Type(value = Monday.class, name = "monday"),
@JsonSubTypes.Type(value = Monday.class, name = "mon"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tuesday"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tues")})
public class Calendar {
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.