mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add converter for json
This commit is contained in:
@@ -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) {
|
||||
// 忽略注册失败的
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user