This commit is contained in:
Looly
2022-09-05 12:20:34 +08:00
parent d141efea62
commit 5b2d2050d2
5 changed files with 130 additions and 103 deletions

View File

@@ -6,6 +6,8 @@ import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
@@ -70,8 +72,19 @@ public class CopyOptions implements Serializable {
/**
* 自定义类型转换器,默认使用全局万能转换器转换
*/
protected TypeConverter converter = (type, value) ->
Convert.convertWithCheck(type, value, null, ignoreError);
protected TypeConverter converter = (type, value) -> {
if(null == value){
return null;
}
final String name = value.getClass().getName();
if(ArrayUtil.contains(new String[]{"cn.hutool.json.JSONObject", "cn.hutool.json.JSONArray"}, name)){
// 由于设计缺陷导致JSON转Bean时无法使用自定义的反序列化器此处采用反射方式修复bug此类问题会在6.x解决
return ReflectUtil.invoke(value, "toBean", ObjectUtil.defaultIfNull(type, Object.class));
}
return Convert.convertWithCheck(type, value, null, ignoreError);
};
//region create

View File

@@ -55,7 +55,7 @@ public abstract class AbstractConverter<T> implements Converter<T>, Serializable
// 除Map外已经是目标类型不需要转换Map类型涉及参数类型需要单独转换
return targetType.cast(value);
}
T result = convertInternal(value);
final T result = convertInternal(value);
return ((null == result) ? defaultValue : result);
} else {
throw new IllegalArgumentException(