Class GenericJackson2JsonRedisSerializer
java.lang.Object
org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer
- All Implemented Interfaces:
RedisSerializer<Object>
Generic Jackson 2-based
RedisSerializer
that maps objects
to JSON using dynamic typing.
JSON reading and writing can be customized by configuring JacksonObjectReader
respective
JacksonObjectWriter
.
- Since:
- 1.6
- Author:
- Christoph Strobl, Mark Paluch, Mao Shuai, John Blum
-
Constructor Summary
ConstructorDescriptionCreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing.GenericJackson2JsonRedisSerializer
(com.fasterxml.jackson.databind.ObjectMapper mapper) Setting a custom-configuredObjectMapper
is one way to take further control of the JSON serialization process.GenericJackson2JsonRedisSerializer
(com.fasterxml.jackson.databind.ObjectMapper mapper, JacksonObjectReader reader, JacksonObjectWriter writer) Setting a custom-configuredObjectMapper
is one way to take further control of the JSON serialization process.GenericJackson2JsonRedisSerializer
(String classPropertyTypeName) CreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing using the given name.GenericJackson2JsonRedisSerializer
(String classPropertyTypeName, JacksonObjectReader reader, JacksonObjectWriter writer) CreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing using the given name. -
Method Summary
Modifier and TypeMethodDescriptionBuilder method used to configure and customize the internal JacksonObjectMapper
created by thisGenericJackson2JsonRedisSerializer
and used to de/serializeobjects
as JSON.deserialize
(byte[] source) Deserialize an object from the given binary data.<T> T
deserialize
(byte[] source, Class<T> type) protected com.fasterxml.jackson.databind.ObjectMapper
Gets the configuredObjectMapper
used internally by thisGenericJackson2JsonRedisSerializer
to de/serializeobjects
as JSON.static void
registerNullValueSerializer
(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String classPropertyTypeName) RegisterGenericJackson2JsonRedisSerializer.NullValueSerializer
in the givenObjectMapper
with an optionalclassPropertyTypeName
.protected com.fasterxml.jackson.databind.JavaType
resolveType
(byte[] source, Class<?> type) byte[]
Serialize the given object to binary data.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.data.redis.serializer.RedisSerializer
canSerialize, getTargetType
-
Constructor Details
-
GenericJackson2JsonRedisSerializer
public GenericJackson2JsonRedisSerializer()CreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing. -
GenericJackson2JsonRedisSerializer
CreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing using the given name. In case of an empty or null String the defaultJsonTypeInfo.Id.CLASS
will be used.- Parameters:
classPropertyTypeName
- name of the JSON property holding type information. Can be null.- See Also:
-
ObjectMapper.activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
ObjectMapper.activateDefaultTyping(PolymorphicTypeValidator, DefaultTyping, As)
-
GenericJackson2JsonRedisSerializer
public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName, JacksonObjectReader reader, JacksonObjectWriter writer) CreatesGenericJackson2JsonRedisSerializer
and configuresObjectMapper
for default typing using the given name. In case of an empty or null String the defaultJsonTypeInfo.Id.CLASS
will be used.- Parameters:
classPropertyTypeName
- name of the JSON property holding type information. Can be null.reader
- theJacksonObjectReader
function to read objects usingObjectMapper
.writer
- theJacksonObjectWriter
function to write objects usingObjectMapper
.- Since:
- 3.0
- See Also:
-
ObjectMapper.activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
ObjectMapper.activateDefaultTyping(PolymorphicTypeValidator, DefaultTyping, As)
-
GenericJackson2JsonRedisSerializer
public GenericJackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper) Setting a custom-configuredObjectMapper
is one way to take further control of the JSON serialization process. For example, an extendedSerializerFactory
can be configured that provides custom serializers for specific types.- Parameters:
mapper
- must not be null.
-
GenericJackson2JsonRedisSerializer
public GenericJackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper, JacksonObjectReader reader, JacksonObjectWriter writer) Setting a custom-configuredObjectMapper
is one way to take further control of the JSON serialization process. For example, an extendedSerializerFactory
can be configured that provides custom serializers for specific types.- Parameters:
mapper
- must not be null.reader
- theJacksonObjectReader
function to read objects usingObjectMapper
.writer
- theJacksonObjectWriter
function to write objects usingObjectMapper
.- Since:
- 3.0
-
-
Method Details
-
registerNullValueSerializer
public static void registerNullValueSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, @Nullable String classPropertyTypeName) RegisterGenericJackson2JsonRedisSerializer.NullValueSerializer
in the givenObjectMapper
with an optionalclassPropertyTypeName
. This method should be called by code that customizesGenericJackson2JsonRedisSerializer
by providing an externalObjectMapper
.- Parameters:
objectMapper
- the object mapper to customize.classPropertyTypeName
- name of the type property. Defaults to@class
if null/empty.- Since:
- 2.2
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Gets the configuredObjectMapper
used internally by thisGenericJackson2JsonRedisSerializer
to de/serializeobjects
as JSON.- Returns:
- the configured
ObjectMapper
.
-
serialize
Description copied from interface:RedisSerializer
Serialize the given object to binary data.- Specified by:
serialize
in interfaceRedisSerializer<Object>
- Parameters:
value
- object to serialize. Can be null.- Returns:
- the equivalent binary data. Can be null.
- Throws:
SerializationException
-
deserialize
Description copied from interface:RedisSerializer
Deserialize an object from the given binary data.- Specified by:
deserialize
in interfaceRedisSerializer<Object>
- Parameters:
source
- object binary representation. Can be null.- Returns:
- the equivalent object instance. Can be null.
- Throws:
SerializationException
-
deserialize
@Nullable public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws SerializationException - Parameters:
source
- can be null.type
- must not be null.- Returns:
- null for empty source.
- Throws:
SerializationException
-
configure
public GenericJackson2JsonRedisSerializer configure(Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperConfigurer) Builder method used to configure and customize the internal JacksonObjectMapper
created by thisGenericJackson2JsonRedisSerializer
and used to de/serializeobjects
as JSON.- Parameters:
objectMapperConfigurer
-Consumer
used to configure and customize the internalObjectMapper
; must not be null.- Returns:
- this
GenericJackson2JsonRedisSerializer
. - Throws:
IllegalArgumentException
- if theConsumer
used to configure and customize the internalObjectMapper
is null.- Since:
- 3.1.5
-
resolveType
protected com.fasterxml.jackson.databind.JavaType resolveType(byte[] source, Class<?> type) throws IOException - Throws:
IOException
-