mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix json format bug
This commit is contained in:
parent
52560c28f8
commit
c5c9f80f0e
@ -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()传空返回true(issue#I3ETTY@Gitee)
|
* 【core 】 修复Validator.isUrl()传空返回true(issue#I3ETTY@Gitee)
|
||||||
* 【db 】 修复数据库driver根据url的判断识别错误问题(issue#I3EWBI@Gitee)
|
* 【db 】 修复数据库driver根据url的判断识别错误问题(issue#I3EWBI@Gitee)
|
||||||
|
* 【json 】 修复JSONStrFormatter换行多余空行问题(issue#I3FA8B@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,24 +1,29 @@
|
|||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON字符串格式化工具,用于简单格式化JSON字符串<br>
|
* JSON字符串格式化工具,用于简单格式化JSON字符串<br>
|
||||||
* from http://blog.csdn.net/lovelong8808/article/details/54580278
|
* from http://blog.csdn.net/lovelong8808/article/details/54580278
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回格式化JSON字符串。
|
* 返回格式化JSON字符串。
|
||||||
*
|
*
|
||||||
* @param json 未格式化的JSON字符串。
|
* @param json 未格式化的JSON字符串。
|
||||||
* @return 格式化的JSON字符串。
|
* @return 格式化的JSON字符串。
|
||||||
*/
|
*/
|
||||||
@ -32,41 +37,48 @@ public class JSONStrFormatter {
|
|||||||
char key;
|
char key;
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -113,7 +130,7 @@ public class JSONStrFormatter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回指定次数的缩进字符串。每一次缩进4个空格,即SPACE。
|
* 返回指定次数的缩进字符串。每一次缩进4个空格,即SPACE。
|
||||||
*
|
*
|
||||||
* @param number 缩进次数。
|
* @param number 缩进次数。
|
||||||
* @return 指定缩进次数的字符串。
|
* @return 指定缩进次数的字符串。
|
||||||
*/
|
*/
|
||||||
|
@ -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() {
|
||||||
@ -16,18 +17,24 @@ public class JSONStrFormaterTest {
|
|||||||
String result = JSONStrFormatter.format(json);
|
String result = JSONStrFormatter.format(json);
|
||||||
Assert.assertNotNull(result);
|
Assert.assertNotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTest2() {
|
public void formatTest2() {
|
||||||
String json = "{\"abc\":{\"def\":\"\\\"[ghi]\"}}";
|
String json = "{\"abc\":{\"def\":\"\\\"[ghi]\"}}";
|
||||||
String result = JSONStrFormatter.format(json);
|
String result = JSONStrFormatter.format(json);
|
||||||
Assert.assertNotNull(result);
|
Assert.assertNotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatTest3() {
|
public void formatTest3() {
|
||||||
String json = "{\"id\":13,\"title\":\"《标题》\",\"subtitle\":\"副标题z'c'z'xv'c'xv\",\"user_id\":6,\"type\":0}";
|
String json = "{\"id\":13,\"title\":\"《标题》\",\"subtitle\":\"副标题z'c'z'xv'c'xv\",\"user_id\":6,\"type\":0}";
|
||||||
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));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user