From 8e91ef29385ba6bb64d20fb5c2313490c962d646 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 11 Apr 2024 21:45:12 +0800 Subject: [PATCH] =?UTF-8?q?JSONConfig=E5=A2=9E=E5=8A=A0setWriteLongAsStrin?= =?UTF-8?q?g=E5=8F=AF=E9=80=89=E6=98=AF=E5=90=A6=E5=B0=86Long=E5=86=99?= =?UTF-8?q?=E5=87=BA=E4=B8=BAString=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../main/java/cn/hutool/json/JSONConfig.java | 25 +++++++++++++++++++ .../cn/hutool/json/serialize/JSONWriter.java | 8 +++++- .../java/cn/hutool/json/Issue3541Test.java | 22 ++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index effbf49c0..6112bbeff 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 【core 】 StrUtil增加toStringOrEmpty方法(issue#I9CPC7@Gitee) * 【extra 】 设置jsch登录认证方式,跳过Kerberos身份验证(pr#3530@Github) * 【extra 】 增加设置验证码大小和针对alias注释(pr#3533@Github) +* 【json 】 JSONConfig增加setWriteLongAsString可选是否将Long写出为String类型(issue#3541@Github) ### 🐞Bug修复 * 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github) diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java b/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java index 969286207..c8fd7ce9b 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONConfig.java @@ -49,6 +49,12 @@ public class JSONConfig implements Serializable { */ private boolean checkDuplicate; + /** + * 是否将Long值写出为字符串类型 + * @since 5.8.28 + */ + private boolean writeLongAsString; + /** * 创建默认的配置项 * @@ -262,4 +268,23 @@ public class JSONConfig implements Serializable { this.checkDuplicate = checkDuplicate; 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; + } } diff --git a/hutool-json/src/main/java/cn/hutool/json/serialize/JSONWriter.java b/hutool-json/src/main/java/cn/hutool/json/serialize/JSONWriter.java index 5994a7451..161ac86bd 100755 --- a/hutool-json/src/main/java/cn/hutool/json/serialize/JSONWriter.java +++ b/hutool-json/src/main/java/cn/hutool/json/serialize/JSONWriter.java @@ -261,7 +261,13 @@ 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) { - 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) { // issue#2572@Github if(value instanceof MonthDay){ diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java new file mode 100644 index 000000000..57cf86036 --- /dev/null +++ b/hutool-json/src/test/java/cn/hutool/json/Issue3541Test.java @@ -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; + } +}