Interface RedisSerializer<T>

All Known Implementing Classes:
GenericJackson2JsonRedisSerializer, GenericToStringSerializer, Jackson2JsonRedisSerializer, JdkSerializationRedisSerializer, OxmSerializer, StringRedisSerializer

public interface RedisSerializer<T>
Basic interface serialization and deserialization of Objects to byte arrays (binary data). It is recommended that implementations are designed to handle null objects/empty arrays on serialization and deserialization side. Note that Redis does not accept null keys or values but can return null replies (for non-existing keys).
Author:
Mark Pollack, Costin Leau, Christoph Strobl
  • Method Details

    • java

      static RedisSerializer<Object> java()
      Obtain a RedisSerializer using java serialization. Note: Ensure that your domain objects are actually serializable.
      Returns:
      never null.
      Since:
      2.1
    • java

      static RedisSerializer<Object> java(@Nullable ClassLoader classLoader)
      Obtain a RedisSerializer using java serialization with the given ClassLoader. Note: Ensure that your domain objects are actually serializable.
      Parameters:
      classLoader - the ClassLoader to use for deserialization. Can be null.
      Returns:
      new instance of RedisSerializer. Never null.
      Since:
      2.1
    • json

      static RedisSerializer<Object> json()
      Obtain a RedisSerializer that can read and write JSON using Jackson.
      Returns:
      never null.
      Since:
      2.1
    • string

      static RedisSerializer<String> string()
      Obtain a simple String to byte[] (and back) serializer using UTF-8 as the default Charset.
      Returns:
      never null.
      Since:
      2.1
    • byteArray

      static RedisSerializer<byte[]> byteArray()
      Obtain a RedisSerializer that passes thru byte[].
      Returns:
      never null.
      Since:
      2.2
    • serialize

      @Nullable byte[] serialize(@Nullable T value) throws SerializationException
      Serialize the given object to binary data.
      Parameters:
      value - object to serialize. Can be null.
      Returns:
      the equivalent binary data. Can be null.
      Throws:
      SerializationException
    • deserialize

      @Nullable T deserialize(@Nullable byte[] bytes) throws SerializationException
      Deserialize an object from the given binary data.
      Parameters:
      bytes - object binary representation. Can be null.
      Returns:
      the equivalent object instance. Can be null.
      Throws:
      SerializationException
    • canSerialize

      default boolean canSerialize(Class<?> type)
      Check whether the given value type can be serialized by this serializer.
      Parameters:
      type - the value type.
      Returns:
      true if the value type can be serialized; false otherwise.
    • getTargetType

      default Class<?> getTargetType()
      Return the serializer target type.
      Returns:
      the serializer target type.