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

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