【发布时间】:2018-12-26 15:46:09
【问题描述】:
我正在寻找一个使用 Jackson (2.8) 的便捷解决方案,以在反序列化之前/期间过滤掉指向空字符串值的字段:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Guis {
public enum Context {
SDS,
NAVIGATION
}
public enum Layer {
Library,
Status,
Drivingdata
}
// client code
String s = "{\"context\":\"\",\"layer\":\"Drivingdata\"}";
ObjectMapper mapper = new ObjectMapper();
Guis o = mapper.readValue(s, Guis.class);
错误
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type cq.speech.rsi.api.Guis$Context from String "": value not one of declared Enum instance names: [SDS NAVIGATION] at [Source: {"context":"","layer":"Drivingdata"}; line: 1, column: 12] (through reference chain: cq.speech.rsi.api.Guis["context"])
我还尝试了什么... 啊这个
mapper.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
错误仍然存在,显然即使谷歌搜索也没有多大帮助......
编辑
set DeserializationFeature 不能像上面示例的那样工作。对我来说,最终的解决方案是添加这个 sn-p:
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
【问题讨论】:
-
你期望
Context在反序列化后会是什么? -
我知道在 jackson 中编写自定义反序列化器的选项。就我而言,完全忽略
Context或Context为空就足够了。 -
这听起来像是值得为之开张票的东西。在这种情况下,我天真地期望枚举上的“空”可以映射为 null。
-
谢谢!很高兴我能提供帮助。请参阅What should I do when someone answers my question?。
-
好的,我删除了确认