add converter for json

This commit is contained in:
Looly 2022-09-08 02:21:05 +08:00
parent bb99d52a87
commit 63028906b9
5 changed files with 41 additions and 21 deletions

View File

@ -29,9 +29,6 @@ import cn.hutool.core.convert.impl.XMLGregorianCalendarConverter;
import cn.hutool.core.convert.impl.ZoneIdConverter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.reflect.TypeUtil;
import cn.hutool.core.util.ServiceLoaderUtil;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.Serializable;
@ -78,6 +75,25 @@ import java.util.concurrent.atomic.AtomicReference;
public class RegisterConverter implements Converter, Serializable {
private static final long serialVersionUID = 1L;
/**
* 类级的内部类也就是静态的成员式内部类该内部类的实例与外部类的实例 没有绑定关系而且只有被调用到才会装载从而实现了延迟加载
*/
private static class SingletonHolder {
/**
* 静态初始化器由JVM来保证线程安全
*/
private static final CompositeConverter INSTANCE = new CompositeConverter();
}
/**
* 获得单例的 ConverterRegistry
*
* @return ConverterRegistry
*/
public static CompositeConverter getInstance() {
return RegisterConverter.SingletonHolder.INSTANCE;
}
/**
* 默认类型转换器
*/
@ -92,7 +108,6 @@ public class RegisterConverter implements Converter, Serializable {
*/
public RegisterConverter() {
registerDefault();
registerCustomBySpi();
}
@Override
@ -233,20 +248,4 @@ public class RegisterConverter implements Converter, Serializable {
defaultConverterMap.put(Optional.class, new OptionalConverter());// since 5.0.0
defaultConverterMap.put(Opt.class, new OptConverter());// since 5.7.16
}
/**
* 使用SPI加载转换器
*/
private void registerCustomBySpi() {
ServiceLoaderUtil.load(Converter.class).forEach(converter -> {
try {
final Type type = TypeUtil.getTypeArgument(ClassUtil.getClass(converter));
if (null != type) {
putCustom(type, converter);
}
} catch (final Exception ignore) {
// 忽略注册失败的
}
});
}
}

View File

@ -67,7 +67,7 @@ public class MapConverter implements Converter, Serializable {
// 二次转换转换键值类型
map = convert(targetType, keyType, valueType, map);
} else {
throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
throw new UnsupportedOperationException(StrUtil.format("Unsupported toMap value type: {}", value.getClass().getName()));
}
return map;
}

View File

@ -7,6 +7,7 @@ import cn.hutool.core.convert.impl.DateConverter;
import cn.hutool.core.convert.impl.TemporalAccessorConverter;
import cn.hutool.core.reflect.TypeUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.json.convert.JSONConverter;
import java.io.Serializable;
import java.time.temporal.TemporalAccessor;
@ -59,6 +60,12 @@ public class JSONConfig implements Serializable {
*/
private Converter converter = (type, value)->{
final Class<?> rawType = TypeUtil.getClass(type);
if(null == rawType){
return value;
}
if(JSON.class.isAssignableFrom(rawType)){
return JSONConverter.INSTANCE.toJSON(value);
}
if(Date.class.isAssignableFrom(rawType) || TemporalAccessor.class.isAssignableFrom(rawType)){
// 用户指定了日期格式获取日期属性时使用对应格式
final String valueStr = Convert.convertWithCheck(String.class, value, null, isIgnoreError());

View File

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.BeanCopier;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.RegisterConverter;
import cn.hutool.core.convert.impl.ArrayConverter;
import cn.hutool.core.convert.impl.CollectionConverter;
import cn.hutool.core.convert.impl.MapConverter;
@ -42,6 +43,11 @@ import java.util.Map;
public class JSONConverter implements Converter {
public static final JSONConverter INSTANCE = new JSONConverter(null);
static {
RegisterConverter.getInstance().putCustom(JSONObject.class, INSTANCE);
RegisterConverter.getInstance().putCustom(JSONArray.class, INSTANCE);
}
/**
* 创建JSON转换器
*
@ -141,6 +147,12 @@ public class JSONConverter implements Converter {
return result;
}
// 标准转换器
final Converter converter = RegisterConverter.getInstance().getConverter(targetType, true);
if (null != converter) {
return (T) converter.convert(targetType, json);
}
// 尝试转Bean
if (BeanUtil.isBean(rawType)) {
return BeanCopier.of(json,

View File

@ -1,3 +1,5 @@
package cn.hutool.json;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;