mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add jmh
This commit is contained in:
parent
2114b1c08f
commit
a19eb17daa
@ -18,6 +18,7 @@ package org.dromara.hutool.json;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.bean.path.BeanPath;
|
import org.dromara.hutool.core.bean.path.BeanPath;
|
||||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
|
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||||
import org.dromara.hutool.json.writer.JSONWriter;
|
import org.dromara.hutool.json.writer.JSONWriter;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -260,7 +261,11 @@ public interface JSON extends Serializable {
|
|||||||
* @param type {@link Type}
|
* @param type {@link Type}
|
||||||
* @return 实体类对象
|
* @return 实体类对象
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
default <T> T toBean(final Type type) {
|
default <T> T toBean(final Type type) {
|
||||||
|
if(JSON.class.isAssignableFrom(TypeUtil.getClass(type))){
|
||||||
|
return (T) this;
|
||||||
|
}
|
||||||
return getFactory().toBean(this, type);
|
return getFactory().toBean(this, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ public class MoshiEngine extends AbstractJSONEngine implements Wrapper<Moshi> {
|
|||||||
public void serialize(final Object bean, final OutputStream out) {
|
public void serialize(final Object bean, final OutputStream out) {
|
||||||
final BufferedSink sink = Okio.buffer(Okio.sink(out));
|
final BufferedSink sink = Okio.buffer(Okio.sink(out));
|
||||||
try {
|
try {
|
||||||
getAdapter(this.moshi, bean.getClass()).toJson(sink, bean);
|
getAdapter(bean.getClass()).toJson(sink, bean);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new JSONException(e);
|
throw new JSONException(e);
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ public class MoshiEngine extends AbstractJSONEngine implements Wrapper<Moshi> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toJsonString(final Object bean) {
|
public String toJsonString(final Object bean) {
|
||||||
final JsonAdapter<Object> adapter = getAdapter(this.moshi, bean.getClass());
|
final JsonAdapter<Object> adapter = getAdapter(bean.getClass());
|
||||||
return adapter.toJson(bean);
|
return adapter.toJson(bean);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,11 +117,10 @@ public class MoshiEngine extends AbstractJSONEngine implements Wrapper<Moshi> {
|
|||||||
/**
|
/**
|
||||||
* 获取并配置{@link JsonAdapter}
|
* 获取并配置{@link JsonAdapter}
|
||||||
*
|
*
|
||||||
* @param moshi {@link Moshi}
|
|
||||||
* @param type Bean类型
|
* @param type Bean类型
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
private JsonAdapter<Object> getAdapter(final Moshi moshi, final Type type) {
|
private JsonAdapter<Object> getAdapter(final Type type) {
|
||||||
initEngine();
|
initEngine();
|
||||||
JsonAdapter<Object> adapter = this.moshi.adapter(type);
|
JsonAdapter<Object> adapter = this.moshi.adapter(type);
|
||||||
if (ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)) {
|
if (ObjUtil.defaultIfNull(this.config, JSONEngineConfig::isPrettyPrint, false)) {
|
||||||
|
@ -39,7 +39,7 @@ public class DefaultDeserializer implements JSONDeserializer<Object> {
|
|||||||
public Object deserialize(final JSON json, final Type deserializeType) {
|
public Object deserialize(final JSON json, final Type deserializeType) {
|
||||||
// 当目标类型不确定时,返回原JSON
|
// 当目标类型不确定时,返回原JSON
|
||||||
final Class<?> rawType = TypeUtil.getClass(deserializeType);
|
final Class<?> rawType = TypeUtil.getClass(deserializeType);
|
||||||
if (null == rawType || Object.class == rawType || rawType == json.getClass()) {
|
if (null == rawType || Object.class == rawType || rawType.isAssignableFrom(json.getClass())) {
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.json.jmh;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import org.dromara.hutool.json.JSON;
|
||||||
|
import org.dromara.hutool.json.engine.JSONEngine;
|
||||||
|
import org.dromara.hutool.json.engine.JSONEngineFactory;
|
||||||
|
import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||||
|
@Warmup(iterations = 5, time = 1) //预热5次调用
|
||||||
|
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||||
|
@Threads(1) //单线程
|
||||||
|
@Fork(1) //
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||||
|
@State(Scope.Benchmark) // 共享域
|
||||||
|
public class FromJsonStringStrJmh {
|
||||||
|
|
||||||
|
private JSONEngine jacksonEngine;
|
||||||
|
private JSONEngine gsonEngine;
|
||||||
|
private JSONEngine fastJSONEngine;
|
||||||
|
private JSONEngine hutoolEngine;
|
||||||
|
|
||||||
|
private String jsonStr;
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
||||||
|
|
||||||
|
jacksonEngine = JSONEngineFactory.createEngine("jackson");
|
||||||
|
gsonEngine = JSONEngineFactory.createEngine("gson");
|
||||||
|
fastJSONEngine = JSONEngineFactory.createEngine("fastjson");
|
||||||
|
hutoolEngine = JSONEngineFactory.createEngine("hutool");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void jacksonJmh() {
|
||||||
|
jacksonEngine.fromJsonString(jsonStr, JsonNode.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void gsonJmh() {
|
||||||
|
gsonEngine.fromJsonString(jsonStr, JsonElement.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void fastJSONJmh() {
|
||||||
|
fastJSONEngine.fromJsonString(jsonStr, com.alibaba.fastjson2.JSON.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void hutoolJSONJmh() {
|
||||||
|
hutoolEngine.fromJsonString(jsonStr, JSON.class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user