mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix json
This commit is contained in:
parent
2854d2c320
commit
0ed9c13f7a
@ -16,6 +16,7 @@
|
||||
* 【cron 】 【可能兼容问题】SimpleValueParser改名为AbsValueParser,改为abstract
|
||||
* 【poi 】 【可能兼容问题】ExcelUtil.getBigWriter返回值改为BigExcelWriter
|
||||
* 【core 】 【可能兼容问题】Opt.ofEmptyAble参数由List改为Collection子类(pr#580@Gitee)
|
||||
* 【json 】 【可能兼容问题】JSON转Bean时,使用JSON本身的相关设置,而非默认(issue#2212@Github)
|
||||
|
||||
### 🐣新特性
|
||||
* 【http 】 HttpRequest.form采用TableMap方式(issue#I4W427@Gitee)
|
||||
@ -57,6 +58,7 @@
|
||||
* 【http 】 修复标签误删问题(issue#I4Z7BV@Gitee)
|
||||
* 【core 】 修复Win下文件名带*问题(pr#584@Gitee)
|
||||
* 【core 】 FileUtil.getMimeType增加rar、7z支持(issue#I4ZBN0@Gitee)
|
||||
* 【json 】 JSON修复transient设置无效问题(issue#2212@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.22 (2022-03-01)
|
||||
|
@ -19,4 +19,25 @@ public interface Converter<T> {
|
||||
*/
|
||||
T convert(Object value, T defaultValue) throws IllegalArgumentException;
|
||||
|
||||
}
|
||||
/**
|
||||
* 转换值为指定类型,可选是否不抛异常转换<br>
|
||||
* 当转换失败时返回默认值
|
||||
*
|
||||
* @param value 值
|
||||
* @param defaultValue 默认值
|
||||
* @param quietly 是否静默转换,true不抛异常
|
||||
* @return 转换后的值
|
||||
* @since 5.8.0
|
||||
* @see #convert(Object, Object)
|
||||
*/
|
||||
default T convertWithCheck(Object value, T defaultValue, boolean quietly) {
|
||||
try {
|
||||
return convert(value, defaultValue);
|
||||
} catch (Exception e) {
|
||||
if(quietly){
|
||||
return defaultValue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -188,10 +188,11 @@ public class JSONObjectTest {
|
||||
JSONObject json = JSONUtil.createObj()//
|
||||
.set("strValue", "null")//
|
||||
.set("intValue", 123)//
|
||||
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
|
||||
.set("beanValue", "null")//
|
||||
.set("list", JSONUtil.createArray().set("a").set("b"));
|
||||
|
||||
TestBean bean = json.toBean(TestBean.class);
|
||||
TestBean bean = json.toBean(TestBean.class, true);
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
Assert.assertEquals("null", bean.getStrValue());
|
||||
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
||||
|
@ -12,14 +12,55 @@ public class TransientTest {
|
||||
private String bizNo;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithoutTransientTest(){
|
||||
Bill detailBill = new Bill();
|
||||
detailBill.setId("3243");
|
||||
detailBill.setBizNo("bizNo");
|
||||
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
final JSONObject jsonObject = new JSONObject(detailBill,
|
||||
JSONConfig.create().setTransientSupport(false));
|
||||
Assert.assertEquals("{\"bizNo\":\"bizNo\",\"id\":\"3243\"}", jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithTransientTest(){
|
||||
Bill detailBill = new Bill();
|
||||
detailBill.setId("3243");
|
||||
detailBill.setBizNo("bizNo");
|
||||
|
||||
//noinspection MismatchedQueryAndUpdateOfCollection
|
||||
final JSONObject jsonObject = new JSONObject(detailBill,
|
||||
JSONConfig.create().setTransientSupport(true));
|
||||
Assert.assertEquals("{\"bizNo\":\"bizNo\"}", jsonObject.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithoutTransientToBeanTest(){
|
||||
Bill detailBill = new Bill();
|
||||
detailBill.setId("3243");
|
||||
detailBill.setBizNo("bizNo");
|
||||
|
||||
final JSONObject jsonObject = new JSONObject(detailBill,
|
||||
JSONConfig.create().setTransientSupport(false));
|
||||
Assert.assertEquals("{\"bizNo\":\"bizNo\"}", jsonObject.toString());
|
||||
|
||||
final Bill bill = jsonObject.toBean(Bill.class);
|
||||
Assert.assertEquals("3243", bill.getId());
|
||||
Assert.assertEquals("bizNo", bill.getBizNo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanWithTransientToBeanTest(){
|
||||
Bill detailBill = new Bill();
|
||||
detailBill.setId("3243");
|
||||
detailBill.setBizNo("bizNo");
|
||||
|
||||
final JSONObject jsonObject = new JSONObject(detailBill,
|
||||
JSONConfig.create().setTransientSupport(true));
|
||||
|
||||
final Bill bill = jsonObject.toBean(Bill.class);
|
||||
Assert.assertNull(bill.getId());
|
||||
Assert.assertEquals("bizNo", bill.getBizNo());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user