Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10631 Fix PojoUtils support localdatetime,lcaldate,localtime serializable and deserialize #10632

Merged
merged 1 commit into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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