Is there some way to specify a custom deserializer for a type that is nested inside an Optional for Jackson?

谁说胖子不能爱 提交于 2019-12-20 04:51:10

问题


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?

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

回答1:


The correct way to accomplish this is:

 @JsonDeserialize(contentUsing=CustomDeserializer.class)
 Optional<SpecialType> myField

There was a defect that prevented this working which was fixed in 2.7.4.



来源:https://stackoverflow.com/questions/36114658/is-there-some-way-to-specify-a-custom-deserializer-for-a-type-that-is-nested-ins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!