Gson中Expose注解的使用

接着上篇SerializedName注解的使用 来看下Expise 注解的使用。

该注解只有在 你使用GsonBuilder去构造Gson时,同时调用excludeFieldsWithoutExposeAnnotation()方法 才起作用。

public @interface Expose {
  
  /**
   * If {@code true}, the field marked with this annotation is written out in the JSON while
   * serializing. If {@code false}, the field marked with this annotation is skipped from the
   * serialized output. Defaults to {@code true}.
   * @since 1.4
   */
  public boolean serialize() default true;

  /**
   * If {@code true}, the field marked with this annotation is deserialized from the JSON.
   * If {@code false}, the field marked with this annotation is skipped during deserialization. 
   * Defaults to {@code true}.
   * @since 1.4
   */
  public boolean deserialize() default true;
}

该类有来过那个方法,serialize() 设置是否序列化,默认值是true,deserialize() 用于设置是否反序列化,默认值也是true,也就是说,如果我们这样写,该字段是无论是是序列化还是反序列化都会执行了的。

@Expose
private int age;

如果写成这样:

 @Expose(deserialize = false,serialize = false)
 private int age;

  那说明该字段既不参与序列化也不参与反序列化。

看下实体类:

public class TestMode {
   @Expose(deserialize = false,serialize = false)
    private int age;

    @Expose(serialize = false, deserialize = true)
    String name;
    @Expose
    @SerializedName(value = "len",alternate = {"height","width"})
    int length;

    public TestMode() {
        if (false) {
        }
    }

    public TestMode(int age, String name, int length) {
        this.age = age;
        this.name = name;
        this.length = length;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "TestMode{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", length=" + length +
                '}';
    }
}

测试方法:

    public static void main(String args[]) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.excludeFieldsWithoutExposeAnnotation();
        Gson gson = gsonBuilder.create();
        TestMode testMode = new TestMode(30, "明", 170);
        System.out.println("序列化:" + gson.toJson(testMode));
        String json = "{\"age\":30,\"name\":\"明\",\"len\":20,\"width\":80,\"height\":70}";
        TestMode jsTestMode = gson.fromJson(json, TestMode.class);
        System.out.println("反序列化:" + jsTestMode.toString());
    }

结果:


序列化:{"len":170}
反序列化:TestMode{age=0, name='明', length=70}

要注意一点,使用GsonBuilder构建Gson,他的实体类,必须要有Expose注解,除非你不想让该字段参加序列化和反序列化。

那我们上边说了必须要调用excludeFieldsWithoutExposeAnnotation()方法  才会生效,

 public GsonBuilder excludeFieldsWithoutExposeAnnotation() {
    //其实就是为excluder重新负值了,那么不调用此方法呢?
    excluder = excluder.excludeFieldsWithoutExposeAnnotation();
    return this;
  }
private Excluder excluder = Excluder.DEFAULT;

其实他本身是有一个默认值的。

public static final Excluder DEFAULT = new Excluder();

Excluder 类没有重写空的的构造函数,重新给deserialize  和serialize复制,那么他们使用的就是默认的值,都是true,所以即参加序列化,也参加反序列化。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章