Alice52 / java-ocean

java-tutorial .We intend to explain Java knowledge in this repository.
MIT License
0 stars 0 forks source link

[json] fastjson #50

Closed Alice52 closed 3 years ago

Alice52 commented 4 years ago
  1. @JSONField(serialize = false) can ignore property when stringfy

  2. fastjson default not serialize the property when value is null

  3. boolean

    • default stingfy is true, not ""true
    • but "true" also can deserialize to relative model
    • recommand: dot not use "true" when property is boolean type
Alice52 commented 4 years ago
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());
  }
}
Alice52 commented 3 years ago

Now please deprecate this lib, using Jackson instead