Replies: 1 comment
-
Ok, so, "Default Typing" really has to (I think) be based on class names, not type names, as it can cover all kinds of classes. It does not seem plausible to -- in general -- use type names, for deserialization. Or conversely, when using type names, Beyond this, the other class name option -- "MINIMAL_CLASS" -- is based on relative Java package name compared to that of base type. That wouldn't work in general as ultimately the only true base type in "Default Typing" case is As to alternatives to Default Typing, I always advocate for a simple wrapper type like:
in which case you should get mostly default typing behavior, forcing polymorphic handling of NON-generic root value. I am not sure why this is not more widely used as "the polymorphic handling wrapper" pattern but here we are. |
Beta Was this translation helpful? Give feedback.
-
Hi Jackson's user community.
TL;DR :
I've seen in the ObjectMapper class's code that whenever using the
activateDefaultTypingAsProperty
, there is no choice using something else than the full class name typing :How comes ?
For terseness I'd like to use a shorter strategy.
Is it possible while still using default typing without encountering ugly troubles ?
Further explanation of my context :
Disclaimers :
I know default typing is not recommended and that the selective usage of @JsonTypeInfo is.
But I'm encountering troubles using it 😋 :
My context is a Spring Boot application using many @cachable annotations, and storing them in a redis cache
I want to replace the default java serialization system for Jackson one.
To do so, I'm using the GenericJackson2JsonRedisSerializer class.
Unfortunately, GenericJackson2JsonRedisSerializer don't seems to work fine whenever the cached objects are more complexe as one of my DTO objects; such as :
I was expecting it would work out of the box, but unfortunately, no matter I build it the following way :
things doesn't work well in the previously mentioned cases.
I guess it's mostly because of the type erasure that occur on generic collections returned by methods annotated with @Cacheable.
That's why I've defined my own version of GenericJackson2JsonRedisSerializer, and setup an extra AOP layer along with it.
It enable to enforce the deserialization type startup point : the AOP layer fetch it from the (type erased) Method return type where @cachable annotation is placed.
And with a few extra home made annotations, it's possible to enforce the right JavaType for cases such as Map< Integer, MyDto >,, to avoid having Strings keys used instead of Integer ones upon cache retrieval for example.
The whole thing is working fine so far, but I still need to have type informations placed in the produced JSON strings, and I can't picture another way to do so than to user a mapper where I've
activateDefaultTypingAsProperty
Whenever I try simply use
@JsonTypeInfo
instead ofactivateDefaultTypingAsProperty
(without @JsonSubTypes, as my DTOs don't really have ones) the type informations are not serialized, which seems to be the intended behaviorSo, in the end, I'm wondering if there is an in between way to configure the whole thing without being forced to use
activateDefaultTypingAsProperty
and the full class name typing coming alongLooking for your advices about it 😋
Beta Was this translation helpful? Give feedback.
All reactions