修复JSON反序列化时,引用字段类型的自定义JsonDeserializer无效

This commit is contained in:
Looly
2022-09-04 23:18:42 +08:00
parent 508c139b22
commit 11724c8761
7 changed files with 116 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ import cn.hutool.core.convert.impl.UUIDConverter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
@@ -279,7 +280,13 @@ public class ConverterRegistry implements Serializable {
// 尝试转Bean
if (BeanUtil.isBean(rowType)) {
return new BeanConverter<T>(type).convert(value, defaultValue);
try {
// 由于5.x设计缺陷JSON转bean无法实现自定义转换因此此处临时使用反射方式获取自定义的转换器此问题会在6.x中彻底解决。
final Class<?> clazz = ClassLoaderUtil.loadClass("cn.hutool.json.BeanConverterForJSON");
return ((Converter<T>)ReflectUtil.newInstance(clazz, type)).convert(value, defaultValue);
}catch (final Throwable ignore){
return new BeanConverter<T>(type).convert(value, defaultValue);
}
}
// 无法转换

View File

@@ -31,7 +31,7 @@ public class BeanConverter<T> extends AbstractConverter<T> {
private final Type beanType;
private final Class<T> beanClass;
private final CopyOptions copyOptions;
protected CopyOptions copyOptions;
/**
* 构造,默认转换选项,注入失败的字段忽略