This commit is contained in:
Looly
2022-03-24 23:26:07 +08:00
parent 2854d2c320
commit 0ed9c13f7a
8 changed files with 99 additions and 13 deletions

View File

@@ -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());
}
}

View File

@@ -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);

View File

@@ -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));
}
/**

View File

@@ -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) {

View File

@@ -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模式下忽略注入

View File

@@ -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());
}
}