Skip to content

Commit

Permalink
Refactor GenericJackson2JsonRedisSerializer.
Browse files Browse the repository at this point in the history
* Cleanup and simpifly source code.
* Fix compiler warnings.
* Edit Javadoc.

Closes #2609
  • Loading branch information
jxblum committed Jun 14, 2023
1 parent 433bb89 commit 4da3bdf
Showing 1 changed file with 108 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package org.springframework.data.redis.serializer;

import java.io.IOException;
import java.io.Serial;
import java.util.Collections;
import java.util.function.Supplier;

import org.springframework.cache.support.NullValue;
import org.springframework.core.KotlinDetector;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -46,41 +48,66 @@
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
* Generic Jackson 2-based {@link RedisSerializer} that maps {@link Object objects} to JSON using dynamic typing.
* Generic Jackson 2-based {@link RedisSerializer} that maps {@link Object objects} to and from {@literal JSON}
* using dynamic typing.
* <p>
* JSON reading and writing can be customized by configuring {@link JacksonObjectReader} respective
* {@link JacksonObjectWriter}.
* {@literal JSON} reading and writing can be customized by configuring a {@link JacksonObjectReader}
* and {@link JacksonObjectWriter}.
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Mao Shuai
* @author John Blum
* @see org.springframework.data.redis.serializer.JacksonObjectReader
* @see org.springframework.data.redis.serializer.JacksonObjectWriter
* @see com.fasterxml.jackson.databind.ObjectMapper
* @since 1.6
*/
public class GenericJackson2JsonRedisSerializer implements RedisSerializer<Object> {

private final ObjectMapper mapper;
/**
* Register {@link NullValueSerializer} in the given {@link ObjectMapper} with an optional
* {@code classPropertyTypeName}. This method should be called by code that customizes
* {@link GenericJackson2JsonRedisSerializer} by providing an external {@link ObjectMapper}.
*
* @param objectMapper the object mapper to customize.
* @param classPropertyTypeName name of the type property. Defaults to {@code @class} if {@literal null}/empty.
* @since 2.2
*/
public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nullable String classPropertyTypeName) {

// Simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here
// since we need the type hint embedded for deserialization using the default typing feature.
objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName)));
}

private final JacksonObjectReader reader;

private final JacksonObjectWriter writer;

private final Lazy<Boolean> defaultTypingEnabled;

private final ObjectMapper mapper;

private final TypeResolver typeResolver;

/**
* Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing.
* Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
* default typing.
*/
public GenericJackson2JsonRedisSerializer() {
this((String) null);
}

/**
* Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
* given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
* {@link JsonTypeInfo.Id#CLASS} will be used.
* Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
* default typing using the given {@link String name}.
* <p>
* In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS}
* will be used.
*
* @param classPropertyTypeName name of the JSON property holding type information. Can be {@literal null}.
* @param classPropertyTypeName {@link String name} of the JSON property holding type information;
* can be {@literal null}.
* @see ObjectMapper#activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
* @see ObjectMapper#activateDefaultTyping(PolymorphicTypeValidator, DefaultTyping, As)
*/
Expand All @@ -89,13 +116,17 @@ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName
}

/**
* Creates {@link GenericJackson2JsonRedisSerializer} and configures {@link ObjectMapper} for default typing using the
* given {@literal name}. In case of an {@literal empty} or {@literal null} String the default
* {@link JsonTypeInfo.Id#CLASS} will be used.
* Creates {@link GenericJackson2JsonRedisSerializer} initialized with an {@link ObjectMapper} configured for
* default typing using the given {@link String name} along with the given, required {@link JacksonObjectReader}
* and {@link JacksonObjectWriter} used to read/write {@link Object Objects} de/serialized as JSON.
* <p>
* In case {@link String name} is {@literal empty} or {@literal null}, then {@link JsonTypeInfo.Id#CLASS}
* will be used.
*
* @param classPropertyTypeName name of the JSON property holding type information. Can be {@literal null}.
* @param reader the {@link JacksonObjectReader} function to read objects using {@link ObjectMapper}.
* @param writer the {@link JacksonObjectWriter} function to write objects using {@link ObjectMapper}.
* @param classPropertyTypeName {@link String name} of the JSON property holding type information;
* can be {@literal null}.
* @param reader {@link JacksonObjectReader} function to read objects using {@link ObjectMapper}.
* @param writer {@link JacksonObjectWriter} function to write objects using {@link ObjectMapper}.
* @see ObjectMapper#activateDefaultTypingAsProperty(PolymorphicTypeValidator, DefaultTyping, String)
* @see ObjectMapper#activateDefaultTyping(PolymorphicTypeValidator, DefaultTyping, As)
* @since 3.0
Expand All @@ -105,19 +136,17 @@ public GenericJackson2JsonRedisSerializer(@Nullable String classPropertyTypeName

this(new ObjectMapper(), reader, writer, classPropertyTypeName);

// simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
// the type hint embedded for deserialization using the default typing feature.
registerNullValueSerializer(mapper, classPropertyTypeName);
registerNullValueSerializer(this.mapper, classPropertyTypeName);

StdTypeResolverBuilder typer = new TypeResolverBuilder(DefaultTyping.EVERYTHING,
mapper.getPolymorphicTypeValidator());
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
StdTypeResolverBuilder typer = TypeResolverBuilder.forEverything(this.mapper)
.init(JsonTypeInfo.Id.CLASS, null)
.inclusion(JsonTypeInfo.As.PROPERTY);

if (StringUtils.hasText(classPropertyTypeName)) {
typer = typer.typeProperty(classPropertyTypeName);
}
mapper.setDefaultTyping(typer);

this.mapper.setDefaultTyping(typer);
}

/**
Expand All @@ -143,58 +172,34 @@ public GenericJackson2JsonRedisSerializer(ObjectMapper mapper) {
*/
public GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader,
JacksonObjectWriter writer) {

this(mapper, reader, writer, null);
}

private GenericJackson2JsonRedisSerializer(ObjectMapper mapper, JacksonObjectReader reader,
JacksonObjectWriter writer, @Nullable String typeHintPropertyName) {

Assert.notNull(mapper, "ObjectMapper must not be null");
Assert.notNull(reader, "Reader must not be null");
Assert.notNull(writer, "Writer must not be null");

this.mapper = mapper;
this.reader = reader;
this.writer = writer;
this.mapper = RedisAssertions.requireObject(mapper, "ObjectMapper must not be null");
this.reader = RedisAssertions.requireObject(reader, "Reader must not be null");
this.writer = RedisAssertions.requireObject(writer, "Writer must not be null");

this.defaultTypingEnabled = Lazy.of(() -> mapper.getSerializationConfig().getDefaultTyper(null) != null);
this.defaultTypingEnabled = Lazy.of(() -> mapper.getSerializationConfig()
.getDefaultTyper(null) != null);

Supplier<String> typeHintPropertyNameSupplier;

if (typeHintPropertyName == null) {
this.typeResolver = new TypeResolver(Lazy.of(mapper::getTypeFactory),
newTypeHintPropertyNameSupplier(mapper, typeHintPropertyName, this.defaultTypingEnabled));
}

typeHintPropertyNameSupplier = Lazy.of(() -> {
if (defaultTypingEnabled.get()) {
return null;
}
private Supplier<String> newTypeHintPropertyNameSupplier(ObjectMapper mapper, @Nullable String typeHintPropertyName,
Lazy<Boolean> defaultTypingEnabled) {

return mapper.getDeserializationConfig().getDefaultTyper(null)
return typeHintPropertyName != null ? () -> typeHintPropertyName
: Lazy.of(() -> defaultTypingEnabled.get() ? null
: mapper.getDeserializationConfig().getDefaultTyper(null)
.buildTypeDeserializer(mapper.getDeserializationConfig(),
mapper.getTypeFactory().constructType(Object.class), Collections.emptyList())
.getPropertyName();

}).or("@class");
} else {
typeHintPropertyNameSupplier = () -> typeHintPropertyName;
}

this.typeResolver = new TypeResolver(Lazy.of(mapper::getTypeFactory), typeHintPropertyNameSupplier);
}

/**
* Register {@link NullValueSerializer} in the given {@link ObjectMapper} with an optional
* {@code classPropertyTypeName}. This method should be called by code that customizes
* {@link GenericJackson2JsonRedisSerializer} by providing an external {@link ObjectMapper}.
*
* @param objectMapper the object mapper to customize.
* @param classPropertyTypeName name of the type property. Defaults to {@code @class} if {@literal null}/empty.
* @since 2.2
*/
public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nullable String classPropertyTypeName) {

// simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
// the type hint embedded for deserialization using the default typing feature.
objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName)));
.getPropertyName())
.or("@class");
}

@Override
Expand All @@ -206,8 +211,9 @@ public byte[] serialize(@Nullable Object source) throws SerializationException {

try {
return writer.write(mapper, source);
} catch (IOException e) {
throw new SerializationException("Could not write JSON: " + e.getMessage(), e);
} catch (IOException cause) {
String message = String.format("Could not write JSON: %s", cause.getMessage());
throw new SerializationException(message, cause);
}
}

Expand All @@ -217,17 +223,24 @@ public Object deserialize(@Nullable byte[] source) throws SerializationException
}

