Class GenericJacksonJsonRedisSerializer
java.lang.Object
org.springframework.data.redis.serializer.GenericJacksonJsonRedisSerializer
- All Implemented Interfaces:
RedisJsonSerializer, RedisSerializer<Object>
Generic Jackson 3-based
RedisSerializer that maps objects to and from JSON.
JSON reading and writing can be customized by configuring a JacksonObjectReader and
JacksonObjectWriter.
- Since:
- 4.0
- Author:
- Christoph Strobl, Moritz Halbritter
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<B extends MapperBuilder<? extends ObjectMapper, ? extends MapperBuilder<?,?>>> GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilderwraps around aJsonMapper.Builderproviding dedicated methods to configure aspects likeNullValueserialization strategy for the resultingObjectMapperto be used withGenericJacksonJsonRedisSerializeras well as potential Object/-readerand-writersettings. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreate aGenericJacksonJsonRedisSerializerwith a custom-configuredObjectMapper.protectedGenericJacksonJsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader, JacksonObjectWriter writer) Create aGenericJacksonJsonRedisSerializerwith a custom-configuredObjectMapperconsidering potential Object/-readerand-writer. -
Method Summary
Modifier and TypeMethodDescriptionstatic GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<JsonMapper.Builder> builder()Creates a newGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilderto configure and build aGenericJacksonJsonRedisSerializerusingJsonMapper.static <B extends MapperBuilder<? extends ObjectMapper, ? extends MapperBuilder<?,?>>>
GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<B> Creates a newGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilderto configure and build aGenericJacksonJsonRedisSerializer.create(Consumer<GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<JsonMapper.Builder>> configurer) Prepare a newGenericJacksonJsonRedisSerializerinstance.deserialize(byte @Nullable [] source) Deserialize an object from the given binary data.<T> @Nullable Tdeserialize(byte @Nullable [] source, Class<T> type) deserialize(byte @Nullable [] source, ResolvableType type) Deserialize the givensourceinto an instance of the type described byResolvableType.protected JavaTyperesolveType(byte[] source, Class<?> type) byte[]Serialize the given object to binary data.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface RedisJsonSerializer
deserializeMethods inherited from interface RedisSerializer
canSerialize, getTargetType
-
Constructor Details
-
GenericJacksonJsonRedisSerializer
Create aGenericJacksonJsonRedisSerializerwith a custom-configuredObjectMapper.- Parameters:
mapper- must not be null.
-
GenericJacksonJsonRedisSerializer
protected GenericJacksonJsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader, JacksonObjectWriter writer) Create aGenericJacksonJsonRedisSerializerwith a custom-configuredObjectMapperconsidering potential Object/-readerand-writer.- Parameters:
mapper- must not be null.reader- theJacksonObjectReaderfunction to read objects usingObjectMapper.writer- theJacksonObjectWriterfunction to write objects usingObjectMapper.
-
-
Method Details
-
create
public static GenericJacksonJsonRedisSerializer create(Consumer<GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<JsonMapper.Builder>> configurer) Prepare a newGenericJacksonJsonRedisSerializerinstance.- Parameters:
configurer- the configurer forGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder.- Returns:
- new instance of
GenericJacksonJsonRedisSerializer.
-
builder
public static GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<JsonMapper.Builder> builder()Creates a newGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilderto configure and build aGenericJacksonJsonRedisSerializerusingJsonMapper. -
builder
public static <B extends MapperBuilder<? extends ObjectMapper, ? extends MapperBuilder<?,?>>> GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder<B> builder(Supplier<B> builderFactory) Creates a newGenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilderto configure and build aGenericJacksonJsonRedisSerializer.- Type Parameters:
B- type of theMapperBuilderto use.- Parameters:
builderFactory- factory to create aMapperBuilderfor theObjectMapper.- Returns:
- a new
GenericJacksonJsonRedisSerializer.GenericJacksonJsonRedisSerializerBuilder.
-
serialize
@Contract("_ -> !null") public byte[] serialize(@Nullable Object value) throws SerializationException Description copied from interface:RedisSerializerSerialize the given object to binary data.- Specified by:
serializein interfaceRedisSerializer<Object>- Parameters:
value- object to serialize. Can be null.- Returns:
- the equivalent binary data. Can be an empty array but never null.
- Throws:
SerializationException
-
deserialize
@Contract("null -> null") public @Nullable Object deserialize(byte @Nullable [] source) throws SerializationException Description copied from interface:RedisSerializerDeserialize an object from the given binary data.- Specified by:
deserializein interfaceRedisSerializer<Object>- Parameters:
source- object binary representation. Can be null.- Returns:
- the equivalent object instance. Can be null.
- Throws:
SerializationException
-
deserialize
@Contract("null, _ -> null") public <T> @Nullable T deserialize(byte @Nullable [] source, Class<T> type) throws SerializationException - Specified by:
deserializein interfaceRedisSerializer<Object>- Parameters:
source- array of bytes containing the JSON to deserialize; can be null.type-typeofObjectfrom which the JSON will be deserialized; must not be null.- Returns:
- null for an empty source, or an
Objectof the giventypedeserialized from the array of bytes containing JSON. - Throws:
IllegalArgumentException- if the giventypeis null.SerializationException- if the array of bytes cannot be deserialized as an instance of the giventype
-
deserialize
public @Nullable Object deserialize(byte @Nullable [] source, ResolvableType type) throws SerializationException Description copied from interface:RedisJsonSerializerDeserialize the givensourceinto an instance of the type described byResolvableType. Use this variant for generic types such asList<Person>that cannot be expressed as aClass.- Specified by:
deserializein interfaceRedisJsonSerializer- Parameters:
source- the JSON representation to read.type- reference describing the target type.- Returns:
- the deserialized object, or null if
sourceis empty or represents JSON null. - Throws:
SerializationException- if the JSON cannot be deserialized.
-
resolveType
- Throws:
IOException
-