mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add methods
This commit is contained in:
@@ -300,7 +300,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void putNullTest() {
|
||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
array.addValue(null);
|
||||
array.addNull();
|
||||
|
||||
assertEquals("[null]", array.toString());
|
||||
}
|
||||
|
@@ -51,22 +51,22 @@ public class JSONNullTest {
|
||||
@Test
|
||||
public void setNullTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.ofObj().putValue("key1", null).toString();
|
||||
String json1 = JSONUtil.ofObj().putNull("key1").toString();
|
||||
Assertions.assertEquals("{}", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).putValue("key1", null).toString();
|
||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).putNull("key1").toString();
|
||||
Assertions.assertEquals("{\"key1\":null}", json1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNullOfJSONArrayTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.ofArray().addValue(null).toString();
|
||||
String json1 = JSONUtil.ofArray().addNull().toString();
|
||||
Assertions.assertEquals("[]", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addValue(null).toString();
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addNull().toString();
|
||||
Assertions.assertEquals("[null]", json1);
|
||||
}
|
||||
}
|
||||
|
@@ -730,7 +730,7 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void nullToEmptyTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false))
|
||||
.putValue("a", null)
|
||||
.putNull("a")
|
||||
.putValue("b", "value2");
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> {
|
||||
|
@@ -0,0 +1,70 @@
|
||||
package org.dromara.hutool.json.jmh;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.google.gson.JsonArray;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 测试JSON树结构转JSON字符串性能
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class JsonAddJmh {
|
||||
|
||||
List<String> testData;
|
||||
private JSONArray hutoolJSON;
|
||||
private JsonArray gson;
|
||||
private com.alibaba.fastjson2.JSONArray fastJSON;
|
||||
private ArrayNode jackson;
|
||||
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Console.log("准备数据。。。");
|
||||
testData = new ArrayList<>(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
testData.add(RandomUtil.randomString(20));
|
||||
}
|
||||
|
||||
hutoolJSON = new JSONArray();
|
||||
gson = new JsonArray();
|
||||
fastJSON = new com.alibaba.fastjson2.JSONArray();
|
||||
jackson = JsonNodeFactory.instance.arrayNode();
|
||||
Console.log("数据完毕");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
testData.forEach(gson::add);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
testData.forEach(hutoolJSON::addValue);
|
||||
//hutoolJSON.putAllObj(testData);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UseBulkOperation")
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
testData.forEach(fastJSON::add);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh(){
|
||||
testData.forEach(jackson::add);
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package org.dromara.hutool.json.serializer;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.StopWatch;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.json.JSONFactory;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JSONMapperTest {
|
||||
|
||||
/**
|
||||
* Mapper性能耗费较多
|
||||
*/
|
||||
@Test
|
||||
@Disabled
|
||||
void toJSONTest() {
|
||||
final JSONFactory factory = JSONFactory.getInstance();
|
||||
|
||||
final JSONMapper mapper = factory.getMapper();
|
||||
|
||||
final StopWatch stopWatch = DateUtil.createStopWatch();
|
||||
|
||||
final int count = 1000;
|
||||
stopWatch.start("use mapper");
|
||||
for (int i = 0; i < count; i++) {
|
||||
mapper.toJSON("qbw123", false);
|
||||
}
|
||||
stopWatch.stop();
|
||||
|
||||
stopWatch.start("use ofPrimitive");
|
||||
for (int i = 0; i < count; i++) {
|
||||
factory.ofPrimitive("qbw123");
|
||||
}
|
||||
stopWatch.stop();
|
||||
|
||||
Console.log(stopWatch.prettyPrint());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user