mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
add ClassTypeAdapter
This commit is contained in:
@@ -37,13 +37,22 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
public interface JSON extends Serializable {
|
||||
|
||||
/**
|
||||
* 获取JSON工厂
|
||||
*
|
||||
* @return JSON工厂
|
||||
*/
|
||||
JSONFactory getFactory();
|
||||
|
||||
/**
|
||||
* 获取JSON配置
|
||||
*
|
||||
* @return {@link JSONConfig}
|
||||
* @since 5.3.0
|
||||
*/
|
||||
JSONConfig config();
|
||||
default JSONConfig config(){
|
||||
return getFactory().getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON大小,对于JSONObject,是键值对的多少,JSONArray则是元素的个数,JSON原始数据为1
|
||||
@@ -250,6 +259,6 @@ public interface JSON extends Serializable {
|
||||
* @return 实体类对象
|
||||
*/
|
||||
default <T> T toBean(final Type type) {
|
||||
return JSONFactory.of(config(), null).toBean(this, type);
|
||||
return getFactory().toBean(this, type);
|
||||
}
|
||||
}
|
||||
|
@@ -103,8 +103,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -22,6 +22,9 @@ import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.JSONMapper;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapter;
|
||||
import org.dromara.hutool.json.support.JSONNodeBeanFactory;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
@@ -54,6 +57,16 @@ public class JSONFactory {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JSON工厂
|
||||
*
|
||||
* @param config JSON配置
|
||||
* @return JSON工厂
|
||||
*/
|
||||
public static JSONFactory of(final JSONConfig config) {
|
||||
return of(config, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JSON工厂
|
||||
*
|
||||
@@ -144,6 +157,31 @@ public class JSONFactory {
|
||||
return this.mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
* @return this
|
||||
*/
|
||||
public JSONFactory register(final Type type, final TypeAdapter typeAdapter) {
|
||||
getMapper().register(type, typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
* @return this
|
||||
*/
|
||||
public JSONFactory register(final TypeAdapter typeAdapter) {
|
||||
getMapper().register(typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
// region ----- of
|
||||
|
||||
/**
|
||||
|
@@ -31,12 +31,10 @@ import java.util.List;
|
||||
public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
|
||||
/**
|
||||
* 获取JSON配置
|
||||
*
|
||||
* @return {@link JSONConfig}
|
||||
* @since 5.3.0
|
||||
* 获取JSON工厂
|
||||
* @return JSON工厂
|
||||
*/
|
||||
JSONConfig config();
|
||||
JSONFactory getFactory();
|
||||
|
||||
/**
|
||||
* key对应值是否为{@code null}或无此key
|
||||
@@ -85,7 +83,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
}
|
||||
|
||||
if (json instanceof JSONObject) {
|
||||
return JSONUtil.parseArray(json, config());
|
||||
return getFactory().parseArray(json);
|
||||
}
|
||||
|
||||
return json.asJSONArray();
|
||||
|
@@ -100,8 +100,8 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return this.factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -119,9 +119,14 @@ public class JSONPrimitive implements Wrapper<Object>, JSON {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得JSON工厂类
|
||||
*
|
||||
* @return JSON工厂类
|
||||
*/
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return this.factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -22,6 +22,10 @@ import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapter;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.dromara.hutool.json.support.JSONStrFormatter;
|
||||
import org.dromara.hutool.json.xml.JSONXMLUtil;
|
||||
|
||||
@@ -404,7 +408,7 @@ public class JSONUtil {
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final Object obj, final JSONConfig config, Type type) {
|
||||
public static <T> T toBean(final Object obj, final JSONConfig config, final Type type) {
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
@@ -634,4 +638,27 @@ public class JSONUtil {
|
||||
return StrUtil.isWrap(StrUtil.trim(str), '[', ']');
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- registerTypeAdapter
|
||||
/**
|
||||
* 全局注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
*/
|
||||
public void registerTypeAdapter(final Type type, final TypeAdapter typeAdapter) {
|
||||
TypeAdapterManager.getInstance().register(type, typeAdapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
*/
|
||||
public void registerTypeAdapter(final TypeAdapter typeAdapter) {
|
||||
TypeAdapterManager.getInstance().register(typeAdapter);
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.impl.DefaultDeserializer;
|
||||
import org.dromara.hutool.json.xml.JSONXMLParser;
|
||||
import org.dromara.hutool.json.xml.ParseConfig;
|
||||
|
||||
@@ -58,7 +59,7 @@ public class JSONMapper implements Serializable {
|
||||
}
|
||||
|
||||
private final JSONFactory factory;
|
||||
private TypeAdapterManager typeAdapterManager;
|
||||
private volatile TypeAdapterManager typeAdapterManager;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@@ -89,6 +90,31 @@ public class JSONMapper implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
* @return this
|
||||
*/
|
||||
public JSONMapper register(final Type type, final TypeAdapter typeAdapter) {
|
||||
initTypeAdapterManager().register(type, typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
* @return this
|
||||
*/
|
||||
public JSONMapper register(final TypeAdapter typeAdapter) {
|
||||
initTypeAdapterManager().register(typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
@@ -121,10 +147,7 @@ public class JSONMapper implements Serializable {
|
||||
}
|
||||
final boolean ignoreError = ObjUtil.defaultIfNull(this.factory.getConfig(), JSONConfig::isIgnoreError, false);
|
||||
if (null == deserializer) {
|
||||
if (ignoreError) {
|
||||
return null;
|
||||
}
|
||||
throw new JSONException("No deserializer for type: " + type);
|
||||
deserializer = DefaultDeserializer.INSTANCE;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -314,4 +337,20 @@ public class JSONMapper implements Serializable {
|
||||
private JSON mapFromTokener(final JSONTokener tokener) {
|
||||
return this.factory.ofParser(tokener).parse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化类型转换器管理器,如果尚未初始化,则初始化,否则直接返回
|
||||
*
|
||||
* @return {@link TypeAdapterManager}
|
||||
*/
|
||||
private TypeAdapterManager initTypeAdapterManager() {
|
||||
if (null == this.typeAdapterManager) {
|
||||
synchronized (this) {
|
||||
if (null == this.typeAdapterManager) {
|
||||
this.typeAdapterManager = TypeAdapterManager.of();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.typeAdapterManager;
|
||||
}
|
||||
}
|
||||
|
@@ -225,7 +225,8 @@ public class TypeAdapterManager {
|
||||
}
|
||||
}
|
||||
|
||||
return DefaultDeserializer.INSTANCE;
|
||||
// 此处返回null,错误处理在mapper中
|
||||
return null;
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.serializer.impl;
|
||||
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Class类型适配器,用于将Class对象序列化为字符串,反序列化为Class对象<br>
|
||||
* 注意:考虑安全问题,此类并不作为默认的适配器,如需启用,需:
|
||||
* <pre>{@code
|
||||
* final JSONFactory factory = JSONFactory.of(null, null);
|
||||
* factory.register(Class<?>.class, ClassTypeAdapter.INSTANCE);
|
||||
* }</pre>
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class ClassTypeAdapter implements JSONSerializer<Class<?>>, JSONDeserializer<Class<?>> {
|
||||
|
||||
/**
|
||||
* 单例
|
||||
*/
|
||||
public static final ClassTypeAdapter INSTANCE = new ClassTypeAdapter();
|
||||
|
||||
@Override
|
||||
public JSON serialize(final Class<?> bean, final JSONContext context) {
|
||||
return context.getOrCreatePrimitive(bean.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> deserialize(final JSON json, final Type deserializeType) {
|
||||
return ClassUtil.forName((String) json.asJSONPrimitive().getValue(), true, null);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user