🐞修复JSONStrFormatter:format函数对于转义符号处理逻辑错误问题

This commit is contained in:
Looly
2023-09-28 16:31:59 +08:00
parent 997f2af8cd
commit 0c3f91bad3
7 changed files with 43 additions and 11 deletions

View File

@@ -54,15 +54,18 @@ public class JSONStrFormatter {
if (null == wrapChar) {
//字符串模式开始
wrapChar = key;
} else if (isEscapeMode) {
//在字符串模式下的转义
isEscapeMode = false;
} else if (wrapChar.equals(key)) {
if (isEscapeMode) {
//字符串模式下,遇到结束符号,也同时结束转义
isEscapeMode = false;
}
//字符串包装结束
wrapChar = null;
}
if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) {
// 值前加空格
result.append(CharUtil.SPACE);
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.json;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class IssueI84V6ITest {
@Test
void formatTest() {
final String a1 = "{'x':'\\n','y':','}";
final String formatJsonStr = JSONUtil.formatJsonStr(a1);
// Console.log(formatJsonStr);
Assertions.assertEquals(
"{\n" +
" 'x': '\\n',\n" +
" 'y': ','\n" +
"}", formatJsonStr);
}
}