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 and from JSON using
dynamic typing.
JSON reading and writing can be customized by configuring a JacksonObjectReader and
JacksonObjectWriter.
- Since:
- 1.6
- Author:
- Christoph Strobl, Mark Paluch, Mao Shuai, John Blum, Anne Lee
- See Also:
-
JacksonObjectReaderJacksonObjectWriterObjectMapper
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for configuring and creating aGenericJackson2JsonRedisSerializer. -
Constructor Summary
ConstructorsConstructorDescriptionCreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing.GenericJackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper) Setting a custom-configuredObjectMapperis 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-configuredObjectMapperis one way to take further control of the JSON serialization process.GenericJackson2JsonRedisSerializer(String typeHintPropertyName) CreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing using the givenname.GenericJackson2JsonRedisSerializer(String typeHintPropertyName, JacksonObjectReader reader, JacksonObjectWriter writer) CreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing using the givennamealong with the given, requiredJacksonObjectReaderandJacksonObjectWriterused to read/writeObjectsde/serialized as JSON. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Factory method returning a Builder used to construct and configure aGenericJackson2JsonRedisSerializer.Builder method used to configure and customize the internal JacksonObjectMappercreated by thisGenericJackson2JsonRedisSerializerand used to de/serializeobjectsas JSON.deserialize(byte[] source) Deserialize an object from the given binary data.<T> Tdeserialize(byte[] source, Class<T> type) protected com.fasterxml.jackson.databind.ObjectMapperGets the configuredObjectMapperused internally by thisGenericJackson2JsonRedisSerializerto de/serializeobjectsas JSON.static voidregisterNullValueSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String typeHintPropertyName) RegisterGenericJackson2JsonRedisSerializer.NullValueSerializerin the givenObjectMapperwith an optionaltypeHintPropertyName.protected com.fasterxml.jackson.databind.JavaTyperesolveType(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, waitMethods inherited from interface org.springframework.data.redis.serializer.RedisSerializer
canSerialize, getTargetType
-
Constructor Details
-
GenericJackson2JsonRedisSerializer
public GenericJackson2JsonRedisSerializer()CreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing. -
GenericJackson2JsonRedisSerializer
CreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing using the givenname.In case
nameis empty or null, thenJsonTypeInfo.Id.CLASSwill be used.- Parameters:
typeHintPropertyName-nameof 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 typeHintPropertyName, JacksonObjectReader reader, JacksonObjectWriter writer) CreatesGenericJackson2JsonRedisSerializerinitialized with anObjectMapperconfigured for default typing using the givennamealong with the given, requiredJacksonObjectReaderandJacksonObjectWriterused to read/writeObjectsde/serialized as JSON.In case
nameis empty or null, thenJsonTypeInfo.Id.CLASSwill be used.- Parameters:
typeHintPropertyName-nameof the JSON property holding type information; can be null.reader-JacksonObjectReaderfunction to read objects usingObjectMapper.writer-JacksonObjectWriterfunction 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-configuredObjectMapperis one way to take further control of the JSON serialization process. For example, an extendedSerializerFactorycan 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-configuredObjectMapperis one way to take further control of the JSON serialization process. For example, an extendedSerializerFactorycan be configured that provides custom serializers for specific types.- Parameters:
mapper- must not be null.reader- theJacksonObjectReaderfunction to read objects usingObjectMapper.writer- theJacksonObjectWriterfunction to write objects usingObjectMapper.- Since:
- 3.0
-
-
Method Details
-
builder
public static GenericJackson2JsonRedisSerializer.GenericJackson2JsonRedisSerializerBuilder builder()Factory method returning a Builder used to construct and configure aGenericJackson2JsonRedisSerializer.- Returns:
- new
GenericJackson2JsonRedisSerializer.GenericJackson2JsonRedisSerializerBuilder. - Since:
- 3.3.1
-
registerNullValueSerializer
public static void registerNullValueSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, @Nullable String typeHintPropertyName) RegisterGenericJackson2JsonRedisSerializer.NullValueSerializerin the givenObjectMapperwith an optionaltypeHintPropertyName. This method should be called by code that customizesGenericJackson2JsonRedisSerializerby providing an externalObjectMapper.- Parameters:
objectMapper- the object mapper to customize.typeHintPropertyName- name of the type property. Defaults to@classif null/empty.- Since:
- 2.2
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Gets the configuredObjectMapperused internally by thisGenericJackson2JsonRedisSerializerto de/serializeobjectsas JSON.- Returns:
- the configured
ObjectMapper.
-
serialize
Description copied from interface:RedisSerializerSerialize the given object to binary data.- Specified by:
serializein 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:RedisSerializerDeserialize an object from the given binary data.- Specified by:
deserializein 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- array of bytes containing the JSON to deserialize; can be null.type-typeofObjectfrom which the JSON will be deserialized; must not be null.- Returns:
- null for an empty source, or an
Objectof the giventypedeserialized from the array of bytes containing JSON. - Throws:
IllegalArgumentException- if the giventypeis null.SerializationException- if the array of bytes cannot be deserialized as an instance of the giventype
-
configure
public GenericJackson2JsonRedisSerializer configure(Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperConfigurer) Builder method used to configure and customize the internal JacksonObjectMappercreated by thisGenericJackson2JsonRedisSerializerand used to de/serializeobjectsas JSON.- Parameters:
objectMapperConfigurer-Consumerused to configure and customize the internalObjectMapper; must not be null.- Returns:
- this
GenericJackson2JsonRedisSerializer. - Throws:
IllegalArgumentException- if theConsumerused to configure and customize the internalObjectMapperis null.- Since:
- 3.1.5
-
resolveType
protected com.fasterxml.jackson.databind.JavaType resolveType(byte[] source, Class<?> type) throws IOException - Throws:
IOException
-