Class Jackson2JsonRedisSerializer<T>

java.lang.Object
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer<T>
All Implemented Interfaces:
RedisSerializer<T>

public class Jackson2JsonRedisSerializer<T> extends Object implements RedisSerializer<T>
RedisSerializer that can read and write JSON using Jackson's and Jackson 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:
1.2
Author:
Thomas Darimont, Mark Paluch
  • Field Details

    • DEFAULT_CHARSET

      @Deprecated(since="3.0", forRemoval=true) public static final Charset DEFAULT_CHARSET
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 3.0 for removal.
  • Constructor Details

    • Jackson2JsonRedisSerializer

      public Jackson2JsonRedisSerializer(Class<T> type)
      Creates a new Jackson2JsonRedisSerializer for the given target Class.
      Parameters:
      type - must not be null.
    • Jackson2JsonRedisSerializer

      public Jackson2JsonRedisSerializer(com.fasterxml.jackson.databind.JavaType javaType)
      Creates a new Jackson2JsonRedisSerializer for the given target JavaType.
      Parameters:
      javaType - must not be null.
    • Jackson2JsonRedisSerializer

      public Jackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<T> type)
      Creates a new Jackson2JsonRedisSerializer for the given target Class.
      Parameters:
      mapper - must not be null.
      type - must not be null.
      Since:
      3.0
    • Jackson2JsonRedisSerializer

      public Jackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.databind.JavaType javaType)
      Creates a new Jackson2JsonRedisSerializer for the given target JavaType.
      Parameters:
      mapper - must not be null.
      javaType - must not be null.
      Since:
      3.0
    • Jackson2JsonRedisSerializer

      public Jackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.databind.JavaType javaType, JacksonObjectReader reader, JacksonObjectWriter writer)
      Creates a new Jackson2JsonRedisSerializer for the given target JavaType.
      Parameters:
      mapper - must not be null.
      javaType - must not be null.
      reader - the JacksonObjectReader function to read objects using ObjectMapper.
      writer - the JacksonObjectWriter function to write objects using ObjectMapper.
      Since:
      3.0
  • Method Details

    • setObjectMapper

      @Deprecated(since="3.0", forRemoval=true) public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 3.0, use constructor creation to configure the object mapper.
      Sets the ObjectMapper for this view. If not set, a default ObjectMapper is used.

      Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. For example, an extended SerializerFactory can be configured that provides custom serializers for specific types. The other option for refining the serialization process is to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.

    • serialize

      public byte[] serialize(@Nullable T value) throws SerializationException
      Description copied from interface: RedisSerializer
      Serialize the given object to binary data.
      Specified by:
      serialize in interface RedisSerializer<T>
      Parameters:
      value - object to serialize. Can be null.
      Returns:
      the equivalent binary data. Can be null.
      Throws:
      SerializationException
    • deserialize

      public T deserialize(@Nullable byte[] bytes) throws SerializationException
      Description copied from interface: RedisSerializer
      Deserialize an object from the given binary data.
      Specified by:
      deserialize in interface RedisSerializer<T>
      Parameters:
      bytes - object binary representation. Can be null.
      Returns:
      the equivalent object instance. Can be null.
      Throws:
      SerializationException
    • getJavaType

      protected com.fasterxml.jackson.databind.JavaType getJavaType(Class<?> clazz)
      Returns the Jackson JavaType for 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