mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-16 07:59:46 +08:00
fix code
This commit is contained in:
parent
c385affaa9
commit
008e21f8f8
@ -49,7 +49,7 @@ import java.io.PushbackInputStream;
|
|||||||
public class BOMInputStream extends InputStream {
|
public class BOMInputStream extends InputStream {
|
||||||
|
|
||||||
private final PushbackInputStream in;
|
private final PushbackInputStream in;
|
||||||
private boolean isInited = false;
|
private boolean initialized;
|
||||||
private final String defaultCharset;
|
private final String defaultCharset;
|
||||||
private String charset;
|
private String charset;
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ public class BOMInputStream extends InputStream {
|
|||||||
* @return 编码
|
* @return 编码
|
||||||
*/
|
*/
|
||||||
public String getCharset() {
|
public String getCharset() {
|
||||||
if (!isInited) {
|
if (!initialized) {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
} catch (final IOException ex) {
|
} catch (final IOException ex) {
|
||||||
@ -104,13 +104,13 @@ public class BOMInputStream extends InputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
isInited = true;
|
initialized = true;
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
isInited = true;
|
initialized = true;
|
||||||
return in.read();
|
return in.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class BOMInputStream extends InputStream {
|
|||||||
* @throws IOException 读取引起的异常
|
* @throws IOException 读取引起的异常
|
||||||
*/
|
*/
|
||||||
protected void init() throws IOException {
|
protected void init() throws IOException {
|
||||||
if (isInited) {
|
if (initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,6 +146,6 @@ public class BOMInputStream extends InputStream {
|
|||||||
in.unread(bom, (n - unread), unread);
|
in.unread(bom, (n - unread), unread);
|
||||||
}
|
}
|
||||||
|
|
||||||
isInited = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,11 @@ public abstract class AtomicLoader<T> implements Loader<T>, Serializable {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInitialized() {
|
||||||
|
return null != reference.get();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化被加载的对象<br>
|
* 初始化被加载的对象<br>
|
||||||
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
||||||
|
@ -18,7 +18,6 @@ package org.dromara.hutool.core.lang.loader;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,12 +41,13 @@ public class LazyFunLoader<T> extends LazyLoader<T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 静态工厂方法,提供语义性与编码便利性
|
* 静态工厂方法,提供语义性与编码便利性
|
||||||
|
*
|
||||||
* @param supplier 用于生成对象的函数
|
* @param supplier 用于生成对象的函数
|
||||||
* @param <T> 对象类型
|
* @param <T> 对象类型
|
||||||
* @return 函数式懒加载加载器对象
|
* @return 函数式懒加载加载器对象
|
||||||
* @since 5.8.0
|
* @since 5.8.0
|
||||||
*/
|
*/
|
||||||
public static <T> LazyFunLoader<T> on(final Supplier<T> supplier) {
|
public static <T> LazyFunLoader<T> of(final Supplier<T> supplier) {
|
||||||
Assert.notNull(supplier, "supplier must be not null!");
|
Assert.notNull(supplier, "supplier must be not null!");
|
||||||
return new LazyFunLoader<>(supplier);
|
return new LazyFunLoader<>(supplier);
|
||||||
}
|
}
|
||||||
@ -68,27 +68,4 @@ public class LazyFunLoader<T> extends LazyLoader<T> {
|
|||||||
this.supplier = null;
|
this.supplier = null;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否已经初始化
|
|
||||||
*
|
|
||||||
* @return 是/否
|
|
||||||
*/
|
|
||||||
public boolean isInitialize() {
|
|
||||||
return this.supplier == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 如果已经初始化,就执行传入函数
|
|
||||||
*
|
|
||||||
* @param consumer 待执行函数
|
|
||||||
*/
|
|
||||||
public void ifInitialized(final Consumer<T> consumer) {
|
|
||||||
Assert.notNull(consumer);
|
|
||||||
|
|
||||||
// 已经初始化
|
|
||||||
if (this.isInitialize()) {
|
|
||||||
consumer.accept(this.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,11 @@ public abstract class LazyLoader<T> implements Loader<T>, Serializable {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInitialized() {
|
||||||
|
return null != object;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化被加载的对象<br>
|
* 初始化被加载的对象<br>
|
||||||
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
* 如果对象从未被加载过,调用此方法初始化加载对象,此方法只被调用一次
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.lang.loader;
|
package org.dromara.hutool.core.lang.loader;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对象加载抽象接口<br>
|
* 对象加载抽象接口<br>
|
||||||
* 通过实现此接口自定义实现对象的加载方式,例如懒加载机制、多线程加载等
|
* 通过实现此接口自定义实现对象的加载方式,例如懒加载机制、多线程加载等
|
||||||
@ -34,4 +36,25 @@ public interface Loader<T> {
|
|||||||
* @return 加载完毕的对象
|
* @return 加载完毕的对象
|
||||||
*/
|
*/
|
||||||
T get();
|
T get();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否已经初始化完毕
|
||||||
|
*
|
||||||
|
* @return 是否已经初始化完毕
|
||||||
|
*/
|
||||||
|
default boolean isInitialized() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果已经初始化,就执行传入函数
|
||||||
|
*
|
||||||
|
* @param consumer 待执行函数,为{@code null}表示不执行任何操作
|
||||||
|
*/
|
||||||
|
default void ifInitialized(final Consumer<T> consumer) {
|
||||||
|
// 已经初始化
|
||||||
|
if (null != consumer && this.isInitialized()) {
|
||||||
|
consumer.accept(get());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ public final class SensitiveUtil {
|
|||||||
/**
|
/**
|
||||||
* @return 是否已经被初始化
|
* @return 是否已经被初始化
|
||||||
*/
|
*/
|
||||||
public static boolean isInited() {
|
public static boolean isInitialized() {
|
||||||
return !sensitiveTree.isEmpty();
|
return !sensitiveTree.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.core.xml;
|
package org.dromara.hutool.core.xml;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.loader.LazyFunLoader;
|
||||||
|
import org.dromara.hutool.core.lang.loader.Loader;
|
||||||
|
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +32,7 @@ public class SAXParserFactoryUtil {
|
|||||||
/**
|
/**
|
||||||
* Sax读取器工厂缓存
|
* Sax读取器工厂缓存
|
||||||
*/
|
*/
|
||||||
private static volatile SAXParserFactory factory;
|
private static final Loader<SAXParserFactory> factoryLoader = LazyFunLoader.of(()->createFactory(false, true));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取全局{@link SAXParserFactory}<br>
|
* 获取全局{@link SAXParserFactory}<br>
|
||||||
@ -41,15 +44,7 @@ public class SAXParserFactoryUtil {
|
|||||||
* @return {@link SAXParserFactory}
|
* @return {@link SAXParserFactory}
|
||||||
*/
|
*/
|
||||||
public static SAXParserFactory getFactory() {
|
public static SAXParserFactory getFactory() {
|
||||||
if (null == factory) {
|
return factoryLoader.get();
|
||||||
synchronized (SAXParserFactoryUtil.class) {
|
|
||||||
if (null == factory) {
|
|
||||||
factory = createFactory(false, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return factory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,7 +36,7 @@ public class LazyFunLoaderTest {
|
|||||||
final LazyFunLoader<BigObject> loader = new LazyFunLoader<>(BigObject::new);
|
final LazyFunLoader<BigObject> loader = new LazyFunLoader<>(BigObject::new);
|
||||||
|
|
||||||
Assertions.assertNotNull(loader.get());
|
Assertions.assertNotNull(loader.get());
|
||||||
Assertions.assertTrue(loader.isInitialize());
|
Assertions.assertTrue(loader.isInitialized());
|
||||||
|
|
||||||
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
||||||
loader.ifInitialized(BigObject::destroy);
|
loader.ifInitialized(BigObject::destroy);
|
||||||
@ -56,16 +56,16 @@ public class LazyFunLoaderTest {
|
|||||||
it.destroy();
|
it.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
Assertions.assertFalse(loader.isInitialize());
|
Assertions.assertFalse(loader.isInitialized());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnLoadStaticFactoryMethod1() {
|
public void testOfLoadStaticFactoryMethod1() {
|
||||||
|
|
||||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.on(BigObject::new);
|
final LazyFunLoader<BigObject> loader = LazyFunLoader.of(BigObject::new);
|
||||||
|
|
||||||
Assertions.assertNotNull(loader.get());
|
Assertions.assertNotNull(loader.get());
|
||||||
Assertions.assertTrue(loader.isInitialize());
|
Assertions.assertTrue(loader.isInitialized());
|
||||||
|
|
||||||
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
// 对于某些对象,在程序关闭时,需要进行销毁操作
|
||||||
loader.ifInitialized(BigObject::destroy);
|
loader.ifInitialized(BigObject::destroy);
|
||||||
@ -74,9 +74,9 @@ public class LazyFunLoaderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnLoadStaticFactoryMethod2() {
|
public void testOfLoadStaticFactoryMethod2() {
|
||||||
|
|
||||||
final LazyFunLoader<BigObject> loader = LazyFunLoader.on(BigObject::new);
|
final LazyFunLoader<BigObject> loader = LazyFunLoader.of(BigObject::new);
|
||||||
|
|
||||||
// 若从未使用,则可以避免不必要的初始化
|
// 若从未使用,则可以避免不必要的初始化
|
||||||
loader.ifInitialized(it -> {
|
loader.ifInitialized(it -> {
|
||||||
|
@ -19,6 +19,7 @@ package org.dromara.hutool.jmh.json;
|
|||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import org.dromara.hutool.json.JSON;
|
import org.dromara.hutool.json.JSON;
|
||||||
|
import org.dromara.hutool.json.JSONUtil;
|
||||||
import org.dromara.hutool.json.engine.JSONEngine;
|
import org.dromara.hutool.json.engine.JSONEngine;
|
||||||
import org.dromara.hutool.json.engine.JSONEngineFactory;
|
import org.dromara.hutool.json.engine.JSONEngineFactory;
|
||||||
import org.openjdk.jmh.annotations.*;
|
import org.openjdk.jmh.annotations.*;
|
||||||
@ -43,7 +44,7 @@ public class FromJsonStringStrJmh {
|
|||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() {
|
||||||
jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
jsonStr = JSONJmhData.jsonStr;
|
||||||
|
|
||||||
jacksonEngine = JSONEngineFactory.createEngine("jackson");
|
jacksonEngine = JSONEngineFactory.createEngine("jackson");
|
||||||
gsonEngine = JSONEngineFactory.createEngine("gson");
|
gsonEngine = JSONEngineFactory.createEngine("gson");
|
||||||
@ -70,4 +71,9 @@ public class FromJsonStringStrJmh {
|
|||||||
public void hutoolJSONJmh() {
|
public void hutoolJSONJmh() {
|
||||||
hutoolEngine.fromJsonString(jsonStr, JSON.class);
|
hutoolEngine.fromJsonString(jsonStr, JSON.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void hutoolJSONParseJmh() {
|
||||||
|
JSONUtil.parseObj(jsonStr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
package org.dromara.hutool.jmh.json;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JSONJmhData {
|
||||||
|
public static String jsonStr = "{\n" +
|
||||||
|
" \"name\": \"张三\",\n" +
|
||||||
|
" \"age\": 18,\n" +
|
||||||
|
" \"birthday\": \"2020-01-01\",\n" +
|
||||||
|
" \"booleanValue\": true,\n" +
|
||||||
|
" \"jsonObjectSub\": {\n" +
|
||||||
|
" \"subStr\": \"abc\",\n" +
|
||||||
|
" \"subNumber\": 150343445454,\n" +
|
||||||
|
" \"subBoolean\": true\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"jsonArraySub\": [\n" +
|
||||||
|
" \"abc\",\n" +
|
||||||
|
" 123,\n" +
|
||||||
|
" false\n" +
|
||||||
|
" ]\n" +
|
||||||
|
"}";
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TestBean{
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private boolean gender;
|
||||||
|
private Date createDate;
|
||||||
|
private Object nullObj;
|
||||||
|
private SubBean jsonObjectSub;
|
||||||
|
private List<Object> jsonArraySub;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class SubBean{
|
||||||
|
private String subStr;
|
||||||
|
private Long subNumber;
|
||||||
|
private boolean subBoolean;
|
||||||
|
}
|
||||||
|
}
|
@ -29,6 +29,7 @@ public class JsonAddJmh {
|
|||||||
private JsonArray gson;
|
private JsonArray gson;
|
||||||
private com.alibaba.fastjson2.JSONArray fastJSON;
|
private com.alibaba.fastjson2.JSONArray fastJSON;
|
||||||
private ArrayNode jackson;
|
private ArrayNode jackson;
|
||||||
|
private ArrayList<Object> arrayList;
|
||||||
|
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
@ -43,6 +44,7 @@ public class JsonAddJmh {
|
|||||||
gson = new JsonArray();
|
gson = new JsonArray();
|
||||||
fastJSON = new com.alibaba.fastjson2.JSONArray();
|
fastJSON = new com.alibaba.fastjson2.JSONArray();
|
||||||
jackson = JsonNodeFactory.instance.arrayNode();
|
jackson = JsonNodeFactory.instance.arrayNode();
|
||||||
|
arrayList = new ArrayList<>();
|
||||||
Console.log("数据完毕");
|
Console.log("数据完毕");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,4 +69,9 @@ public class JsonAddJmh {
|
|||||||
public void jacksonJmh(){
|
public void jacksonJmh(){
|
||||||
testData.forEach(jackson::add);
|
testData.forEach(jackson::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void arrayListJmh(){
|
||||||
|
testData.forEach(arrayList::add);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ public class JsonPutJmh {
|
|||||||
private JsonObject gson;
|
private JsonObject gson;
|
||||||
private com.alibaba.fastjson2.JSONObject fastJSON;
|
private com.alibaba.fastjson2.JSONObject fastJSON;
|
||||||
private ObjectNode jackson;
|
private ObjectNode jackson;
|
||||||
|
private HashMap<String, Object> hashMap;
|
||||||
|
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
@ -41,6 +42,7 @@ public class JsonPutJmh {
|
|||||||
gson = new JsonObject();
|
gson = new JsonObject();
|
||||||
fastJSON = new com.alibaba.fastjson2.JSONObject();
|
fastJSON = new com.alibaba.fastjson2.JSONObject();
|
||||||
jackson = JsonNodeFactory.instance.objectNode();
|
jackson = JsonNodeFactory.instance.objectNode();
|
||||||
|
hashMap = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
@ -63,4 +65,9 @@ public class JsonPutJmh {
|
|||||||
public void jacksonJmh(){
|
public void jacksonJmh(){
|
||||||
testData.forEach(jackson::put);
|
testData.forEach(jackson::put);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void hashMapJmh(){
|
||||||
|
testData.forEach(hashMap::put);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ public class ParseTreeJmh {
|
|||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
public void setup() {
|
||||||
jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
jsonStr = JSONJmhData.jsonStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
package org.dromara.hutool.json;
|
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.loader.LazyFunLoader;
|
||||||
|
import org.dromara.hutool.core.lang.loader.Loader;
|
||||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.json.reader.JSONParser;
|
import org.dromara.hutool.json.reader.JSONParser;
|
||||||
@ -85,7 +87,7 @@ public class JSONFactory {
|
|||||||
* entry中,key在JSONObject中为name,在JSONArray中为index
|
* entry中,key在JSONObject中为name,在JSONArray中为index
|
||||||
*/
|
*/
|
||||||
private final Predicate<MutableEntry<Object, Object>> predicate;
|
private final Predicate<MutableEntry<Object, Object>> predicate;
|
||||||
private volatile JSONMapper mapper;
|
private final Loader<JSONMapper> mapperLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
@ -96,6 +98,7 @@ public class JSONFactory {
|
|||||||
public JSONFactory(final JSONConfig config, final Predicate<MutableEntry<Object, Object>> predicate) {
|
public JSONFactory(final JSONConfig config, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||||
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
|
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
|
this.mapperLoader = LazyFunLoader.of(()->JSONMapper.of(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -146,14 +149,7 @@ public class JSONFactory {
|
|||||||
* @return {@link JSONMapper}
|
* @return {@link JSONMapper}
|
||||||
*/
|
*/
|
||||||
public JSONMapper getMapper() {
|
public JSONMapper getMapper() {
|
||||||
if (null == this.mapper) {
|
return this.mapperLoader.get();
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.mapper) {
|
|
||||||
this.mapper = JSONMapper.of(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,6 +59,13 @@ public class HutoolJSONEngine extends AbstractJSONEngine {
|
|||||||
return json.toBean((Type) type);
|
return json.toBean((Type) type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T fromJsonString(final String jsonStr, final Object type) {
|
||||||
|
initEngine();
|
||||||
|
final JSON json = jsonFactory.parse(jsonStr);
|
||||||
|
return json.toBean((Type) type);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void reset() {
|
protected void reset() {
|
||||||
jsonFactory = null;
|
jsonFactory = null;
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.json.serializer;
|
package org.dromara.hutool.json.serializer;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.lang.Opt;
|
import org.dromara.hutool.core.lang.Opt;
|
||||||
|
import org.dromara.hutool.core.lang.loader.LazyFunLoader;
|
||||||
|
import org.dromara.hutool.core.lang.loader.Loader;
|
||||||
import org.dromara.hutool.core.reflect.TypeReference;
|
import org.dromara.hutool.core.reflect.TypeReference;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.json.*;
|
import org.dromara.hutool.json.*;
|
||||||
@ -55,7 +58,7 @@ public class JSONMapper implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final JSONFactory factory;
|
private final JSONFactory factory;
|
||||||
private volatile TypeAdapterManager typeAdapterManager;
|
private Loader<TypeAdapterManager> typeAdapterManagerLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
@ -64,6 +67,7 @@ public class JSONMapper implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public JSONMapper(final JSONFactory factory) {
|
public JSONMapper(final JSONFactory factory) {
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
|
this.typeAdapterManagerLoader = LazyFunLoader.of(TypeAdapterManager::of);
|
||||||
}
|
}
|
||||||
|
|
||||||
// region ----- typeAdapterManager
|
// region ----- typeAdapterManager
|
||||||
@ -74,17 +78,17 @@ public class JSONMapper implements Serializable {
|
|||||||
* @return 类型转换器管理器
|
* @return 类型转换器管理器
|
||||||
*/
|
*/
|
||||||
public TypeAdapterManager getTypeAdapterManager() {
|
public TypeAdapterManager getTypeAdapterManager() {
|
||||||
return this.typeAdapterManager;
|
return typeAdapterManagerLoader.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置自定义类型转换器,用于将自定义类型转换为JSONObject
|
* 设置自定义类型转换器,用于将自定义类型转换为JSONObject
|
||||||
*
|
*
|
||||||
* @param typeAdapterManager 类型转换器管理器
|
* @param typeAdapterManager 类型转换器管理器,不能为空
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public JSONMapper setTypeAdapterManager(final TypeAdapterManager typeAdapterManager) {
|
public JSONMapper setTypeAdapterManager(final TypeAdapterManager typeAdapterManager) {
|
||||||
this.typeAdapterManager = typeAdapterManager;
|
this.typeAdapterManagerLoader = () -> Assert.notNull(typeAdapterManager);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +100,7 @@ public class JSONMapper implements Serializable {
|
|||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public JSONMapper register(final Type type, final TypeAdapter typeAdapter) {
|
public JSONMapper register(final Type type, final TypeAdapter typeAdapter) {
|
||||||
initTypeAdapterManager().register(type, typeAdapter);
|
getTypeAdapterManager().register(type, typeAdapter);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +113,7 @@ public class JSONMapper implements Serializable {
|
|||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public JSONMapper register(final TypeAdapter typeAdapter) {
|
public JSONMapper register(final TypeAdapter typeAdapter) {
|
||||||
initTypeAdapterManager().register(typeAdapter);
|
getTypeAdapterManager().register(typeAdapter);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
@ -285,22 +289,6 @@ public class JSONMapper implements Serializable {
|
|||||||
json.getClass().getName(), result.getClass().getName());
|
json.getClass().getName(), result.getClass().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化类型转换器管理器,如果尚未初始化,则初始化,否则直接返回
|
|
||||||
*
|
|
||||||
* @return {@link TypeAdapterManager}
|
|
||||||
*/
|
|
||||||
private TypeAdapterManager initTypeAdapterManager() {
|
|
||||||
if (null == this.typeAdapterManager) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.typeAdapterManager) {
|
|
||||||
this.typeAdapterManager = TypeAdapterManager.of();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.typeAdapterManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取JSON对象对应的序列化器,先查找局部自定义,如果没有则查找全局自定义
|
* 获取JSON对象对应的序列化器,先查找局部自定义,如果没有则查找全局自定义
|
||||||
*
|
*
|
||||||
@ -311,8 +299,8 @@ public class JSONMapper implements Serializable {
|
|||||||
private JSONSerializer<Object> getSerializer(final Object obj, final Class<?> clazz) {
|
private JSONSerializer<Object> getSerializer(final Object obj, final Class<?> clazz) {
|
||||||
JSONSerializer<Object> serializer = null;
|
JSONSerializer<Object> serializer = null;
|
||||||
// 自定义序列化
|
// 自定义序列化
|
||||||
if (null != this.typeAdapterManager) {
|
if (this.typeAdapterManagerLoader.isInitialized()) {
|
||||||
serializer = this.typeAdapterManager.getSerializer(obj, clazz);
|
serializer = getTypeAdapterManager().getSerializer(obj, clazz);
|
||||||
}
|
}
|
||||||
// 全局自定义序列化
|
// 全局自定义序列化
|
||||||
if (null == serializer) {
|
if (null == serializer) {
|
||||||
@ -331,8 +319,8 @@ public class JSONMapper implements Serializable {
|
|||||||
private JSONDeserializer<Object> getDeserializer(final JSON json, final Type type) {
|
private JSONDeserializer<Object> getDeserializer(final JSON json, final Type type) {
|
||||||
JSONDeserializer<Object> deserializer = null;
|
JSONDeserializer<Object> deserializer = null;
|
||||||
// 自定义反序列化
|
// 自定义反序列化
|
||||||
if (null != this.typeAdapterManager) {
|
if (this.typeAdapterManagerLoader.isInitialized()) {
|
||||||
deserializer = this.typeAdapterManager.getDeserializer(json, type);
|
deserializer = getTypeAdapterManager().getDeserializer(json, type);
|
||||||
}
|
}
|
||||||
// 全局自定义反序列化
|
// 全局自定义反序列化
|
||||||
if (null == deserializer) {
|
if (null == deserializer) {
|
||||||
|
@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.json.serializer;
|
package org.dromara.hutool.json.serializer;
|
||||||
|
|
||||||
import org.dromara.hutool.core.collection.CollUtil;
|
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
|
import org.dromara.hutool.core.lang.loader.LazyFunLoader;
|
||||||
|
import org.dromara.hutool.core.lang.loader.Loader;
|
||||||
import org.dromara.hutool.core.lang.tuple.Pair;
|
import org.dromara.hutool.core.lang.tuple.Pair;
|
||||||
import org.dromara.hutool.core.lang.tuple.Triple;
|
import org.dromara.hutool.core.lang.tuple.Triple;
|
||||||
import org.dromara.hutool.core.lang.tuple.Tuple;
|
import org.dromara.hutool.core.lang.tuple.Tuple;
|
||||||
import org.dromara.hutool.core.map.MapUtil;
|
|
||||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||||
import org.dromara.hutool.json.JSON;
|
import org.dromara.hutool.json.JSON;
|
||||||
@ -83,26 +83,30 @@ public class TypeAdapterManager {
|
|||||||
/**
|
/**
|
||||||
* 用户自定义序列化器,存储自定义匹配规则的一类对象的转换器
|
* 用户自定义序列化器,存储自定义匹配规则的一类对象的转换器
|
||||||
*/
|
*/
|
||||||
private volatile Set<MatcherJSONSerializer<?>> serializerSet;
|
private final Loader<Set<MatcherJSONSerializer<?>>> serializerSetLoader;
|
||||||
/**
|
/**
|
||||||
* 用户自定义精确类型转换器<br>
|
* 用户自定义精确类型转换器<br>
|
||||||
* 主要存储类型明确(无子类)的转换器
|
* 主要存储类型明确(无子类)的转换器
|
||||||
*/
|
*/
|
||||||
private volatile Map<Type, JSONSerializer<?>> serializerMap;
|
private final Loader<Map<Type, JSONSerializer<?>>> serializerMapLoader;
|
||||||
/**
|
/**
|
||||||
* 用户自定义类型转换器,存储自定义匹配规则的一类对象的转换器
|
* 用户自定义类型转换器,存储自定义匹配规则的一类对象的转换器
|
||||||
*/
|
*/
|
||||||
private volatile Set<MatcherJSONDeserializer<?>> deserializerSet;
|
private final Loader<Set<MatcherJSONDeserializer<?>>> deserializerSetLoader;
|
||||||
/**
|
/**
|
||||||
* 用户自定义精确类型转换器<br>
|
* 用户自定义精确类型转换器<br>
|
||||||
* 主要存储类型明确(无子类)的转换器
|
* 主要存储类型明确(无子类)的转换器
|
||||||
*/
|
*/
|
||||||
private volatile Map<Type, JSONDeserializer<?>> deserializerMap;
|
private final Loader<Map<Type, JSONDeserializer<?>>> deserializerMapLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*/
|
*/
|
||||||
public TypeAdapterManager() {
|
public TypeAdapterManager() {
|
||||||
|
serializerSetLoader = LazyFunLoader.of(LinkedHashSet::new);
|
||||||
|
serializerMapLoader = LazyFunLoader.of(HashMap::new);
|
||||||
|
deserializerSetLoader = LazyFunLoader.of(LinkedHashSet::new);
|
||||||
|
deserializerMapLoader = LazyFunLoader.of(HashMap::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
// region ----- register
|
// region ----- register
|
||||||
@ -117,12 +121,12 @@ public class TypeAdapterManager {
|
|||||||
*/
|
*/
|
||||||
public TypeAdapterManager register(final TypeAdapter typeAdapter) {
|
public TypeAdapterManager register(final TypeAdapter typeAdapter) {
|
||||||
Assert.notNull(typeAdapter, "typeAdapter must be not null!");
|
Assert.notNull(typeAdapter, "typeAdapter must be not null!");
|
||||||
if(typeAdapter instanceof MatcherJSONSerializer || typeAdapter instanceof MatcherJSONDeserializer){
|
if (typeAdapter instanceof MatcherJSONSerializer || typeAdapter instanceof MatcherJSONDeserializer) {
|
||||||
if(typeAdapter instanceof MatcherJSONSerializer){
|
if (typeAdapter instanceof MatcherJSONSerializer) {
|
||||||
getSerializerSet().add((MatcherJSONSerializer<?>) typeAdapter);
|
serializerSetLoader.get().add((MatcherJSONSerializer<?>) typeAdapter);
|
||||||
}
|
}
|
||||||
if(typeAdapter instanceof MatcherJSONDeserializer){
|
if (typeAdapter instanceof MatcherJSONDeserializer) {
|
||||||
getDeserializerSet().add((MatcherJSONDeserializer<?>) typeAdapter);
|
deserializerSetLoader.get().add((MatcherJSONDeserializer<?>) typeAdapter);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -133,18 +137,18 @@ public class TypeAdapterManager {
|
|||||||
/**
|
/**
|
||||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||||
*
|
*
|
||||||
* @param type 类型
|
* @param type 类型
|
||||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public TypeAdapterManager register(final Type type, final TypeAdapter typeAdapter) {
|
public TypeAdapterManager register(final Type type, final TypeAdapter typeAdapter) {
|
||||||
Assert.notNull(type);
|
Assert.notNull(type);
|
||||||
if(typeAdapter instanceof JSONSerializer || typeAdapter instanceof JSONDeserializer){
|
if (typeAdapter instanceof JSONSerializer || typeAdapter instanceof JSONDeserializer) {
|
||||||
if(typeAdapter instanceof JSONSerializer){
|
if (typeAdapter instanceof JSONSerializer) {
|
||||||
getSerializerMap().put(type, (JSONSerializer<?>) typeAdapter);
|
serializerMapLoader.get().put(type, (JSONSerializer<?>) typeAdapter);
|
||||||
}
|
}
|
||||||
if(typeAdapter instanceof JSONDeserializer){
|
if (typeAdapter instanceof JSONDeserializer) {
|
||||||
getDeserializerMap().put(type, (JSONDeserializer<?>) typeAdapter);
|
deserializerMapLoader.get().put(type, (JSONDeserializer<?>) typeAdapter);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -172,16 +176,19 @@ public class TypeAdapterManager {
|
|||||||
return (JSONSerializer<Object>) ConstructorUtil.newInstanceIfPossible(rawType);
|
return (JSONSerializer<Object>) ConstructorUtil.newInstanceIfPossible(rawType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MapUtil.isNotEmpty(this.serializerMap)) {
|
if (this.serializerMapLoader.isInitialized()) {
|
||||||
final JSONSerializer<?> result = this.serializerMap.get(rawType);
|
final Map<Type, JSONSerializer<?>> serializerMap = this.serializerMapLoader.get();
|
||||||
if(null != result){
|
if (!serializerMap.isEmpty()) {
|
||||||
return (JSONSerializer<Object>) result;
|
final JSONSerializer<?> result = serializerMap.get(rawType);
|
||||||
|
if (null != result) {
|
||||||
|
return (JSONSerializer<Object>) result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matcher
|
// Matcher
|
||||||
if (CollUtil.isNotEmpty(this.serializerSet)) {
|
if (this.serializerSetLoader.isInitialized()) {
|
||||||
for (final MatcherJSONSerializer<?> serializer : this.serializerSet) {
|
for (final MatcherJSONSerializer<?> serializer : this.serializerSetLoader.get()) {
|
||||||
if (serializer.match(bean, null)) {
|
if (serializer.match(bean, null)) {
|
||||||
return (MatcherJSONSerializer<Object>) serializer;
|
return (MatcherJSONSerializer<Object>) serializer;
|
||||||
}
|
}
|
||||||
@ -209,19 +216,24 @@ public class TypeAdapterManager {
|
|||||||
return (JSONDeserializer<Object>) ConstructorUtil.newInstanceIfPossible(rawType);
|
return (JSONDeserializer<Object>) ConstructorUtil.newInstanceIfPossible(rawType);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MapUtil.isNotEmpty(this.deserializerMap)) {
|
if (this.deserializerMapLoader.isInitialized()) {
|
||||||
final JSONDeserializer<?> jsonDeserializer = this.deserializerMap.get(rawType);
|
final Map<Type, JSONDeserializer<?>> deserializerMap = this.deserializerMapLoader.get();
|
||||||
if (null != jsonDeserializer) {
|
if (!deserializerMap.isEmpty()) {
|
||||||
return (JSONDeserializer<Object>) jsonDeserializer;
|
final JSONDeserializer<?> result = deserializerMap.get(rawType);
|
||||||
|
if (null != result) {
|
||||||
|
return (JSONDeserializer<Object>) result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matcher
|
// Matcher
|
||||||
if (CollUtil.isNotEmpty(this.deserializerSet)) {
|
if (this.deserializerSetLoader.isInitialized()) {
|
||||||
for (final MatcherJSONDeserializer<?> deserializer : this.deserializerSet) {
|
final Set<MatcherJSONDeserializer<?>> deserializerSet = this.deserializerSetLoader.get();
|
||||||
if (deserializer.match(json, type)) {
|
if (!deserializerSet.isEmpty()) {
|
||||||
return (JSONDeserializer<Object>) deserializer;
|
return (JSONDeserializer<Object>) deserializerSet.stream()
|
||||||
}
|
.filter(deserializer -> deserializer.match(json, type))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,52 +242,6 @@ public class TypeAdapterManager {
|
|||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- getSet or Map
|
|
||||||
private Set<MatcherJSONSerializer<?>> getSerializerSet() {
|
|
||||||
if (null == this.serializerSet) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.serializerSet) {
|
|
||||||
this.serializerSet = new LinkedHashSet<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.serializerSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Type, JSONSerializer<?>> getSerializerMap() {
|
|
||||||
if (null == this.serializerMap) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.serializerMap) {
|
|
||||||
this.serializerMap = new HashMap<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.serializerMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Set<MatcherJSONDeserializer<?>> getDeserializerSet() {
|
|
||||||
if (null == this.deserializerSet) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.deserializerSet) {
|
|
||||||
this.deserializerSet = new LinkedHashSet<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.deserializerSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Type, JSONDeserializer<?>> getDeserializerMap() {
|
|
||||||
if (null == this.deserializerMap) {
|
|
||||||
synchronized (this) {
|
|
||||||
if (null == this.deserializerMap) {
|
|
||||||
this.deserializerMap = new HashMap<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.deserializerMap;
|
|
||||||
}
|
|
||||||
// endregion
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册默认的序列化器和反序列化器
|
* 注册默认的序列化器和反序列化器
|
||||||
*
|
*
|
||||||
|
@ -20,8 +20,10 @@ import com.alibaba.fastjson2.JSON;
|
|||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.alibaba.fastjson2.JSONWriter;
|
import com.alibaba.fastjson2.JSONWriter;
|
||||||
import com.alibaba.fastjson2.writer.ObjectWriter;
|
import com.alibaba.fastjson2.writer.ObjectWriter;
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
import org.dromara.hutool.core.text.StrUtil;
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class FastJSONTest {
|
public class FastJSONTest {
|
||||||
@ -42,6 +44,7 @@ public class FastJSONTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Disabled
|
||||||
void toStringTest() {
|
void toStringTest() {
|
||||||
final String jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
final String jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
||||||
final JSONObject jsonObject = JSON.parseObject(jsonStr);
|
final JSONObject jsonObject = JSON.parseObject(jsonStr);
|
||||||
@ -51,7 +54,7 @@ public class FastJSONTest {
|
|||||||
|
|
||||||
final JSONWriter.Context context = writer.getContext();
|
final JSONWriter.Context context = writer.getContext();
|
||||||
final ObjectWriter<?> objectWriter = context.getObjectWriter(jsonObject.getClass());
|
final ObjectWriter<?> objectWriter = context.getObjectWriter(jsonObject.getClass());
|
||||||
//Console.log(objectWriter.getClass());
|
Console.log(objectWriter.getClass());
|
||||||
|
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user