mirror of
https://gitee.com/dromara/hutool.git
synced 2025-10-26 18:59:23 +08:00
fix json
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user