You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 :
publicObjectMapperactivateDefaultTypingAsProperty(PolymorphicTypeValidatorptv,
DefaultTypingapplicability, StringpropertyName)
{
TypeResolverBuilder<?> typer = _constructDefaultTypeResolverBuilder(applicability,
ptv);
// we'll always use full class name, when using defaultingJsonTypeInfo.ValuetypeInfo = JsonTypeInfo.Value.construct(JsonTypeInfo.Id.CLASS, JsonTypeInfo.As.PROPERTY,
propertyName, null, false, null);
typer = typer.withSettings(typeInfo);
returnsetDefaultTyping(typer);
}
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.
I know I can use a non Generic version but it would necessistat to define as many cache managers than we have of kind of cached types, which would be cumbersome because we have many many (many).
Unfortunately, GenericJackson2JsonRedisSerializer don't seems to work fine whenever the cached objects are more complexe as one of my DTO objects; such as :
Locale
List< MyDto >
Map< Integer, MyDto >
...
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 of activateDefaultTypingAsProperty (without @JsonSubTypes, as my DTOs don't really have ones) the type informations are not serialized, which seems to be the intended behavior
So, 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 along
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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