json format

This commit is contained in:
Looly 2024-08-10 12:01:10 +08:00
parent 40fbed9173
commit 011abcba4b
11 changed files with 125 additions and 20 deletions

View File

@ -1145,7 +1145,7 @@ public class CharSequenceUtil extends StrValidator {
* @return 移除后的字符串
* @since 5.3.8
*/
public static String removeAny(final CharSequence str, final CharSequence... strsToRemove) {
public static String removeAll(final CharSequence str, final CharSequence... strsToRemove) {
String result = toStringOrNull(str);
if (isNotEmpty(str)) {
for (final CharSequence strToRemove : strsToRemove) {

View File

@ -134,7 +134,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
* @since 3.0.9
*/
default String toStringPretty() throws JSONException {
return this.toJSONString(4);
return this.toJSONString(2);
}
/**

View File

@ -27,20 +27,20 @@ public class JSONStrFormatter {
/**
* 默认实例
*/
public static final JSONStrFormatter INSTANCE = new JSONStrFormatter(4, CharUtil.LF);
public static final JSONStrFormatter INSTANCE = new JSONStrFormatter(2, StrUtil.LF);
private int indentFactor;
private char newLineChar;
private CharSequence newLineChars;
/**
* 构造
*
* @param indentFactor 缩进因子即每个缩进空格数
* @param newLineChar 换行符
* @param newLineChars 换行符
*/
public JSONStrFormatter(final int indentFactor, final char newLineChar){
public JSONStrFormatter(final int indentFactor, final CharSequence newLineChars){
this.indentFactor = indentFactor;
this.newLineChar = newLineChar;
this.newLineChars = newLineChars;
}
/**
@ -55,11 +55,11 @@ public class JSONStrFormatter {
/**
* 设置换行符
* @param newLineChar 换行符
* @param newLineChars 换行符
* @return this
*/
public JSONStrFormatter setNewLineChar(final char newLineChar) {
this.newLineChar = newLineChar;
public JSONStrFormatter setNewLineChars(final CharSequence newLineChars) {
this.newLineChars = newLineChars;
return this;
}
@ -124,10 +124,10 @@ public class JSONStrFormatter {
if ((key == CharUtil.BRACKET_START) || (key == CharUtil.DELIM_START)) {
//如果前面还有字符并且字符为打印换行和缩进字符字符串
if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) {
result.append(newLineChar).append(indent(number));
result.append(newLineChars).append(indent(number));
}
//前方括号前花括号的后面必须换行打印换行
result.append(key).append(newLineChar);
result.append(key).append(newLineChars);
//每出现一次前方括号前花括号缩进次数增加一次打印新行缩进
result.append(indent(++number));
continue;
@ -136,7 +136,7 @@ public class JSONStrFormatter {
// 3如果当前字符是后方括号后花括号做如下处理
if ((key == CharUtil.BRACKET_END) || (key == CharUtil.DELIM_END)) {
// 1后方括号后花括号的前面必须换行打印换行
result.append(newLineChar);
result.append(newLineChars);
// 2每出现一次后方括号后花括号缩进次数减少一次打印缩进
result.append(indent(--number));
// 3打印当前字符
@ -151,7 +151,7 @@ public class JSONStrFormatter {
// 4如果当前字符是逗号逗号后面换行并缩进不改变缩进次数
if ((key == CharUtil.COMMA)) {
result.append(key).append(newLineChar).append(indent(number));
result.append(key).append(newLineChars).append(indent(number));
continue;
}

View File

@ -35,7 +35,7 @@ public class HutoolJSONEngine extends AbstractJSONEngine {
public void serialize(final Object bean, final Writer writer) {
initEngine();
final JSON json = (JSON) JSONUtil.parse(bean, this.hutoolSJONConfig);
json.write(writer, ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false) ? 4 : 0, 0, null);
json.write(writer, ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false) ? 2 : 0, 0, null);
}
@Override

View File

@ -23,6 +23,15 @@ import java.io.Serializable;
public class JSONEngineConfig implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建JSON引擎配置
*
* @return JSONEngineConfig
*/
public static JSONEngineConfig of() {
return new JSONEngineConfig();
}
/**
* 是否格式化输出
*/

View File

@ -16,6 +16,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.util.ObjUtil;
@ -83,10 +84,10 @@ public class JacksonEngine extends AbstractJSONEngine {
return;
}
this.mapper = new ObjectMapper();
final ObjectMapper mapper = new ObjectMapper();
// 默认配置
this.mapper.enable(
mapper.enable(
// 允许出现单引号
JsonParser.Feature.ALLOW_SINGLE_QUOTES,
// 允许没有引号的字段名(非标准)
@ -95,7 +96,9 @@ public class JacksonEngine extends AbstractJSONEngine {
// 自定义配置
if(ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)){
this.mapper.writerWithDefaultPrettyPrinter();
}
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
this.mapper = mapper;
}
}

View File

@ -0,0 +1,23 @@
package org.dromara.hutool.json.engine;
import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class FastJSONTest {
@Test
void prettyPrintTest() {
final JSONEngine engine = JSONEngineFactory.createEngine("fastjson");
engine.init(JSONEngineConfig.of().setPrettyPrint(true));
final JSONEngineFactoryTest.TestBean testBean = new JSONEngineFactoryTest.TestBean("张三", 18, true);
String jsonString = engine.toJsonString(testBean);
// 使用统一换行符
jsonString = StrUtil.removeAll(jsonString, '\r');
Assertions.assertEquals("{\n" +
" \"name\":\"张三\",\n" +
" \"age\":18,\n" +
" \"gender\":true\n" +
"}", jsonString);
}
}

View File

@ -0,0 +1,24 @@
package org.dromara.hutool.json.engine;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class GsonTest {
/**
* Gson默认缩进两个空格使用\n换行符
*/
@Test
void prettyPrintTest() {
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
engine.init(JSONEngineConfig.of().setPrettyPrint(true));
final JSONEngineFactoryTest.TestBean testBean = new JSONEngineFactoryTest.TestBean("张三", 18, true);
final String jsonString = engine.toJsonString(testBean);
// 使用统一换行符
Assertions.assertEquals("{\n" +
" \"name\": \"张三\",\n" +
" \"age\": 18,\n" +
" \"gender\": true\n" +
"}", jsonString);
}
}

View File

@ -0,0 +1,20 @@
package org.dromara.hutool.json.engine;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class HutoolJSONTest {
@Test
void prettyPrintTest() {
final JSONEngine engine = JSONEngineFactory.createEngine("hutoolJSON");
engine.init(JSONEngineConfig.of().setPrettyPrint(true));
final JSONEngineFactoryTest.TestBean testBean = new JSONEngineFactoryTest.TestBean("张三", 18, true);
final String jsonString = engine.toJsonString(testBean);
Assertions.assertEquals("{\n" +
" \"name\": \"张三\",\n" +
" \"age\": 18,\n" +
" \"gender\": true\n" +
"}", jsonString);
}
}

View File

@ -0,0 +1,26 @@
package org.dromara.hutool.json.engine;
import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class JacksonTest {
/**
* jackson默认缩进两个空格使用\r\n换行符
*/
@Test
void prettyPrintTest() {
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
engine.init(JSONEngineConfig.of().setPrettyPrint(true));
final JSONEngineFactoryTest.TestBean testBean = new JSONEngineFactoryTest.TestBean("张三", 18, true);
String jsonString = engine.toJsonString(testBean);
// 使用统一换行符
jsonString = StrUtil.removeAll(jsonString, '\r');
Assertions.assertEquals("{\n" +
" \"name\" : \"张三\",\n" +
" \"age\" : 18,\n" +
" \"gender\" : true\n" +
"}", jsonString);
}
}