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
After creating the XXXSerializer class, I suggest to add a getter method that can access the ObjectMapper to adjust the configurations.
If ObjectMapper is not specified in the current Serializer class constructor, the ObjectMapper that has been set internally by default is used.
In this state, if the user wants to make minor modifications to the ObjectMapper, since it cannot be accessed, it is inconvenient to create an ObjectMapper with new settings and then create a new Serializer.
And I know that DefaultTyping of ObjectMapper of GenericJackson2JsonRedisSerializer has been changed from DefaultTyping.NON_FINAL to EVERYTHING since version 2.7.0 due to the issue & PR below.
If there is a method that can access the ObjectMapper, it has the advantage of being able to flexibly change only the necessary parts of the default setting even after creating the Serializer.
Sample
Without getter()
// always make new ObjectMapper when add Modules or adjust configurationsfinalObjectMappermapper = newObjectMapper()
.registerModule(newParameterNamesModule(JsonCreator.Mode.DEFAULT))
.registerModule(newJdk8Module())
.registerModule(newJavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// duplicated -> same as default ObjectMapper configurations, mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(),
DefaultTyping.EVERYTHING,
As.PROPERTY);
returnnewGenericJackson2JsonRedisSerializer(mapper);
If a getter() method is provided for the ObjectMapper, it can be used flexibly in the same way as the test code below.
@TestvoidgenericJackson2JsonRedisSerializerCanCustomizeObjectMapperAfterCreated() {
GenericJackson2JsonRedisSerializerserializer = newGenericJackson2JsonRedisSerializer();
serializer.getMapper() // add getter method for accessing ObjectMapper in Serializer
.registerModule(newJavaTimeModule()) // just add module with default ObjectMapper
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) // adjust configurations
.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);
ObjectWithOffsetDateTimesource = newObjectWithOffsetDateTime(1L, "doljae", OffsetDateTime.now());
assertThat(serializer.deserialize(serializer.serialize(source))).isEqualTo(source);
}
Additional info
Regarding this, after folking the repository, I've already done some work targeting the GenericJackson2JsonRedisSerializer.
We do not want to expose the ObjectMapper from the serializer because that is a potential source for bugs and it leaks internal implementation details. Instead, please configure multiple serializers or create the serializer with a given ObjectMapper.
Description
After creating the
XXXSerializer
class, I suggest to add a getter method that can access theObjectMapper
to adjust the configurations.If
ObjectMapper
is not specified in the current Serializer class constructor, theObjectMapper
that has been set internally by default is used.In this state, if the user wants to make minor modifications to the
ObjectMapper
, since it cannot be accessed, it is inconvenient to create anObjectMapper
with new settings and then create a new Serializer.And I know that
DefaultTyping
ofObjectMapper
ofGenericJackson2JsonRedisSerializer
has been changed fromDefaultTyping.NON_FINAL
toEVERYTHING
since version 2.7.0 due to the issue & PR below.GenericJackson2JsonRedisSerializer
[DATAREDIS-995] #1566If there is a method that can access the
ObjectMapper
, it has the advantage of being able to flexibly change only the necessary parts of the default setting even after creating the Serializer.Sample
Without getter()
If a getter() method is provided for the ObjectMapper, it can be used flexibly in the same way as the test code below.
Additional info
Regarding this, after folking the repository, I've already done some work targeting the
GenericJackson2JsonRedisSerializer
.The text was updated successfully, but these errors were encountered: