Gson补充

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"}

注意前者的优先级高于后者,当使用前者的时候,按照前者的设置

参考的是Google官方的教程以及Api文档.

你可能感兴趣的:(json,gson)