This commit is contained in:
Looly 2024-09-30 00:02:11 +08:00
parent 5f45b2275e
commit 867e1ce55e
5 changed files with 27 additions and 21 deletions

View File

@ -41,7 +41,6 @@
<gson.version>2.11.0</gson.version>
<fastjson2.version>2.0.41</fastjson2.version>
<moshi.version>1.15.1</moshi.version>
<jmh.version>1.37</jmh.version>
</properties>
<dependencies>
@ -108,18 +107,5 @@
<version>${jjwt.version}</version>
<scope>test</scope>
</dependency>
<!-- 基准性能测试 -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -225,7 +225,9 @@ public interface JSON extends Serializable {
* @throws JSONException 包含非法数抛出此异常
*/
default String toJSONString(final int indentFactor) throws JSONException {
return toJSONString(indentFactor, null);
final JSONWriter jsonWriter = getFactory().ofWriter(new StringBuilder(), indentFactor);
this.write(jsonWriter);
return jsonWriter.toString();
}
/**

View File

@ -38,12 +38,6 @@ public class JsonToStringJmh {
Assertions.assertNotNull(jsonStr);
}
@Benchmark
public void gsonAppendJmh() {
final String jsonStr = gson.toString();
Assertions.assertNotNull(jsonStr);
}
@Benchmark
public void hutoolJmh() {
final String jsonStr = hutoolJSON.toString();

View File

@ -1,6 +1,9 @@
package org.dromara.hutool.json.jmh;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.dromara.hutool.json.JSONObject;
@ -49,4 +52,11 @@ public class ParseTreeJmh {
final com.alibaba.fastjson2.JSONObject jsonObject = JSON.parseObject(jsonStr);
assertNotNull(jsonObject);
}
@Benchmark
public void jacksonJmh() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final JsonNode jsonNode = mapper.readTree(jsonStr);
assertNotNull(jsonNode);
}
}

14
pom.xml
View File

@ -58,6 +58,7 @@
<junit.version>5.10.3</junit.version>
<lombok.version>1.18.34</lombok.version>
<kotlin-version>1.9.25</kotlin-version>
<jmh.version>1.37</jmh.version>
</properties>
<dependencies>
@ -81,6 +82,19 @@
<version>${lombok.version}</version>
<scope>test</scope>
</dependency>
<!-- 基准性能测试 -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<issueManagement>