Skip to content

Commit

Permalink
Fix PojoUtils support localdatetime,lcaldate,localtime serializable a…
Browse files Browse the repository at this point in the history
…nd deserialize (#10632)
  • Loading branch information
wuwen5 authored Sep 22, 2022
1 parent 688177f commit c676fb9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,6 +137,10 @@ private static Object generalize(Object pojo, Map<Object, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit c676fb9

Please sign in to comment.