/**
* @param source can be {@literal null}.
* @param type must not be {@literal null}.
* @return {@literal null} for empty source.
* @throws SerializationException
* Deserialized the array of bytes containing {@literal JSON} as an {@link Object} of the given,
* required {@link Class type}.
*
* @param source array of bytes containing the {@literal JSON} to deserialize; can be {@literal null}.
* @param type {@link Class type} of {@link Object} from which the {@literal JSON} will be deserialized;
* must not be {@literal null}.
* @return {@literal null} for an empty source, or an {@link Object} of the given {@link Class type}
* deserialized from the array of bytes containing {@literal JSON}.
* @throws IllegalArgumentException if the given {@link Class type} is {@literal null}.
* @throws SerializationException if the array of bytes cannot be deserialized as an instance of
* the given {@link Class type}
*/
@Nullable
@SuppressWarnings("unchecked")
public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws SerializationException {

Assert.notNull(type,
"Deserialization type must not be null Please provide Object.class to make use of Jackson2 default typing.");
Assert.notNull(type, "Deserialization type must not be null;"
+ " Please provide Object.class to make use of Jackson2 default typing.");

if (SerializationUtils.isEmpty(source)) {
return null;
Expand Down Expand Up @@ -292,7 +305,9 @@ protected JavaType resolveType(byte[] source, Class<?> type) throws IOException
*/
private static class NullValueSerializer extends StdSerializer<NullValue> {

@Serial
private static final long serialVersionUID = 1999052150548658808L;

private final String classIdentifier;

/**
Expand All @@ -305,17 +320,19 @@ private static class NullValueSerializer extends StdSerializer<NullValue> {
}

@Override
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
public void serialize(NullValue value, JsonGenerator jsonGenerator, SerializerProvider provider)
throws IOException {

jgen.writeStartObject();
jgen.writeStringField(classIdentifier, NullValue.class.getName());
jgen.writeEndObject();
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField(classIdentifier, NullValue.class.getName());
jsonGenerator.writeEndObject();
}

@Override
public void serializeWithType(NullValue value, JsonGenerator gen, SerializerProvider serializers,
TypeSerializer typeSer) throws IOException {
serialize(value, gen, serializers);
public void serializeWithType(NullValue value, JsonGenerator jsonGenerator, SerializerProvider serializers,
TypeSerializer typeSerializer) throws IOException {

serialize(value, jsonGenerator, serializers);
}
}

Expand All @@ -329,8 +346,12 @@ public void serializeWithType(NullValue value, JsonGenerator gen, SerializerProv
*/
private static class TypeResolverBuilder extends ObjectMapper.DefaultTypeResolverBuilder {

public TypeResolverBuilder(DefaultTyping t, PolymorphicTypeValidator ptv) {
super(t, ptv);
static TypeResolverBuilder forEverything(ObjectMapper mapper) {
return new TypeResolverBuilder(DefaultTyping.EVERYTHING, mapper.getPolymorphicTypeValidator());
}

public TypeResolverBuilder(DefaultTyping typing, PolymorphicTypeValidator polymorphicTypeValidator) {
super(typing, polymorphicTypeValidator);
}

@Override
Expand All @@ -343,25 +364,25 @@ public ObjectMapper.DefaultTypeResolverBuilder withDefaultImpl(Class<?> defaultI
* Boolean, Integer, Double) will never use typing; that is both due to them being concrete and final, and since
* actual serializers and deserializers will also ignore any attempts to enforce typing.
*/
public boolean useForType(JavaType t) {
public boolean useForType(JavaType javaType) {

if (t.isJavaLangObject()) {
if (javaType.isJavaLangObject()) {
return true;
}

t = resolveArrayOrWrapper(t);
javaType = resolveArrayOrWrapper(javaType);

if (t.isEnumType() || ClassUtils.isPrimitiveOrWrapper(t.getRawClass())) {
if (javaType.isEnumType() || ClassUtils.isPrimitiveOrWrapper(javaType.getRawClass())) {
return false;
}

if (t.isFinal() && !KotlinDetector.isKotlinType(t.getRawClass())
&& t.getRawClass().getPackageName().startsWith("java")) {
if (javaType.isFinal() && !KotlinDetector.isKotlinType(javaType.getRawClass())
&& javaType.getRawClass().getPackageName().startsWith("java")) {
return false;
}

// [databind#88] Should not apply to JSON tree models:
return !TreeNode.class.isAssignableFrom(t.getRawClass());
return !TreeNode.class.isAssignableFrom(javaType.getRawClass());
}

private JavaType resolveArrayOrWrapper(JavaType type) {
Expand Down

0 comments on commit 4da3bdf

Please sign in to comment.