Class JacksonJsonRedisSerializer<T>
java.lang.Object
org.springframework.data.redis.serializer.JacksonJsonRedisSerializer<T>
- All Implemented Interfaces:
RedisSerializer<T>
RedisSerializer that can read and write JSON using
Jackson 3 and
Jackson 3 Databind ObjectMapper.
This serializer can be used to bind to typed beans, or untyped HashMap instances.
Note:Null objects are serialized as empty arrays and vice versa.
JSON reading and writing can be customized by configuring JacksonObjectReader respective
JacksonObjectWriter.
- Since:
- 4.0
- Author:
- Christoph Strobl, Thomas Darimont, Mark Paluch
-
Constructor Summary
ConstructorsConstructorDescriptionJacksonJsonRedisSerializer(Class<T> type) Creates a newJacksonJsonRedisSerializerfor the given targetClass.JacksonJsonRedisSerializer(tools.jackson.databind.JavaType javaType) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType.JacksonJsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, Class<T> type) Creates a newJacksonJsonRedisSerializerfor the given targetClass.JacksonJsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType.JacksonJsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType, JacksonObjectReader reader, JacksonObjectWriter writer) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType. -
Method Summary
Modifier and TypeMethodDescription@Nullable Tdeserialize(byte @Nullable [] bytes) Deserialize an object from the given binary data.protected tools.jackson.databind.JavaTypegetJavaType(Class<?> clazz) Returns the JacksonJavaTypefor the specific class.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
-
JacksonJsonRedisSerializer
Creates a newJacksonJsonRedisSerializerfor the given targetClass.- Parameters:
type- must not be null.
-
JacksonJsonRedisSerializer
public JacksonJsonRedisSerializer(tools.jackson.databind.JavaType javaType) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType.- Parameters:
javaType- must not be null.
-
JacksonJsonRedisSerializer
Creates a newJacksonJsonRedisSerializerfor the given targetClass.- Parameters:
mapper- must not be null.type- must not be null.
-
JacksonJsonRedisSerializer
public JacksonJsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType.- Parameters:
mapper- must not be null.javaType- must not be null.
-
JacksonJsonRedisSerializer
public JacksonJsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType, JacksonObjectReader reader, JacksonObjectWriter writer) Creates a newJacksonJsonRedisSerializerfor the given targetJavaType.- Parameters:
mapper- must not be null.javaType- must not be null.reader- theJacksonObjectReaderfunction to read objects usingObjectMapper.writer- theJacksonObjectWriterfunction to write objects usingObjectMapper.
-
-
Method Details
-
serialize
Description copied from interface:RedisSerializerSerialize the given object to binary data.- Specified by:
serializein interfaceRedisSerializer<T>- Parameters:
value- object to serialize. Can be null.- Returns:
- the equivalent binary data. Can be an empty array but never null.
- Throws:
SerializationException
-
deserialize
Description copied from interface:RedisSerializerDeserialize an object from the given binary data.- Specified by:
deserializein interfaceRedisSerializer<T>- Parameters:
bytes- object binary representation. Can be null.- Returns:
- the equivalent object instance. Can be null.
- Throws:
SerializationException
-
getJavaType
Returns the JacksonJavaTypefor the specific class.Default implementation returns
TypeFactory.constructType(java.lang.reflect.Type), but this can be overridden in subclasses, to allow for custom generic collection handling. For instance:protected JavaType getJavaType(Class<?> clazz) { if (List.class.isAssignableFrom(clazz)) { return TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, MyBean.class); } else { return super.getJavaType(clazz); } }- Parameters:
clazz- the class to return the java type for- Returns:
- the java type
-