1.Gson支持格式化的打印
Gson默認使用toJson()的時候,打印出來的json是壓縮的,也就是節點之間沒有空格的,例如,這樣:
{"album_id":"ddd","iamge_file":"dfkdlfdf","image_title":"lfjdlfjdlfjdljfldfdf","tags":[]}
可以通過開啓格式化,如:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(someObject);
打印出來的結果就會變成這樣,方便在調試的時候查看json.
{
"album_id": "ddd",
"iamge_file": "dfkdlfdf",
"image_title": "lfjdlfjdlfjdljfldfdf",
"tags": []
}
2.Gson支持空對象
Gson在把Java對象解析成Json的時候是默認忽略掉null的字段,當我們需要把這些值爲null的字段也顯示出來的時候,我們可以JsonBuilder的serializeNulls()方法來解析這些空值.例如:
public class Dog {
private String name;
public Dog(){}
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
GsonBuilder builder=new GsonBuilder();
//builder.serializeNulls();
Gson gson=builder.create();
Dog dog1=new Dog();
String json = gson.toJson(dog1);
System.out.println(json);
}
}
注掉builder.serializeNulls();跟打開這行代碼的結果:
注掉:
{}
打開:
{"name":null}
3.Gson支持版本
同一個Java類可以使用@Since註解來區分不同的版本.如果沒有版本註解,默認解析所有的屬性.下面是demo:
public class VersionedClass {
@Since(1.1) private final String newerField;
@Since(1.0) private final String newField;
private final String field;
public VersionedClass() {
this.newerField = "newer";
this.newField = "new";
this.field = "old";
}
}
例子:
VersionedClass someObject = new VersionedClass();
Gson gson = new GsonBuilder().setVersion(1.0).create();
String jsonOutput = gson.toJson(someObject);
System.out.println("1.0版本:"+jsonOutput);
System.out.println();
gson = new GsonBuilder().setVersion(1.1).create();
System.out.println("1.1版本:"+gson.toJson(someObject));
System.out.println();
gson = new Gson();
jsonOutput = gson.toJson(someObject);
System.out.println("默認:"+jsonOutput);
打印的結果:
1.0版本:{"newField":"new","field":"old"}
1.1版本:{"newerField":"newer","newField":"new","field":"old"}
默認:{"newerField":"newer","newField":"new","field":"old"}
4.支持屬性別名
可以使用@SerializedName註解爲屬性添加別名
例子:
public class SomeObject {
@SerializedName("custom_naming") private final String someField;
private final String someOtherField;
public SomeObject(String a, String b) {
this.someField = a;
this.someOtherField = b;
}
public static void main(String[] args) {
SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().create();
String jsonRepresentation = gson.toJson(someObject);
System.out.println(jsonRepresentation);
}
}
打印的結果:
{"custom_naming":"first","someOtherField":"second"}
也可以使用一些Gson內置的轉換名字策略
- FieldNamingPolicy.IDENTITY
- FieldNamingPolicy.LOWER_CASE_WITH_DASHES
- FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES
- FieldNamingPolicy.UPPER_CAMEL_CASE
- FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES
1.FieldNamingPolicy.IDENTITY
就是跟java類中一致
2.FieldNamingPolicy.LOWER_CASE_WITH_DASHES
例如:
- someFieldName —> some-field-name
- _someFieldName —> _some-field-name
- aStringField —> a-string-field
- aURL —> a-u-r-l
3.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES
例如:
- someFieldName —> some_field_name
- _someFieldName —> _some_field_name
- aStringField —> a_string_field
- aURL —> a_u_r_l
4.FieldNamingPolicy.UPPER_CAMEL_CASE
例如:
- someFieldName —> SomeFieldName
- _someFieldName —> _SomeFieldName
5.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES
例如:
- someFieldName —> Some Field Name
- _someFieldName —> _Some Field Name
使用的方式:
SomeObject someObject = new SomeObject("first", "second");
Gson gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
String jsonRepresentation = gson.toJson(someObject);
System.out.println(jsonRepresentation);
打印結果:
{"custom_naming":"first","some_other_field":"second"}
注意前者的優先級高於後者,當使用前者的時候,按照前者的設置