40
40
import java .util .concurrent .atomic .AtomicReference ;
41
41
import java .util .function .Consumer ;
42
42
43
+ import com .fasterxml .jackson .core .JsonGenerator ;
44
+ import com .fasterxml .jackson .databind .SerializerProvider ;
45
+ import com .fasterxml .jackson .databind .jsontype .TypeSerializer ;
46
+ import com .fasterxml .jackson .databind .ser .std .StdSerializer ;
43
47
import org .junit .jupiter .api .Test ;
44
48
import org .mockito .Mockito ;
45
49
@@ -420,7 +424,7 @@ void deserializesJavaTimeFrimBytes() {
420
424
}
421
425
422
426
@ Test // GH-2601
423
- public void internalObjectMapperCustomization () {
427
+ void internalObjectMapperCustomization () {
424
428
425
429
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer ();
426
430
@@ -438,14 +442,49 @@ public void internalObjectMapperCustomization() {
438
442
}
439
443
440
444
@ Test // GH-2601
441
- public void configureWithNullConsumerThrowsIllegalArgumentException () {
445
+ void configureWithNullConsumerThrowsIllegalArgumentException () {
442
446
443
447
assertThatIllegalArgumentException ()
444
448
.isThrownBy (() -> new GenericJackson2JsonRedisSerializer ().configure (null ))
445
449
.withMessage ("Consumer used to configure and customize ObjectMapper must not be null" )
446
450
.withNoCause ();
447
451
}
448
452
453
+ @ Test
454
+ void customSerializeAndDeserializeNullValue () {
455
+
456
+ GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer (
457
+ null ,
458
+ (mapper , source , type ) -> {
459
+ if (type .getRawClass () == User .class ) {
460
+ return mapper .readerWithView (Views .Basic .class ).forType (type ).readValue (source );
461
+ }
462
+ return mapper .readValue (source , type );
463
+ }, JacksonObjectWriter .create (), new StdSerializer <>(NullValue .class ) {
464
+ @ Override
465
+ public void serialize (NullValue nullValue ,
466
+ JsonGenerator jsonGenerator ,
467
+ SerializerProvider serializerProvider ) throws IOException {
468
+ jsonGenerator .writeNull ();
469
+ }
470
+
471
+ @ Override
472
+ public void serializeWithType (NullValue value , JsonGenerator jsonGenerator , SerializerProvider serializers ,
473
+ TypeSerializer typeSerializer ) throws IOException {
474
+
475
+ serialize (value , jsonGenerator , serializers );
476
+ }
477
+ });
478
+
479
+ NullValue nv = BeanUtils .instantiateClass (NullValue .class );
480
+
481
+ byte [] serializedValue = serializer .serialize (nv );
482
+ assertThat (serializedValue ).isNotNull ();
483
+
484
+ Object deserializedValue = serializer .deserialize (serializedValue );
485
+ assertThat (deserializedValue ).isNull ();
486
+ }
487
+
449
488
private static void serializeAndDeserializeNullValue (GenericJackson2JsonRedisSerializer serializer ) {
450
489
451
490
NullValue nv = BeanUtils .instantiateClass (NullValue .class );
0 commit comments