fix json format bug

This commit is contained in:
Looly 2021-04-06 09:34:30 +08:00
parent 52560c28f8
commit c5c9f80f0e
3 changed files with 52 additions and 27 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.6.3 (2021-04-04) # 5.6.3 (2021-04-05)
### 新特性 ### 新特性
* 【core 】 修改数字转换的实现增加按照指定端序转换pr#1492@Github * 【core 】 修改数字转换的实现增加按照指定端序转换pr#1492@Github
@ -14,6 +14,7 @@
### Bug修复 ### Bug修复
* 【core 】 修复Validator.isUrl()传空返回trueissue#I3ETTY@Gitee * 【core 】 修复Validator.isUrl()传空返回trueissue#I3ETTY@Gitee
* 【db 】 修复数据库driver根据url的判断识别错误问题issue#I3EWBI@Gitee * 【db 】 修复数据库driver根据url的判断识别错误问题issue#I3EWBI@Gitee
* 【json 】 修复JSONStrFormatter换行多余空行问题issue#I3FA8B@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -1,5 +1,6 @@
package cn.hutool.json; package cn.hutool.json;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
/** /**
@ -11,9 +12,13 @@ import cn.hutool.core.util.StrUtil;
*/ */
public class JSONStrFormatter { public class JSONStrFormatter {
/** 单位缩进字符串。*/ /**
* 单位缩进字符串
*/
private static final String SPACE = " "; private static final String SPACE = " ";
/** 换行符*/ /**
* 换行符
*/
private static final char NEW_LINE = StrUtil.C_LF; private static final char NEW_LINE = StrUtil.C_LF;
/** /**
@ -33,40 +38,47 @@ public class JSONStrFormatter {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
key = json.charAt(i); key = json.charAt(i);
if('"' == key || '\'' == key) { if (CharUtil.DOUBLE_QUOTES == key || CharUtil.SINGLE_QUOTE == key) {
if(null == wrapChar) { if (null == wrapChar) {
//字符串模式开始 //字符串模式开始
wrapChar = key; wrapChar = key;
}else if(isEscapeMode) { } else if (isEscapeMode) {
//在字符串模式下的转义 //在字符串模式下的转义
isEscapeMode = false; isEscapeMode = false;
}else if(wrapChar.equals(key)){ } else if (wrapChar.equals(key)) {
//字符串包装结束 //字符串包装结束
wrapChar = null; wrapChar = null;
} }
if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) {
result.append(CharUtil.SPACE);
}
result.append(key); result.append(key);
continue; continue;
}else if('\\' == key) { }
if(null != wrapChar) {
if (CharUtil.BACKSLASH == key) {
if (null != wrapChar) {
//字符串模式下转义有效 //字符串模式下转义有效
isEscapeMode = !isEscapeMode; isEscapeMode = !isEscapeMode;
result.append(key); result.append(key);
continue; continue;
}else { } else {
result.append(key); result.append(key);
} }
} }
if(null != wrapChar) { if (null != wrapChar) {
//字符串模式 //字符串模式
result.append(key); result.append(key);
continue; 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(NEW_LINE);
result.append(indent(number)); result.append(indent(number));
} }
@ -81,7 +93,7 @@ public class JSONStrFormatter {
} }
// 3如果当前字符是后方括号后花括号做如下处理 // 3如果当前字符是后方括号后花括号做如下处理
if ((key == ']') || (key == '}')) { if ((key == CharUtil.BRACKET_END) || (key == CharUtil.DELIM_END)) {
// 1后方括号后花括号的前面必须换行打印换行 // 1后方括号后花括号的前面必须换行打印换行
result.append(NEW_LINE); result.append(NEW_LINE);
// 2每出现一次后方括号后花括号缩进次数减少一次打印缩进 // 2每出现一次后方括号后花括号缩进次数减少一次打印缩进
@ -90,9 +102,9 @@ public class JSONStrFormatter {
// 3打印当前字符 // 3打印当前字符
result.append(key); result.append(key);
// 4如果当前字符后面还有字符并且字符不为打印换行 // 4如果当前字符后面还有字符并且字符不为打印换行
if (((i + 1) < length) && (json.charAt(i + 1) != ',')) { // if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
result.append(NEW_LINE); // result.append(NEW_LINE);
} // }
// 5继续下一次循环 // 5继续下一次循环
continue; continue;
} }
@ -104,6 +116,11 @@ public class JSONStrFormatter {
result.append(indent(number)); result.append(indent(number));
continue; continue;
} }
if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) {
result.append(CharUtil.SPACE);
}
// 5打印当前字符 // 5打印当前字符
result.append(key); result.append(key);
} }

View File

@ -1,5 +1,6 @@
package cn.hutool.json; package cn.hutool.json;
import cn.hutool.core.lang.Console;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -8,7 +9,7 @@ import org.junit.Test;
* @author looly * @author looly
* *
*/ */
public class JSONStrFormaterTest { public class JSONStrFormatterTest {
@Test @Test
public void formatTest() { public void formatTest() {
@ -30,4 +31,10 @@ public class JSONStrFormaterTest {
String result = JSONStrFormatter.format(json); String result = JSONStrFormatter.format(json);
Assert.assertNotNull(result); 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));
}
} }