This commit is contained in:
Looly 2022-06-22 22:43:23 +08:00
parent a4f0f46ab1
commit 8ee7124797
2 changed files with 31 additions and 5 deletions

View File

@ -251,7 +251,10 @@ public final class InternalJSONUtil {
.setIgnoreCase(config.isIgnoreCase())
.setIgnoreError(config.isIgnoreError())
.setIgnoreNullValue(config.isIgnoreNullValue())
.setTransientSupport(config.isTransientSupport());
.setTransientSupport(config.isTransientSupport())
// 使用JSON转换器
.setConverter((type, value) ->
JSONConverter.convertWithCheck(type, value, null, config.isIgnoreError()));
}
/**

View File

@ -2,7 +2,6 @@ package cn.hutool.json;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.convert.ConverterRegistry;
@ -30,9 +29,10 @@ public class JSONConverter implements Converter {
public static JSONConverter INSTANCE = new JSONConverter();
private static final ConverterRegistry registry;
static {
// 注册到转换中心
final ConverterRegistry registry = ConverterRegistry.getInstance();
registry = new ConverterRegistry();
registry.putCustom(JSON.class, INSTANCE);
registry.putCustom(JSONObject.class, INSTANCE);
registry.putCustom(JSONArray.class, INSTANCE);
@ -43,6 +43,29 @@ public class JSONConverter implements Converter {
return JSONUtil.parse(value);
}
/**
* 转换值为指定类型可选是否不抛异常转换<br>
* 当转换失败时返回默认值
*
* @param <T> 目标类型
* @param type 目标类型
* @param value
* @param defaultValue 默认值
* @param quietly 是否静默转换true不抛异常
* @return 转换后的值
* @since 5.3.2
*/
public static <T> T convertWithCheck(final Type type, final Object value, final T defaultValue, final boolean quietly) {
try {
return registry.convert(type, value, defaultValue);
} catch (final Exception e) {
if(quietly){
return defaultValue;
}
throw e;
}
}
/**
* JSON递归转换<br>
* 首先尝试JDK类型转换如果失败尝试JSON转Bean<br>
@ -81,7 +104,7 @@ public class JSONConverter implements Converter {
return (T) Base64.decode((CharSequence) value);
} else if (targetClass.isAssignableFrom(Date.class) || targetClass.isAssignableFrom(TemporalAccessor.class)) {
// 用户指定了日期格式获取日期属性时使用对应格式
final String valueStr = Convert.toStr(value);
final String valueStr = convertWithCheck(String.class, value, null, true);
if (null == valueStr) {
return null;
}
@ -137,7 +160,7 @@ public class JSONConverter implements Converter {
}
}
final T targetValue = Convert.convertWithCheck(targetType, value, null, ignoreError);
final T targetValue = convertWithCheck(targetType, value, null, ignoreError);
if (null == targetValue && false == ignoreError) {
if (StrUtil.isBlankIfStr(value)) {