|
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 |
|
25 | 29 | import java.io.IOException;
|
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.time.LocalDate; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.UUID; |
26 | 34 | import java.util.concurrent.atomic.AtomicReference;
|
27 | 35 |
|
28 | 36 | import org.junit.jupiter.api.Test;
|
@@ -261,6 +269,65 @@ void includesTypingForWrappedObjectTypes() {
|
261 | 269 | });
|
262 | 270 | }
|
263 | 271 |
|
| 272 | + @Test // GH-2396 |
| 273 | + void verifySerializeUUIDIntoBytes() { |
| 274 | + |
| 275 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 276 | + |
| 277 | + UUID source = UUID.fromString("730145fe-324d-4fb1-b12f-60b89a045730"); |
| 278 | + assertThat(serializer.serialize(source)).isEqualTo(("\"" + source + "\"").getBytes(StandardCharsets.UTF_8)); |
| 279 | + } |
| 280 | + |
| 281 | + @Test // GH-2396 |
| 282 | + void deserializesUUIDFromBytes() { |
| 283 | + |
| 284 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 285 | + UUID deserializedUuid = serializer |
| 286 | + .deserialize("\"730145fe-324d-4fb1-b12f-60b89a045730\"".getBytes(StandardCharsets.UTF_8), UUID.class); |
| 287 | + |
| 288 | + assertThat(deserializedUuid).isEqualTo(UUID.fromString("730145fe-324d-4fb1-b12f-60b89a045730")); |
| 289 | + } |
| 290 | + |
| 291 | + @Test // GH-2396 |
| 292 | + void serializesEnumIntoBytes() { |
| 293 | + |
| 294 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 295 | + |
| 296 | + assertThat(serializer.serialize(EnumType.ONE)).isEqualTo(("\"ONE\"").getBytes(StandardCharsets.UTF_8)); |
| 297 | + } |
| 298 | + |
| 299 | + @Test // GH-2396 |
| 300 | + void deserializesEnumFromBytes() { |
| 301 | + |
| 302 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 303 | + |
| 304 | + assertThat(serializer.deserialize("\"TWO\"".getBytes(StandardCharsets.UTF_8), EnumType.class)) |
| 305 | + .isEqualTo(EnumType.TWO); |
| 306 | + } |
| 307 | + |
| 308 | + @Test // GH-2396 |
| 309 | + void serializesJavaTimeIntoBytes() { |
| 310 | + |
| 311 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 312 | + |
| 313 | + WithJsr310 source = new WithJsr310(); |
| 314 | + source.myDate = java.time.LocalDate.of(2022, 9, 2); |
| 315 | + |
| 316 | + assertThat(serializer.serialize(source)).isEqualTo( |
| 317 | + ("{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$WithJsr310\",\"myDate\":[2022,9,2]}") |
| 318 | + .getBytes(StandardCharsets.UTF_8)); |
| 319 | + } |
| 320 | + |
| 321 | + @Test // GH-2396 |
| 322 | + void deserializesJavaTimeFrimBytes() { |
| 323 | + |
| 324 | + GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
| 325 | + |
| 326 | + byte[] source = "{\"@class\":\"org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializerUnitTests$WithJsr310\",\"myDate\":[2022,9,2]}" |
| 327 | + .getBytes(StandardCharsets.UTF_8); |
| 328 | + assertThat(serializer.deserialize(source, WithJsr310.class).myDate).isEqualTo(java.time.LocalDate.of(2022, 9, 2)); |
| 329 | + } |
| 330 | + |
264 | 331 | private static void serializeAndDeserializeNullValue(GenericJackson2JsonRedisSerializer serializer) {
|
265 | 332 |
|
266 | 333 | NullValue nv = BeanUtils.instantiateClass(NullValue.class);
|
@@ -366,4 +433,13 @@ static class WithWrapperTypes {
|
366 | 433 | AtomicReference<Integer[]> primitiveArrayWrapper;
|
367 | 434 | AtomicReference<SimpleObject> simpleObjectWrapper;
|
368 | 435 | }
|
| 436 | + |
| 437 | + enum EnumType { |
| 438 | + ONE, TWO |
| 439 | + } |
| 440 | + |
| 441 | + static class WithJsr310 { |
| 442 | + @JsonSerialize(using = LocalDateSerializer.class) |
| 443 | + @JsonDeserialize(using = LocalDateDeserializer.class) private LocalDate myDate; |
| 444 | + } |
369 | 445 | }
|
0 commit comments