Closed Alice52 closed 3 years ago
code
public class TypeModel {
private byte b;
private short day;
private boolean gender;
private char c;
private int age;
private float f;
private double salary;
private long l;
@JSONField(serialize = false)
private String alias;
private String name;
private Date birthDay;
private Object object;
private List<Object> list;
private Map<String, Integer> map;
...
}
public class JsonParse {
@Test
public void testSerialize() {
TypeModel typeModel = new TypeModel();
typeModel.setAge(12);
typeModel.setB((byte) 2);
typeModel.setBirthDay(new Date());
typeModel.setC('C');
typeModel.setDay((short) 5);
typeModel.setF(2.0F);
typeModel.setGender(true);
typeModel.setL(5L);
typeModel.setAge(18);
typeModel.setSalary(15.20D);
typeModel.setName("zack");
typeModel.setAlias("alice52");
typeModel.setList(Arrays.asList(new Object()));
typeModel.setMap(new HashMap<>());
typeModel.setObject(new Object());
String jsonString = JSON.toJSONString(typeModel);
// {"age":18,"b":2,"birthDay":1575555510221,"c":"C","day":5,"f":2.0,"gender":true,"l":5,"list":[{}],"map":{},"name":"zack","object":{},"salary":15.2}
System.out.println(jsonString);
typeModel.setObject(null);
// fastjson ignore null value property, and labeled by @JSONField(serialize = false)
// {"age":18,"b":2,"birthDay":1575555561523,"c":"C","day":5,"f":2.0,"gender":true,"l":5,"list":[{}],"map":{},"name":"zack","salary":15.2}
System.out.println(JSON.toJSONString(typeModel));
}
@Test
public void testJsonParse() {
String jsonString =
"{\"age\":18,\"b\":2,\"birthDay\":1575554883473,\"c\":\"C\",\"day\":5,\"f\":2.0,\"gender\":true,\"l\":5,\"list\":[{}],\"map\":{},\"name\":\"zack\",\"object\":{},\"salary\":15.2}\n";
TypeModel typeModel = JSON.parseObject(jsonString, TypeModel.class);
// TypeModel{b=2, day=5, gender=true, c=C, age=18, f=2.0, salary=15.2, l=5, name='zack',
// birthDay=Thu Dec 05 22:08:03 CST 2019, object={}, list=[{}], map={}}
System.out.println(typeModel.toString());
String jsonString2 =
"{\"age\":18,\"b\":2,\"birthDay\":1575554883473,\"c\":\"C\",\"day\":5,\"f\":2.0,\"gender\":\"true\",\"l\":5,\"list\":[{}],\"map\":{},\"name\":\"zack\",\"object\":\"null\",\"salary\":15.2}\n";
TypeModel typeModel2 = JSON.parseObject(jsonString2, TypeModel.class);
// TypeModel{b=2, day=5, gender=true, c=C, age=18, f=2.0, salary=15.2, l=5, name='zack',
// birthDay=Thu Dec 05 22:08:03 CST 2019, object=null, list=[{}], map={}}
System.out.println(typeModel2.toString());
String jsonString3 =
"{\"age\":18,\"b\":2,\"birthDay\":1575554883473,\"c\":\"C\",\"day\":5,\"f\":2.0,\"gender\":\"true\",\"l\":5,\"list\":\"null\",\"map\":{},\"name\":\"null\",\"object\":\"163\",\"salary\":15.2}\n";
TypeModel typeModel3 = JSON.parseObject(jsonString3, TypeModel.class);
// TypeModel{b=2, day=5, gender=true, c=C, age=18, f=2.0, salary=15.2, l=5, name='null',
// birthDay=Thu Dec 05 22:08:03 CST 2019, object=163, list=[null], map={}}
System.out.println(typeModel3.toString());
}
}
Now please deprecate this lib, using Jackson instead
@JSONField(serialize = false)
can ignore property when stringfyfastjson default not serialize the property when value is null
boolean
true
, not""true
"true"
also can deserialize to relative model