mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user