mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
json format
This commit is contained in:
parent
40fbed9173
commit
011abcba4b
@ -1145,7 +1145,7 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
* @return 移除后的字符串
|
* @return 移除后的字符串
|
||||||
* @since 5.3.8
|
* @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);
|
String result = toStringOrNull(str);
|
||||||
if (isNotEmpty(str)) {
|
if (isNotEmpty(str)) {
|
||||||
for (final CharSequence strToRemove : strsToRemove) {
|
for (final CharSequence strToRemove : strsToRemove) {
|
||||||
|
@ -134,7 +134,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
|
|||||||
* @since 3.0.9
|
* @since 3.0.9
|
||||||
*/
|
*/
|
||||||
default String toStringPretty() throws JSONException {
|
default String toStringPretty() throws JSONException {
|
||||||
return this.toJSONString(4);
|
return this.toJSONString(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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 int indentFactor;
|
||||||
private char newLineChar;
|
private CharSequence newLineChars;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param indentFactor 缩进因子,即每个缩进空格数
|
* @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.indentFactor = indentFactor;
|
||||||
this.newLineChar = newLineChar;
|
this.newLineChars = newLineChars;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,11 +55,11 @@ public class JSONStrFormatter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置换行符
|
* 设置换行符
|
||||||
* @param newLineChar 换行符
|
* @param newLineChars 换行符
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public JSONStrFormatter setNewLineChar(final char newLineChar) {
|
public JSONStrFormatter setNewLineChars(final CharSequence newLineChars) {
|
||||||
this.newLineChar = newLineChar;
|
this.newLineChars = newLineChars;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,10 +124,10 @@ public class JSONStrFormatter {
|
|||||||
if ((key == CharUtil.BRACKET_START) || (key == CharUtil.DELIM_START)) {
|
if ((key == CharUtil.BRACKET_START) || (key == CharUtil.DELIM_START)) {
|
||||||
//如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
|
//如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。
|
||||||
if ((i > 1) && (json.charAt(i - 1) == CharUtil.COLON)) {
|
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));
|
result.append(indent(++number));
|
||||||
continue;
|
continue;
|
||||||
@ -136,7 +136,7 @@ public class JSONStrFormatter {
|
|||||||
// 3、如果当前字符是后方括号、后花括号做如下处理:
|
// 3、如果当前字符是后方括号、后花括号做如下处理:
|
||||||
if ((key == CharUtil.BRACKET_END) || (key == CharUtil.DELIM_END)) {
|
if ((key == CharUtil.BRACKET_END) || (key == CharUtil.DELIM_END)) {
|
||||||
// (1)后方括号、后花括号,的前面必须换行。打印:换行。
|
// (1)后方括号、后花括号,的前面必须换行。打印:换行。
|
||||||
result.append(newLineChar);
|
result.append(newLineChars);
|
||||||
// (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
|
// (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。
|
||||||
result.append(indent(--number));
|
result.append(indent(--number));
|
||||||
// (3)打印:当前字符。
|
// (3)打印:当前字符。
|
||||||
@ -151,7 +151,7 @@ public class JSONStrFormatter {
|
|||||||
|
|
||||||
// 4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
|
// 4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。
|
||||||
if ((key == CharUtil.COMMA)) {
|
if ((key == CharUtil.COMMA)) {
|
||||||
result.append(key).append(newLineChar).append(indent(number));
|
result.append(key).append(newLineChars).append(indent(number));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ public class HutoolJSONEngine extends AbstractJSONEngine {
|
|||||||
public void serialize(final Object bean, final Writer writer) {
|
public void serialize(final Object bean, final Writer writer) {
|
||||||
initEngine();
|
initEngine();
|
||||||
final JSON json = (JSON) JSONUtil.parse(bean, this.hutoolSJONConfig);
|
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
|
@Override
|
||||||
|
@ -23,6 +23,15 @@ import java.io.Serializable;
|
|||||||
public class JSONEngineConfig implements Serializable {
|
public class JSONEngineConfig implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建JSON引擎配置
|
||||||
|
*
|
||||||
|
* @return JSONEngineConfig
|
||||||
|
*/
|
||||||
|
public static JSONEngineConfig of() {
|
||||||
|
return new JSONEngineConfig();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否格式化输出
|
* 是否格式化输出
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,7 @@ import com.fasterxml.jackson.core.JsonParser;
|
|||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
@ -83,10 +84,10 @@ public class JacksonEngine extends AbstractJSONEngine {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
// 默认配置
|
// 默认配置
|
||||||
this.mapper.enable(
|
mapper.enable(
|
||||||
// 允许出现单引号
|
// 允许出现单引号
|
||||||
JsonParser.Feature.ALLOW_SINGLE_QUOTES,
|
JsonParser.Feature.ALLOW_SINGLE_QUOTES,
|
||||||
// 允许没有引号的字段名(非标准)
|
// 允许没有引号的字段名(非标准)
|
||||||
@ -95,7 +96,9 @@ public class JacksonEngine extends AbstractJSONEngine {
|
|||||||
|
|
||||||
// 自定义配置
|
// 自定义配置
|
||||||
if(ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)){
|
if(ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)){
|
||||||
this.mapper.writerWithDefaultPrettyPrinter();
|
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.mapper = mapper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user