使用Jackson反序列化JSON时的隐式默认值 [英] Implicit default values when deserializing JSON using Jackson
问题描述
在反序列化各种JSON消息时,我想为某种类型的属性提供默认值。 一般建议只是在类中指定值,但如果你必须这样做,这很容易出错很多课程。您可能会忘记一个,最终得到 null
而不是默认值。我的目的是将每个属于 Optional< T>
的属性设置为 Optional.absent
。由于 null
正是可选
正试图消除,因此将它们与Jackson一起使用已被证明令人沮丧。
When deserializing a variety of JSON messages, I want to provide a default value for attributes of a certain type. It is generally suggested to simply specify the value in the Class, but this is error-prone if you have to do this across many Classes. You might forget one and end up with null
instead of a default value. My intention is to set every property that is an Optional<T>
to Optional.absent
. Since null
is exactly what Optional
is trying to eliminate, using them with Jackson has proven to be frustrating.
Jackson允许您自定义反序列化过程的大多数功能都集中在作为输入的JSON上,而不是围绕实例化您要反序列化的Object的过程。我似乎最接近一般解决方案是建立我自己的 ValueInstantiator
,但我还有两个问题:
Most features of Jackson that allow you to customize the deserialization process focus on the JSON that is the input, not around the process of instantiating the Object that you are deserializing into. The closest I seem to be getting to a general solution is by building my own ValueInstantiator
, but there are two remaining issues I have:
- 如何让它只实例化
可选
为缺席
但不是干扰实例化过程的其余部分? - 如何将最终结果连接到我的
ObjectMapper
?
- how do I make it only instantiate
Optional
asabsent
but not interfere with the rest of the instantiation process? - how do I wire the end result into my
ObjectMapper
?
更新:我想澄清一下,我正在寻找一个不涉及修改<的解决方案em>每个包含可选
的类。我反对违反DRY原则。每当我们将 Optional
添加到新的或现有的类时,我或我的同事不应该考虑必须做额外的事情。我希望能够说,在每个Class中反序列化每个可选字段,预先填充 Absent
,只需执行一次,然后完成。
UPDATE: I want to clarify that I am looking for a solution that does not involve modifying each Class that contains Optional
's. I'm opposed to violating the DRY principle. Me or my colleagues should not have to think about having to do something extra every time we add Optional
's to a new or existing Class. I want to be able to say, "make every Optional field in every Class I deserialize into, pre-filled with Absent
", only once, and be done with it.
这意味着以下内容:
- 抽象父类(需要声明) )
- 自定义Builder / Creator / JsonDeserializer(需要在每个适用的类上进行注释)
- MixIn的?我尝试了这个,结合反射,但我不知道如何访问我被混合的类......
推荐答案
特别是对于java.lang.Optional,杰克逊人自己有一个模块: https://github.com/FasterXML/jackson-datatype-jdk8
Specifically for java.lang.Optional, there is a module by the Jackson guys themselves: https://github.com/FasterXML/jackson-datatype-jdk8
Guava可选由 https://github.com/FasterXML/jackson-datatype-guava
它将为null创建一个Optional.absent,但不会为缺少的JSON值创建: - (。
It will create a Optional.absent for null's, but not for absent JSON values :-(.
参见 https://github.com/FasterXML/jackson-databind/issues/618 和 https://github.com/FasterXML/jackson-datatype-jdk8/issues/2 。
因此,您应该像初始化集合一样初始化Optionals。这是一个很好的做法,所以你应该能够强制执行它。
So you're stuck with initializing your Optionals just as you should initialize collections. It is a good practice, so you should be able to enforce it.
private Optional<Xxx> xxx = Optional.absent();
private List<Yyy> yyys = Lists.newArrayList();
这篇关于使用Jackson反序列化JSON时的隐式默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!