我有一个类似的JSON对象:
{"name":"John", "grade":"A"}
或
{"name":"Mike", "grade":"B"}
或
{"name":"Simon", "grade":"C"}
等
我正在尝试将上面的JSON映射到:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee{
@JsonIgnoreProperties(ignoreUnknown = true)
public enum Grade{ A, B, C }
Grade grade;
String name;
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
上面的映射工作正常,但将来会有更多的“等级”类型,比如D,E等打破现有的映射并抛出以下异常
05-08 09:56:28.130: W/System.err(21309): org.codehaus.jackson.map.JsonMappingException: Can not construct instance of Employee from String value 'D': value not one of declared Enum instance names
有没有办法忽略枚举类型中的未知字段?
由于
答案 0 :(得分:20)
我认为您应该为Grade
枚举定义外部deserializer。
我在枚举中添加了其他字段 - UNKNOWN:
enum Grade {
A, B, C, UNKNOWN;
public static Grade fromString(String value) {
for (Grade grade : values()) {
if (grade.name().equalsIgnoreCase(value)) {
return grade;
}
}
return UNKNOWN;
}
}
class Employee {
@JsonDeserialize(using = GradeDeserializer.class)
private Grade grade;
private String name;
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [grade=" + grade + ", name=" + name + "]";
}
}
现在,解析器看起来像这样:
class GradeDeserializer extends JsonDeserializer<Grade> {
@Override
public Grade deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
return Grade.fromString(parser.getValueAsString());
}
}
使用示例:
public class JacksonProgram {
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory();
JsonParser parser = jsonFactory
.createJsonParser("{\"name\":\"John\", \"grade\":\"D\"}");
Employee employee = objectMapper.readValue(parser, Employee.class);
System.out.println(employee);
}
}
输出:
Employee [grade=UNKNOWN, name=John]
如果您不想添加其他字段,请返回null
,例如。
答案 1 :(得分:20)
我找到了一种方法,如下所示:
public static void main(String[] args) throws JsonParseException, JsonMappingException, UnsupportedEncodingException, IOException {
String json = "{\"name\":\"John\", \"grade\":\"D\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
Employee employee = mapper.readValue(new ByteArrayInputStream(json.getBytes("UTF-8")), Employee.class);
System.out.println(employee.getGrade());
}
输出:
空
其他课程:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {
private String name;
private Grade grade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public enum Grade {A, B, C}
我还没有想过用注释做到这一点的方法。
我希望这会有所帮助。
答案 2 :(得分:5)
@JsonCreator
相比, @JsonDeserialize
提供了更简洁的解决方案。
我们的想法是使用@JsonCreator
注释您的valueOf()
替换(在此示例中称为safeValueOf()
),然后Jackson会使用您的实现对字符串进行反序列化。
请注意,实现在枚举中,您可以将其用作其他对象中的字段而不做任何更改。
下面的解决方案包含在单元测试中,因此您可以直接运行它。
import static junit.framework.TestCase.assertEquals;
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EmployeeGradeTest {
public enum Grade {
A, B, C, OTHER;
@JsonCreator
public static Grade safeValueOf(String string) {
try {
return Grade.valueOf(string);
} catch (IllegalArgumentException e) {
return OTHER;
}
}
}
@Test
public void deserialize() throws IOException {
assertEquals(Grade.A, new ObjectMapper().readValue("\"A\"", Grade.class));
}
@Test
public void deserializeNewValue() throws IOException {
assertEquals(Grade.OTHER, new ObjectMapper().readValue("\"D\"", Grade.class));
}
}
答案 3 :(得分:2)
有两种处理此类情况的方法:
null
为此,请在READ_UNKNOWN_ENUM_VALUES_AS_NULL
中启用ObjectMapper
设置。此后,当您解析具有未知枚举字段值的对象时,它将被反序列化为null
。请参阅下面的示例代码:
import com.fasterxml.jackson.databind.*;
public static ObjectMapper getMapper() {
return ObjectMapper().enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
}
UNKNOWN
)为此,请首先在READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE
中启用ObjectMapper
设置。然后,在您的枚举类中,用@JsonEnumDefaultValue
注释要用作默认枚举值的元素。请参阅下面的代码示例:
import com.fasterxml.jackson.databind.*;
public static ObjectMapper getMapper() {
return ObjectMapper().enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
}
public enum YourEnum {
A,
B,
C,
@JsonEnumDefaultValue UNKNOWN;
}
答案 4 :(得分:0)
我正在使用boot2(尽管这也可以在boot 1中使用),但是您可以仅在applicaiton properties / yaml中启用反序列化功能;
spring:
jackson:
deserialization:
READ_UNKNOWN_ENUM_VALUES_AS_NULL: true