Class GenericJackson2JsonRedisSerializer

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

public class GenericJackson2JsonRedisSerializer extends Object implements 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
See Also:
  • Constructor Details

    • GenericJackson2JsonRedisSerializer

      public GenericJackson2JsonRedisSerializer()
      Creates GenericJackson2JsonRedisSerializer initialized with an ObjectMapper configured for default typing.
    • GenericJackson2JsonRedisSerializer

      public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName)
      Creates GenericJackson2JsonRedisSerializer initialized with an ObjectMapper configured for default typing using the given name.

      In case name is empty or null, then JsonTypeInfo.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)
      Creates GenericJackson2JsonRedisSerializer initialized with an ObjectMapper configured for default typing using the given name along with the given, required JacksonObjectReader and JacksonObjectWriter used to read/write Objects de/serialized as JSON.

      In case name is empty or null, then JsonTypeInfo.Id.CLASS will be used.

      Parameters:
      classPropertyTypeName - name of the JSON property holding type information; can be null.
      reader - JacksonObjectReader function to read objects using ObjectMapper.
      writer - JacksonObjectWriter function to write objects using ObjectMapper.
      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-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.
      Parameters:
      mapper - must not be null.
    • GenericJackson2JsonRedisSerializer

      public GenericJackson2JsonRedisSerializer(com.fasterxml.jackson.databind.ObjectMapper mapper, JacksonObjectReader reader, JacksonObjectWriter writer)
      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.
      Parameters:
      mapper - 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

    • registerNullValueSerializer

      public static void registerNullValueSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, @Nullable String classPropertyTypeName)
      Register GenericJackson2JsonRedisSerializer.NullValueSerializer in the given ObjectMapper with an optional classPropertyTypeName. This method should be called by code that customizes GenericJackson2JsonRedisSerializer by providing an external ObjectMapper.
      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 configured ObjectMapper used internally by this GenericJackson2JsonRedisSerializer to de/serialize objects as JSON.
      Returns:
      the configured ObjectMapper.
    • serialize

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

      public Object deserialize(@Nullable byte[] source) throws SerializationException
      Description copied from interface: RedisSerializer
      Deserialize an object from the given binary data.
      Specified by:
      deserialize in interface RedisSerializer<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
      Deserialized the array of bytes containing JSON as an Object of the given, required type.
      Parameters:
      source - array of bytes containing the JSON to deserialize; can be null.
      type - type of Object from which the JSON will be deserialized; must not be null.
      Returns:
      null for an empty source, or an Object of the given type deserialized from the array of bytes containing JSON.
      Throws:
      IllegalArgumentException - if the given type is null.
      SerializationException - if the array of bytes cannot be deserialized as an instance of the given type
    • configure

      public GenericJackson2JsonRedisSerializer configure(Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperConfigurer)
      Builder method used to configure and customize the internal Jackson ObjectMapper created by this GenericJackson2JsonRedisSerializer and used to de/serialize objects as JSON.
      Parameters:
      objectMapperConfigurer - Consumer used to configure and customize the internal ObjectMapper; must not be null.
      Returns:
      this GenericJackson2JsonRedisSerializer.
      Throws:
      IllegalArgumentException - if the Consumer used to configure and customize the internal ObjectMapper is null.
      Since:
      3.1.5
    • resolveType

      protected com.fasterxml.jackson.databind.JavaType resolveType(byte[] source, Class<?> type) throws IOException
      Throws:
      IOException