mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix LocalDateTime not support bug
This commit is contained in:
32
hutool-json/src/test/java/cn/hutool/json/Isse644Test.java
Normal file
32
hutool-json/src/test/java/cn/hutool/json/Isse644Test.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 问题反馈对象中有JDK8日期对象时转换失败,5.0.7修复
|
||||
*/
|
||||
public class Isse644Test {
|
||||
|
||||
@Test
|
||||
public void toBeanTest(){
|
||||
final BeanWithDate beanWithDate = new BeanWithDate();
|
||||
beanWithDate.setDate(LocalDateTime.now());
|
||||
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(beanWithDate);
|
||||
|
||||
BeanWithDate beanWithDate2 = JSONUtil.toBean(jsonObject, BeanWithDate.class);
|
||||
Assert.assertEquals(beanWithDate.getDate(), beanWithDate2.getDate());
|
||||
|
||||
beanWithDate2 = JSONUtil.toBean(jsonObject.toString(), BeanWithDate.class);
|
||||
Assert.assertEquals(beanWithDate.getDate(), beanWithDate2.getDate());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class BeanWithDate{
|
||||
private LocalDateTime date;
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package cn.hutool.json;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@@ -58,8 +59,8 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
public void toStringTest3() {
|
||||
JSONObject json = JSONUtil.createObj()//
|
||||
.put("dateTime", DateUtil.parse("2019-05-02 22:12:01"))//
|
||||
JSONObject json = Objects.requireNonNull(JSONUtil.createObj()//
|
||||
.put("dateTime", DateUtil.parse("2019-05-02 22:12:01")))//
|
||||
.setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("{\"dateTime\":\"2019-05-02\"}", json.toString());
|
||||
}
|
||||
@@ -67,9 +68,10 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void toStringWithDateTest() {
|
||||
JSONObject json = JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
assert json != null;
|
||||
Assert.assertEquals("{\"date\":1557314301000}", json.toString());
|
||||
|
||||
json = JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21")).setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
json = Objects.requireNonNull(JSONUtil.createObj().put("date", DateUtil.parse("2019-05-08 19:18:21"))).setDateFormat(DatePattern.NORM_DATE_PATTERN);
|
||||
Assert.assertEquals("{\"date\":\"2019-05-08\"}", json.toString());
|
||||
}
|
||||
|
||||
@@ -131,6 +133,7 @@ public class JSONObjectTest {
|
||||
Console.log(json2);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
JSONObject subJson = JSONUtil.createObj().put("value1", "strValue1").put("value2", "234");
|
||||
@@ -149,6 +152,7 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals(TestEnum.TYPE_A, bean.getTestEnum());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void toBeanNullStrTest() {
|
||||
JSONObject json = JSONUtil.createObj()//
|
||||
@@ -217,6 +221,7 @@ public class JSONObjectTest {
|
||||
/**
|
||||
* 在JSON转Bean过程中,Bean中字段如果为父类定义的泛型类型,则应正确转换,此方法用于测试这类情况
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void toBeanTest6() {
|
||||
JSONObject json = JSONUtil.createObj().put("targetUrl", "http://test.com").put("success", "true").put("result", JSONUtil.createObj().put("token", "tokenTest").put("userId", "测试用户1"));
|
||||
@@ -271,6 +276,7 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals(bean.toString(), bean2.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void parseBeanTest3() {
|
||||
JSONObject json = JSONUtil.createObj().put("code", 22).put("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
|
||||
@@ -310,6 +316,7 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals(DateUtil.parse("2018-10-25"), bean.getDate());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void beanTransTest3() {
|
||||
JSONObject userAJson = JSONUtil.createObj().put("a", "AValue").put("name", "nameValue").put("date", "08:00:00");
|
||||
|
Reference in New Issue
Block a user