diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b93dc404..69d5f1a31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.6.3 (2021-04-04) +# 5.6.3 (2021-04-05) ### 新特性 * 【core 】 修改数字转换的实现,增加按照指定端序转换(pr#1492@Github) @@ -14,6 +14,7 @@ ### Bug修复 * 【core 】 修复Validator.isUrl()传空返回true(issue#I3ETTY@Gitee) * 【db 】 修复数据库driver根据url的判断识别错误问题(issue#I3EWBI@Gitee) +* 【json 】 修复JSONStrFormatter换行多余空行问题(issue#I3FA8B@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONStrFormatter.java b/hutool-json/src/main/java/cn/hutool/json/JSONStrFormatter.java index 41edf0231..5181401be 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONStrFormatter.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONStrFormatter.java @@ -1,24 +1,29 @@ package cn.hutool.json; +import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.StrUtil; /** * JSON字符串格式化工具,用于简单格式化JSON字符串
* from http://blog.csdn.net/lovelong8808/article/details/54580278 - * + * * @author looly * @since 3.1.2 */ public class JSONStrFormatter { - - /** 单位缩进字符串。*/ + + /** + * 单位缩进字符串。 + */ private static final String SPACE = " "; - /** 换行符*/ + /** + * 换行符 + */ private static final char NEW_LINE = StrUtil.C_LF; /** * 返回格式化JSON字符串。 - * + * * @param json 未格式化的JSON字符串。 * @return 格式化的JSON字符串。 */ @@ -32,41 +37,48 @@ public class JSONStrFormatter { char key; for (int i = 0; i < length; i++) { key = json.charAt(i); - - if('"' == key || '\'' == key) { - if(null == wrapChar) { + + if (CharUtil.DOUBLE_QUOTES == key || CharUtil.SINGLE_QUOTE == key) { + if (null == wrapChar) { //字符串模式开始 wrapChar = key; - }else if(isEscapeMode) { + } else if (isEscapeMode) { //在字符串模式下的转义 isEscapeMode = false; - }else if(wrapChar.equals(key)){ + } else if (wrapChar.equals(key)) { //字符串包装结束 wrapChar = null; } + + if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) { + result.append(CharUtil.SPACE); + } + result.append(key); continue; - }else if('\\' == key) { - if(null != wrapChar) { + } + + if (CharUtil.BACKSLASH == key) { + if (null != wrapChar) { //字符串模式下转义有效 isEscapeMode = !isEscapeMode; result.append(key); continue; - }else { + } else { result.append(key); } } - - if(null != wrapChar) { + + if (null != wrapChar) { //字符串模式 result.append(key); continue; } - + //如果当前字符是前方括号、前花括号做如下处理: - if ((key == '[') || (key == '{')) { + if ((key == CharUtil.BRACKET_START) || (key == CharUtil.DELIM_START)) { //如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。 - if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) { + if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) { result.append(NEW_LINE); result.append(indent(number)); } @@ -81,7 +93,7 @@ public class JSONStrFormatter { } // 3、如果当前字符是后方括号、后花括号做如下处理: - if ((key == ']') || (key == '}')) { + if ((key == CharUtil.BRACKET_END) || (key == CharUtil.DELIM_END)) { // (1)后方括号、后花括号,的前面必须换行。打印:换行。 result.append(NEW_LINE); // (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。 @@ -90,9 +102,9 @@ public class JSONStrFormatter { // (3)打印:当前字符。 result.append(key); // (4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。 - if (((i + 1) < length) && (json.charAt(i + 1) != ',')) { - result.append(NEW_LINE); - } +// if (((i + 1) < length) && (json.charAt(i + 1) != ',')) { +// result.append(NEW_LINE); +// } // (5)继续下一次循环。 continue; } @@ -104,6 +116,11 @@ public class JSONStrFormatter { result.append(indent(number)); continue; } + + if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) { + result.append(CharUtil.SPACE); + } + // 5、打印:当前字符。 result.append(key); } @@ -113,7 +130,7 @@ public class JSONStrFormatter { /** * 返回指定次数的缩进字符串。每一次缩进4个空格,即SPACE。 - * + * * @param number 缩进次数。 * @return 指定缩进次数的字符串。 */ diff --git a/hutool-json/src/test/java/cn/hutool/json/JSONStrFormaterTest.java b/hutool-json/src/test/java/cn/hutool/json/JSONStrFormatterTest.java similarity index 71% rename from hutool-json/src/test/java/cn/hutool/json/JSONStrFormaterTest.java rename to hutool-json/src/test/java/cn/hutool/json/JSONStrFormatterTest.java index 87c7b628e..fc89eace0 100644 --- a/hutool-json/src/test/java/cn/hutool/json/JSONStrFormaterTest.java +++ b/hutool-json/src/test/java/cn/hutool/json/JSONStrFormatterTest.java @@ -1,5 +1,6 @@ package cn.hutool.json; +import cn.hutool.core.lang.Console; import org.junit.Assert; import org.junit.Test; @@ -8,7 +9,7 @@ import org.junit.Test; * @author looly * */ -public class JSONStrFormaterTest { +public class JSONStrFormatterTest { @Test public void formatTest() { @@ -16,18 +17,24 @@ public class JSONStrFormaterTest { String result = JSONStrFormatter.format(json); Assert.assertNotNull(result); } - + @Test public void formatTest2() { String json = "{\"abc\":{\"def\":\"\\\"[ghi]\"}}"; String result = JSONStrFormatter.format(json); Assert.assertNotNull(result); } - + @Test public void formatTest3() { String json = "{\"id\":13,\"title\":\"《标题》\",\"subtitle\":\"副标题z'c'z'xv'c'xv\",\"user_id\":6,\"type\":0}"; String result = JSONStrFormatter.format(json); Assert.assertNotNull(result); } + + @Test + public void formatTest4(){ + String jsonStr = "{\"employees\":[{\"firstName\":\"Bill\",\"lastName\":\"Gates\"},{\"firstName\":\"George\",\"lastName\":\"Bush\"},{\"firstName\":\"Thomas\",\"lastName\":\"Carter\"}]}"; + Console.log(JSONUtil.formatJsonStr(jsonStr)); + } }