use*_*663 1 java enums jackson json-deserialization
更新:删除了多余的代码
我需要反序列化一个 JSON 对象,该对象可以是父类的许多子类型之一。具体是哪种子类型由对象中的特定枚举字段决定。我已经向父类添加了@JsonTypeInfo注释@JsonSubTypes,据我所知它们都是正确的。
但是,枚举值与 Java 类中的枚举名称并不完全相同。因此,在枚举类中,我注释了一个静态方法,@JsonCreator以便 Jackson 知道如何将值反序列化为正确的 Java 枚举。
我遇到的问题是,一旦我反序列化 JSON 字符串,保存枚举的字段不包含实际的枚举值 - 它保留为null.
为什么该day字段保留为null反序列化期间的预期值以及如何将其设置为预期值?
代码如下。
家长日历类
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
儿童班 周一和周二 周一
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;
}
}
Run Code Online (Sandbox Code Playgroud)
周二
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
JSON 源字符串
{
"calendarName":"My Tuesdays",
"day":"tuesday",
"object":{
"meetings":[
"team",
"company",
"client"]
}
}
Run Code Online (Sandbox Code Playgroud)
反序列化对象(从上面的字符串反序列化)
Tuesday {
object=Meetings {
meetings=[team, company, client]
},
calendarName=My Tuesdays,
day=null //I want this to be "tuesday", not null
}
Run Code Online (Sandbox Code Playgroud)
通过同事找到了答案。
我需要将注释visible的属性设置@JsonTypeInfo为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 {
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |