mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复JSONConfig.setDateFormat设置后setWriteLongAsString失效问题
This commit is contained in:
parent
c3b576f698
commit
9262ace7da
@ -13,7 +13,7 @@ import java.util.Date;
|
||||
* @author looly
|
||||
* @since 5.8.13
|
||||
*/
|
||||
public class NumberWithFormat extends Number implements TypeConverter{
|
||||
public class NumberWithFormat extends Number implements TypeConverter {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Number number;
|
||||
@ -21,6 +21,7 @@ public class NumberWithFormat extends Number implements TypeConverter{
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param number 数字
|
||||
* @param format 格式
|
||||
*/
|
||||
@ -40,7 +41,7 @@ public class NumberWithFormat extends Number implements TypeConverter{
|
||||
return new DateConverter((Class<? extends Date>) clazz, format).convert(this.number, null);
|
||||
} else if (TemporalAccessor.class.isAssignableFrom(clazz)) {
|
||||
return new TemporalAccessorConverter(clazz, format).convert(this.number, null);
|
||||
} else if(String.class == clazz){
|
||||
} else if (String.class == clazz) {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@ -51,6 +52,16 @@ public class NumberWithFormat extends Number implements TypeConverter{
|
||||
return Convert.convertWithCheck(targetType, this.number, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始Number
|
||||
*
|
||||
* @return 原始Number
|
||||
* @since 5.8.32
|
||||
*/
|
||||
public Object getNumber() {
|
||||
return this.number;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return this.number.intValue();
|
||||
|
@ -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写出为字符串类型
|
||||
|
@ -5,13 +5,14 @@ import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class IssueI6IS5BTest {
|
||||
@Test
|
||||
public void payloadToBeanTest() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user