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:
@@ -2,6 +2,7 @@ package cn.hutool.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
@@ -43,12 +44,12 @@ final class InternalJSONUtil {
|
||||
} else if (value instanceof JSON) {
|
||||
((JSON) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Map) {
|
||||
new JSONObject((Map<?, ?>) value).write(writer, indentFactor, indent);
|
||||
new JSONObject(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Iterable || value instanceof Iterator || value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
writer.write(NumberUtil.toStr((Number) value));
|
||||
} else if (value instanceof Date || value instanceof Calendar) {
|
||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||
final String format = (null == config) ? null : config.getDateFormat();
|
||||
writer.write(formatDate(value, format));
|
||||
} else if (value instanceof Boolean) {
|
||||
@@ -138,7 +139,6 @@ final class InternalJSONUtil {
|
||||
* @return A simple JSON value.
|
||||
*/
|
||||
protected static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if (null == string || "null".equalsIgnoreCase(string)) {
|
||||
return JSONNull.NULL;
|
||||
}
|
||||
@@ -158,8 +158,8 @@ final class InternalJSONUtil {
|
||||
if ((b >= '0' && b <= '9') || b == '-') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1 || string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
double d = Double.parseDouble(string);
|
||||
if (false == Double.isInfinite(d) && false == Double.isNaN(d)) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
@@ -228,12 +228,21 @@ final class InternalJSONUtil {
|
||||
*/
|
||||
private static String formatDate(Object dateObj, String format) {
|
||||
if (StrUtil.isNotBlank(format)) {
|
||||
final Date date = (dateObj instanceof Date) ? (Date) dateObj : ((Calendar) dateObj).getTime();
|
||||
//用户定义了日期格式
|
||||
return JSONUtil.quote(DateUtil.format(date, format));
|
||||
return JSONUtil.quote(DateUtil.format(Convert.toDate(dateObj), format));
|
||||
}
|
||||
|
||||
//默认使用时间戳
|
||||
return String.valueOf((dateObj instanceof Date) ? ((Date) dateObj).getTime() : ((Calendar) dateObj).getTimeInMillis());
|
||||
long timeMillis;
|
||||
if(dateObj instanceof TemporalAccessor){
|
||||
timeMillis = DateUtil.toInstant((TemporalAccessor)dateObj).toEpochMilli();
|
||||
} else if(dateObj instanceof Date){
|
||||
timeMillis = ((Date) dateObj).getTime();
|
||||
} else if(dateObj instanceof Calendar){
|
||||
timeMillis = ((Calendar) dateObj).getTimeInMillis();
|
||||
} else{
|
||||
throw new UnsupportedOperationException("Unsupported Date type: " + dateObj.getClass());
|
||||
}
|
||||
return String.valueOf(timeMillis);
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,8 @@ import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
@@ -650,7 +652,10 @@ public final class JSONUtil {
|
||||
}
|
||||
|
||||
// 日期类型原样保存,便于格式化
|
||||
if (object instanceof Date || object instanceof Calendar) {
|
||||
if (object instanceof Date
|
||||
|| object instanceof Calendar
|
||||
|| object instanceof TemporalAccessor
|
||||
) {
|
||||
return object;
|
||||
}
|
||||
// 枚举类保存其字符串形式(4.0.2新增)
|
||||
|
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