mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复JSONConfig.setDateFormat设置后setWriteLongAsString失效问题
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.convert.NumberWithFormat;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.FileReader;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
@@ -754,6 +755,10 @@ public class JSONUtil {
|
||||
|| object instanceof Number //
|
||||
|| ObjectUtil.isBasicType(object) //
|
||||
) {
|
||||
if(object instanceof Number && null != jsonConfig.getDateFormat()){
|
||||
// 当JSONConfig中设置了日期格式,则包装为NumberWithFormat,以便在Converter中使用自定义格式转换日期时间
|
||||
return new NumberWithFormat((Number) object, jsonConfig.getDateFormat());
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.json.serialize;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.NumberWithFormat;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TemporalAccessorUtil;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
@@ -254,6 +255,11 @@ public class JSONWriter extends Writer {
|
||||
} else if (value instanceof Iterable || value instanceof Iterator || ArrayUtil.isArray(value)) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
// issue#IALQ0N,避免设置日期格式后writeLongAsString失效
|
||||
if(value instanceof NumberWithFormat){
|
||||
value = ((NumberWithFormat) value).getNumber();
|
||||
}
|
||||
|
||||
if(value instanceof Long && config.isWriteLongAsString()){
|
||||
// issue#3541
|
||||
// long可能溢出,此时可选是否将long写出为字符串类型
|
||||
|
Reference in New Issue
Block a user