mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix json format bug
This commit is contained in:
@@ -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 指定缩进次数的字符串。
|
||||
*/
|
||||
|
Reference in New Issue
Block a user