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