Skip to content

Commit 28dee8e

Browse files
committed
feat: add setter method to define custom NullValueSerializer
1 parent 3ab09fb commit 28dee8e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.function.Consumer;
2222
import java.util.function.Supplier;
2323

24+
import com.fasterxml.jackson.databind.JsonSerializer;
2425
import org.springframework.cache.support.NullValue;
2526
import org.springframework.core.KotlinDetector;
2627
import org.springframework.data.util.Lazy;
@@ -231,6 +232,10 @@ public static void registerNullValueSerializer(ObjectMapper objectMapper, @Nulla
231232
objectMapper.registerModule(new SimpleModule().addSerializer(new NullValueSerializer(classPropertyTypeName)));
232233
}
233234

235+
public void registerCustomNullValueSerializer(JsonSerializer<?> nullValueSerializer) {
236+
this.mapper.registerModule(new SimpleModule().addSerializer(nullValueSerializer));
237+
}
238+
234239
/**
235240
* Gets the configured {@link ObjectMapper} used internally by this {@link GenericJackson2JsonRedisSerializer} to
236241
* de/serialize {@link Object objects} as {@literal JSON}.

src/test/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializerUnitTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import java.util.concurrent.atomic.AtomicReference;
4141
import java.util.function.Consumer;
4242

43+
import com.fasterxml.jackson.core.JsonGenerator;
44+
import com.fasterxml.jackson.databind.SerializerProvider;
45+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
4346
import org.junit.jupiter.api.Test;
4447
import org.mockito.Mockito;
4548

@@ -420,7 +423,7 @@ void deserializesJavaTimeFrimBytes() {
420423
}
421424

422425
@Test // GH-2601
423-
public void internalObjectMapperCustomization() {
426+
void internalObjectMapperCustomization() {
424427

425428
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
426429

@@ -438,14 +441,34 @@ public void internalObjectMapperCustomization() {
438441
}
439442

440443
@Test // GH-2601
441-
public void configureWithNullConsumerThrowsIllegalArgumentException() {
444+
void configureWithNullConsumerThrowsIllegalArgumentException() {
442445

443446
assertThatIllegalArgumentException()
444447
.isThrownBy(() -> new GenericJackson2JsonRedisSerializer().configure(null))
445448
.withMessage("Consumer used to configure and customize ObjectMapper must not be null")
446449
.withNoCause();
447450
}
448451

452+
@Test
453+
void customSerializeAndDeserializeNullValue() {
454+
455+
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
456+
String expected = "null";
457+
458+
serializer.registerCustomNullValueSerializer(new StdSerializer<>(String.class) {
459+
@Override
460+
public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
461+
gen.writeRawValue(expected);
462+
}
463+
});
464+
465+
byte[] serializedValue = serializer.serialize(expected);
466+
assertThat(serializedValue).isNotNull();
467+
468+
Object deserializedValue = serializer.deserialize(serializedValue);
469+
assertThat(deserializedValue).isNull();
470+
}
471+
449472
private static void serializeAndDeserializeNullValue(GenericJackson2JsonRedisSerializer serializer) {
450473

451474
NullValue nv = BeanUtils.instantiateClass(NullValue.class);

0 commit comments

Comments
 (0)