Class Jackson3JsonRedisSerializer<T>

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

public class Jackson3JsonRedisSerializer<T> extends Object implements 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 Jackson3ObjectReader respective Jackson3ObjectWriter.

Since:
4.0
Author:
Christoph Strobl, Thomas Darimont, Mark Paluch
  • Constructor Details

    • Jackson3JsonRedisSerializer

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

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

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

      public Jackson3JsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType)
      Creates a new Jackson3JsonRedisSerializer for the given target JavaType.
      Parameters:
      mapper - must not be null.
      javaType - must not be null.
    • Jackson3JsonRedisSerializer

      public Jackson3JsonRedisSerializer(tools.jackson.databind.ObjectMapper mapper, tools.jackson.databind.JavaType javaType, Jackson3ObjectReader reader, Jackson3ObjectWriter writer)
      Creates a new Jackson3JsonRedisSerializer for the given target JavaType.
      Parameters:
      mapper - must not be null.
      javaType - must not be null.
      reader - the Jackson3ObjectReader function to read objects using ObjectMapper.
      writer - the Jackson3ObjectWriter function to write objects using ObjectMapper.
  • Method Details

    • 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 an empty array but never null.
      Throws:
      SerializationException
    • deserialize

      public @Nullable T deserialize(byte @Nullable [] 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 tools.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