简体   繁体   中英

Is there some way to specify a custom deserializer for a type that is nested inside an Optional for 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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM