问题描述
我有一些类型需要使用它们自己的特殊反序列化器和序列化器来处理,但是当类型嵌套在Optional中时,如何指示Jackson使用它们呢?
I have some types which need to be handled with their own special deserializers and serializers, but how do I instruct Jackson to use them when the type is nested inside an Optional?
我正在使用JDK8Module,它非常适合不需要任何特殊处理的任何类型.当在可选字段上使用@JsonDeserialize和@JsonSerialize批注时,它们似乎没有任何方法可应用于Optional内部的值:
I am using the JDK8Module which works great for any type not needing any special handling. The @JsonDeserialize and @JsonSerialize annotations don't seem to have any way to apply to the value inside an Optional when they are used on an Optional field:
@JsonDeserialize(?????)
Optional<SpecialType> myField
推荐答案
完成此操作的正确方法是:
The correct way to accomplish this is:
@JsonDeserialize(contentUsing=CustomDeserializer.class)
Optional<SpecialType> myField
有一个缺陷阻止了此工作,该问题已修复2.7.4.
There was a defect that prevented this working which was fixed in 2.7.4.
这篇关于有什么方法可以为嵌套在杰克逊的Optional内的类型指定自定义解串器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!