mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix json
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
@@ -191,4 +192,19 @@ public final class InternalJSONUtil {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将{@link JSONConfig}参数转换为Bean拷贝所用的{@link CopyOptions}
|
||||
*
|
||||
* @param config {@link JSONConfig}
|
||||
* @return {@link CopyOptions}
|
||||
* @since 5.8.0
|
||||
*/
|
||||
static CopyOptions toCopyOptions(JSONConfig config) {
|
||||
return CopyOptions.create()
|
||||
.setIgnoreCase(config.isIgnoreCase())
|
||||
.setIgnoreError(config.isIgnoreError())
|
||||
.setIgnoreNullValue(config.isIgnoreNullValue())
|
||||
.setTransientSupport(config.isTransientSupport());
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.convert.Converter;
|
||||
import cn.hutool.core.convert.ConverterRegistry;
|
||||
import cn.hutool.core.convert.impl.ArrayConverter;
|
||||
import cn.hutool.core.convert.impl.BeanConverter;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -112,6 +114,16 @@ public class JSONConverter implements Converter<JSON> {
|
||||
//noinspection unchecked
|
||||
return (T) deserializer.deserialize((JSON) value);
|
||||
}
|
||||
|
||||
// issue#2212@Github
|
||||
// 在JSONObject转Bean时,读取JSONObject本身的配置文件
|
||||
if(value instanceof JSONGetter
|
||||
&& targetType instanceof Class && BeanUtil.hasSetter((Class<?>) targetType)){
|
||||
final JSONConfig config = ((JSONGetter<?>) value).getConfig();
|
||||
final Converter<T> converter = new BeanConverter<>(targetType,
|
||||
InternalJSONUtil.toCopyOptions(config).setIgnoreError(ignoreError));
|
||||
return converter.convertWithCheck(value, null, ignoreError);
|
||||
}
|
||||
}
|
||||
|
||||
final T targetValue = Convert.convertWithCheck(targetType, value, null, ignoreError);
|
||||
|
@@ -2,8 +2,6 @@ package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.BeanPath;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.bean.copier.BeanCopier;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
@@ -537,12 +535,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
* @param bean Bean对象
|
||||
*/
|
||||
private void populateMap(Object bean) {
|
||||
BeanCopier.create(bean, this,
|
||||
CopyOptions.create()
|
||||
.setIgnoreCase(config.isIgnoreCase())
|
||||
.setIgnoreError(true)
|
||||
.setIgnoreNullValue(config.isIgnoreNullValue())
|
||||
).copy();
|
||||
BeanUtil.beanToMap(bean, this, InternalJSONUtil.toCopyOptions(config));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -20,9 +20,9 @@ public class JSONSupport implements JSONString, JSONBeanParser<JSON> {
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON String转Bean
|
||||
* JSON转Bean
|
||||
*
|
||||
* @param json JSON String
|
||||
* @param json JSON
|
||||
*/
|
||||
@Override
|
||||
public void parse(JSON json) {
|
||||
|
Reference in New Issue
Block a user