|
20 | 20 | import static org.springframework.test.util.ReflectionTestUtils.*; |
21 | 21 | import static org.springframework.util.ObjectUtils.*; |
22 | 22 |
|
| 23 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 24 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 25 | +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; |
| 26 | +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; |
23 | 27 | import lombok.Data; |
24 | 28 | import lombok.ToString; |
25 | 29 |
|
26 | 30 | import java.io.IOException; |
27 | | -import java.util.concurrent.atomic.AtomicReference; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.time.LocalDate; |
28 | 33 | import java.util.Map; |
| 34 | +import java.util.UUID; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
29 | 36 |
|
30 | 37 | import org.junit.jupiter.api.Test; |
31 | 38 | import org.mockito.Mockito; |
@@ -344,6 +351,61 @@ void includesTypingForWrappedObjectTypes() { |
344 | 351 | }); |
345 | 352 | } |
346 | 353 |
|
| 354 | + @Test // GH-2396 |
| 355 | + void verifySerializeUUIDIntoBytes() { |
| 356 | + |
| 357 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 358 | + |
| 359 | + UUID source = UUID.fromString("730145fe-324d-4fb1-b12f-60b89a045730"); |
| 360 | + assertThat(serializer.serialize(source)).isEqualTo(("\"" + source + "\"").getBytes(StandardCharsets.UTF_8)); |
| 361 | + } |
| 362 | + |
| 363 | + @Test // GH-2396 |
| 364 | + void deserializesUUIDFromBytes() { |
| 365 | + |
| 366 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 367 | + UUID deserializedUuid = serializer |
| 368 | + .deserialize("\"730145fe-324d-4fb1-b12f-60b89a045730\"".getBytes(StandardCharsets.UTF_8), UUID.class); |
| 369 | + |
| 370 | + assertThat(deserializedUuid).isEqualTo(UUID.fromString("730145fe-324d-4fb1-b12f-60b89a045730")); |
| 371 | + } |
| 372 | + |
| 373 | + @Test // GH-2396 |
| 374 | + void serializesEnumIntoBytes() { |
| 375 | + |
| 376 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 377 | + |
| 378 | + assertThat(serializer.serialize(EnumType.ONE)).isEqualTo(("\"ONE\"").getBytes(StandardCharsets.UTF_8)); |
| 379 | + } |
| 380 | + |
| 381 | + @Test // GH-2396 |
| 382 | + void deserializesEnumFromBytes() { |
| 383 | + |
| 384 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 385 | + |
| 386 | + assertThat(serializer.deserialize("\"TWO\"".getBytes(StandardCharsets.UTF_8), EnumType.class)).isEqualTo(EnumType.TWO); |
| 387 | + } |
| 388 | + |
| 389 | + @Test // GH-2396 |
| 390 | + void serializesJavaTimeIntoBytes() { |
| 391 | + |
| 392 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 393 | + |
| 394 | + WithJsr310 source = new WithJsr310(); |
| 395 | + source.myDate = java.time.LocalDate.of(2022,9,2); |
| 396 | + |
| 397 | + assertThat(serializer.serialize(source)).isEqualTo(("{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$WithJsr310\",\"myDate\":[2022,9,2]}").getBytes(StandardCharsets.UTF_8)); |
| 398 | + } |
| 399 | + |
| 400 | + @Test // GH-2396 |
| 401 | + void deserializesJavaTimeFrimBytes() { |
| 402 | + |
| 403 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 404 | + |
| 405 | + byte[] source = "{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$WithJsr310\",\"myDate\":[2022,9,2]}".getBytes(StandardCharsets.UTF_8); |
| 406 | + assertThat(serializer.deserialize(source, WithJsr310.class).myDate).isEqualTo(java.time.LocalDate.of(2022,9,2)); |
| 407 | + } |
| 408 | + |
347 | 409 | private static void serializeAndDeserializeNullValue(GenericJackson2JsonRedisSerializer serializer) { |
348 | 410 |
|
349 | 411 | NullValue nv = BeanUtils.instantiateClass(NullValue.class); |
@@ -464,4 +526,14 @@ static class WithWrapperTypes { |
464 | 526 | AtomicReference<Integer[]> primitiveArrayWrapper; |
465 | 527 | AtomicReference<SimpleObject> simpleObjectWrapper; |
466 | 528 | } |
| 529 | + |
| 530 | + enum EnumType { |
| 531 | + ONE, TWO |
| 532 | + } |
| 533 | + |
| 534 | + static class WithJsr310 { |
| 535 | + @JsonSerialize(using = LocalDateSerializer.class) |
| 536 | + @JsonDeserialize(using = LocalDateDeserializer.class) |
| 537 | + private LocalDate myDate; |
| 538 | + } |
467 | 539 | } |
0 commit comments