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
@ -14,6 +14,7 @@
### Bug修复
* 【core 】 修复Validator.isUrl()传空返回trueissue#I3ETTY@Gitee
* 【db 】 修复数据库driver根据url的判断识别错误问题issue#I3EWBI@Gitee
* 【json 】 修复JSONStrFormatter换行多余空行问题issue#I3FA8B@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -1,24 +1,29 @@
package cn.hutool.json;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
/**
* JSON字符串格式化工具用于简单格式化JSON字符串<br>
* 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 指定缩进次数的字符串
*/

View File

@ -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));
}
}