mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add /GlobalCustomFormat
This commit is contained in:
@@ -3,6 +3,7 @@ package cn.hutool.json;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TemporalAccessorUtil;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
@@ -167,10 +168,6 @@ final class InternalJSONUtil {
|
||||
try {
|
||||
if (StrUtil.containsAnyIgnoreCase(string, ".", "e")) {
|
||||
// pr#192@Gitee,Double会出现小数精度丢失问题,此处使用BigDecimal
|
||||
//double d = Double.parseDouble(string);
|
||||
//if (false == Double.isInfinite(d) && false == Double.isNaN(d)) {
|
||||
// return d;
|
||||
//}
|
||||
return new BigDecimal(string);
|
||||
} else {
|
||||
final long myLong = Long.parseLong(string);
|
||||
@@ -275,6 +272,12 @@ final class InternalJSONUtil {
|
||||
} else{
|
||||
dateStr = DateUtil.format(Convert.toDate(dateObj), format);
|
||||
}
|
||||
|
||||
if(GlobalCustomFormat.FORMAT_SECONDS.equals(format)
|
||||
|| GlobalCustomFormat.FORMAT_MILLISECONDS.equals(format)){
|
||||
// Hutool自定义的秒和毫秒表示,默认不包装双引号
|
||||
return dateStr;
|
||||
}
|
||||
//用户定义了日期格式
|
||||
return JSONUtil.quote(dateStr);
|
||||
}
|
||||
|
@@ -113,21 +113,25 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
|
||||
@Override
|
||||
default Date getDate(K key, Date defaultValue){
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof Date){
|
||||
return (Date) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = getStr(key);
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
return defaultValue;
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
}
|
||||
|
||||
|
@@ -450,6 +450,27 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals(DateUtil.beginOfDay(date), parse.getDate("date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setCustomDateFormatTest(){
|
||||
JSONConfig jsonConfig = JSONConfig.create();
|
||||
jsonConfig.setDateFormat("#sss");
|
||||
jsonConfig.setOrder(true);
|
||||
|
||||
Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
json.set("bbb", "222");
|
||||
json.set("aaa", "123");
|
||||
|
||||
String jsonStr = "{\"date\":1591326971,\"bbb\":\"222\",\"aaa\":\"123\"}";
|
||||
|
||||
Assert.assertEquals(jsonStr, json.toString());
|
||||
|
||||
// 解析测试
|
||||
final JSONObject parse = JSONUtil.parseObj(jsonStr, jsonConfig);
|
||||
Assert.assertEquals(date, parse.getDate("date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTimestampTest(){
|
||||
String timeStr = "1970-01-01 00:00:00";
|
||||
|
Reference in New Issue
Block a user