From c676fb9b72c016c4f93fef5d773e032d74043693 Mon Sep 17 00:00:00 2001 From: wuwen Date: Thu, 22 Sep 2022 15:43:04 +0800 Subject: [PATCH] Fix PojoUtils support localdatetime,lcaldate,localtime serializable and deserialize (#10632) --- .../common/utils/CompatibleTypeUtils.java | 13 ++++++++++++- .../apache/dubbo/common/utils/PojoUtils.java | 7 +++++++ .../dubbo/common/utils/PojoUtilsTest.java | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java index c73a976cabe..c2cbd395ea1 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java @@ -22,6 +22,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -33,6 +34,11 @@ public class CompatibleTypeUtils { private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + /** + * the text to parse such as "2007-12-03T10:15:30" + */ + private static final int ISO_LOCAL_DATE_TIME_MIN_LEN = 19; + private CompatibleTypeUtils() { } @@ -127,7 +133,12 @@ public static Object compatibleTypeConvert(Object value, Class type) { if (StringUtils.isEmpty(string)) { return null; } - return LocalDateTime.parse(string).toLocalTime(); + + if (string.length() >= ISO_LOCAL_DATE_TIME_MIN_LEN) { + return LocalDateTime.parse(string).toLocalTime(); + } else { + return LocalTime.parse(string); + } } if (type == Class.class) { try { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index a6eb767b340..d23a4b5ea7a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -33,6 +33,9 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -134,6 +137,10 @@ private static Object generalize(Object pojo, Map history) { if (ReflectUtils.isPrimitives(pojo.getClass())) { return pojo; } + + if (pojo instanceof LocalDate || pojo instanceof LocalDateTime || pojo instanceof LocalTime) { + return pojo.toString(); + } if (pojo instanceof Class) { return ((Class) pojo).getName(); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java index ae0f549d2ac..8aaf2b96a0a 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java @@ -32,6 +32,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -792,6 +795,22 @@ public void testMapToPojo() throws Exception { assertEquals(parent.getFeatures().get("height"), "177cm"); } + @Test + public void testJava8Time() { + + Object localDateTimeGen = PojoUtils.generalize(LocalDateTime.now()); + Object localDateTime = PojoUtils.realize(localDateTimeGen, LocalDateTime.class); + assertEquals(localDateTimeGen, localDateTime.toString()); + + Object localDateGen = PojoUtils.generalize(LocalDate.now()); + Object localDate = PojoUtils.realize(localDateGen, LocalDate.class); + assertEquals(localDateGen, localDate.toString()); + + Object localTimeGen = PojoUtils.generalize(LocalTime.now()); + Object localTime = PojoUtils.realize(localTimeGen, LocalTime.class); + assertEquals(localTimeGen, localTime.toString()); + } + public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }