mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
json format
This commit is contained in:
@@ -23,8 +23,8 @@ public class IssueI84V6ITest {
|
||||
// Console.log(formatJsonStr);
|
||||
Assertions.assertEquals(
|
||||
"{\n" +
|
||||
" 'x': '\\n',\n" +
|
||||
" 'y': ','\n" +
|
||||
" 'x': '\\n',\n" +
|
||||
" 'y': ','\n" +
|
||||
"}", formatJsonStr);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user