Skip to content

Commit

Permalink
toJavaObject support LocalDateTime, for issue #3091
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 23, 2024
1 parent ca1d527 commit 2b0efee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,10 @@ public static <T> T cast(Object obj, Class<T> targetClass, ObjectReaderProvider
return (T) JdbcSupport.createTimestamp(millis);
case "java.sql.Time":
return (T) JdbcSupport.createTime(millis);
case "java.time.LocalDateTime":
return (T) LocalDateTime.ofInstant(
Instant.ofEpochMilli(millis),
DateUtils.DEFAULT_ZONE_ID);
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.fastjson2.issues_3000;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue3091 {
@Test
public void test() {
String json = "{\"time\":0}";
JSONObject jsonObject = JSONObject.parseObject(json);
Time2 bean = jsonObject.toJavaObject(Time2.class);
assertNotNull(bean.time);
assertEquals(1970, bean.time.getYear());
}

@Data
public static class Time2 {
private LocalDateTime time;
}
}

0 comments on commit 2b0efee

Please sign in to comment.