mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
JSONConfig增加setWriteLongAsString可选是否将Long写出为String类型
This commit is contained in:
parent
5fa835d702
commit
8e91ef2938
@ -9,6 +9,7 @@
|
|||||||
* 【core 】 StrUtil增加toStringOrEmpty方法(issue#I9CPC7@Gitee)
|
* 【core 】 StrUtil增加toStringOrEmpty方法(issue#I9CPC7@Gitee)
|
||||||
* 【extra 】 设置jsch登录认证方式,跳过Kerberos身份验证(pr#3530@Github)
|
* 【extra 】 设置jsch登录认证方式,跳过Kerberos身份验证(pr#3530@Github)
|
||||||
* 【extra 】 增加设置验证码大小和针对alias注释(pr#3533@Github)
|
* 【extra 】 增加设置验证码大小和针对alias注释(pr#3533@Github)
|
||||||
|
* 【json 】 JSONConfig增加setWriteLongAsString可选是否将Long写出为String类型(issue#3541@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github)
|
||||||
|
@ -49,6 +49,12 @@ public class JSONConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private boolean checkDuplicate;
|
private boolean checkDuplicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否将Long值写出为字符串类型
|
||||||
|
* @since 5.8.28
|
||||||
|
*/
|
||||||
|
private boolean writeLongAsString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建默认的配置项
|
* 创建默认的配置项
|
||||||
*
|
*
|
||||||
@ -262,4 +268,23 @@ public class JSONConfig implements Serializable {
|
|||||||
this.checkDuplicate = checkDuplicate;
|
this.checkDuplicate = checkDuplicate;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否将Long值写出为字符串类型
|
||||||
|
* @return 是否将Long值写出为字符串类型
|
||||||
|
* @since 5.8.28
|
||||||
|
*/
|
||||||
|
public boolean isWriteLongAsString() {
|
||||||
|
return writeLongAsString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否将Long值写出为字符串类型
|
||||||
|
* @param writeLongAsString 是否将Long值写出为字符串类型
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public JSONConfig setWriteLongAsString(boolean writeLongAsString) {
|
||||||
|
this.writeLongAsString = writeLongAsString;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -261,7 +261,13 @@ public class JSONWriter extends Writer {
|
|||||||
} else if (value instanceof Iterable || value instanceof Iterator || ArrayUtil.isArray(value)) {
|
} else if (value instanceof Iterable || value instanceof Iterator || ArrayUtil.isArray(value)) {
|
||||||
new JSONArray(value).write(writer, indentFactor, indent);
|
new JSONArray(value).write(writer, indentFactor, indent);
|
||||||
} else if (value instanceof Number) {
|
} else if (value instanceof Number) {
|
||||||
writeNumberValue((Number) value);
|
if(value instanceof Long && config.isWriteLongAsString()){
|
||||||
|
// issue#3541
|
||||||
|
// long可能溢出,此时可选是否将long写出为字符串类型
|
||||||
|
writeStrValue(value.toString());
|
||||||
|
} else {
|
||||||
|
writeNumberValue((Number) value);
|
||||||
|
}
|
||||||
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
} else if (value instanceof Date || value instanceof Calendar || value instanceof TemporalAccessor) {
|
||||||
// issue#2572@Github
|
// issue#2572@Github
|
||||||
if(value instanceof MonthDay){
|
if(value instanceof MonthDay){
|
||||||
|
22
hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java
Normal file
22
hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package cn.hutool.json;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Issue3541Test {
|
||||||
|
@Test
|
||||||
|
public void longToStringTest() {
|
||||||
|
Demo demo = new Demo();
|
||||||
|
demo.setId(1227690722069581409L);
|
||||||
|
demo.setName("hutool");
|
||||||
|
String jsonStr = JSONUtil.toJsonStr(demo, JSONConfig.create().setWriteLongAsString(true));
|
||||||
|
Assert.assertEquals("{\"id\":\"1227690722069581409\",\"name\":\"hutool\"}", jsonStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Demo {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user