Gson 简明教程

Gson - Excluding fields from Serialization

默认情况下,GSON 会将瞬态和静态字段排除在序列化/反序列化过程中。让我们来看一下下面的示例。

Example

在 C:\>GSON_WORKSPACE 创建一个名为 GsonTester 的 Java 类文件。

File − GsonTester.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonTester {
   public static void main(String args[]) {

      GsonBuilder builder = new GsonBuilder();
      Gson gson = builder.create();

      Student student = new Student();
      student.setRollNo(1);
      student.setName("Mahesh Kumar");
      student.setVerified(true);
      student.setId(1);
      student.className = "VI";

      String jsonString = gson.toJson(student);
      System.out.println(jsonString);
   }
}

class Student {
   private int rollNo;
   private String name;
   private boolean verified;
   private transient int id;
   public static String className;

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

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

   public void setVerified(boolean verified) {
      this.verified = verified;
   }

   public boolean isVerified() {
      return verified;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
}

Verify the result

使用以下 javac 编译器编译类:

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 以查看结果 −

C:\GSON_WORKSPACE>java GsonTester

验证输出

{"rollNo":1,"name":"Mahesh Kumar","verified":true}

Using excludeFieldsWithModifiers

GsonBuilder 提供对使用 excludeFieldsWithModifiers() 方法排除具有特殊修饰符的字段的控制,以排除序列化/反序列化过程。请参阅以下示例。

Example

在 C:\>GSON_WORKSPACE 中创建一个名为 GsonTester 的 Java 类文件。

File − GsonTester.java

import java.lang.reflect.Modifier;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonTester {
   public static void main(String args[]) {

      GsonBuilder builder = new GsonBuilder();
      builder.excludeFieldsWithModifiers(Modifier.TRANSIENT);
      Gson gson = builder.create();

      Student student = new Student();
      student.setRollNo(1);
      student.setName("Mahesh Kumar");
      student.setVerified(true);
      student.setId(1);
      student.className = "VI";

      String jsonString = gson.toJson(student);
      System.out.println(jsonString);
   }
}

class Student {
   private int rollNo;
   private String name;
   private boolean verified;
   private transient int id;
   public static String className;

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

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

   public void setVerified(boolean verified) {
      this.verified = verified;
   }

   public boolean isVerified() {
      return verified;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
}

Verify the result

使用以下 javac 编译器编译类:

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 以查看结果 −

C:\GSON_WORKSPACE>java GsonTester

验证输出

{"rollNo":1,"name":"Mahesh Kumar","verified":true,"className":"VI"}

Using @Expose Annotation

Gson 提供 @Expose 注释来控制基于类的作用域的 Json 序列化/反序列化。考虑具有变量具有 @Expose 支持的以下类。此类中, namerollno 变量公开序列化。然后,我们使用 GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法来指示仅公开变量将被序列化/反序列化。请参阅以下示例。

Example

在 C:\>GSON_WORKSPACE 创建一个名为 GsonTester 的 Java 类文件。

File − GsonTester.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

public class GsonTester {
   public static void main(String args[]) {

      GsonBuilder builder = new GsonBuilder();
      builder.excludeFieldsWithoutExposeAnnotation();
      Gson gson = builder.create();

      Student student = new Student();
      student.setRollNo(1);
      student.setName("Mahesh Kumar");
      student.setVerified(true);
      student.setId(1);
      student.className = "VI";

      String jsonString = gson.toJson(student);
      System.out.println(jsonString);
   }
}
class Student {
   @Expose
   private int rollNo;

   @Expose
   private String name;
   private boolean verified;
   private int id;
   public static String className;

   public int getRollNo() {
      return rollNo;
   }
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public void setVerified(boolean verified) {
      this.verified = verified;
   }
   public boolean isVerified() {
      return verified;
   }
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
}

Verify the result

使用以下 javac 编译器编译类:

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 以查看结果 −

C:\GSON_WORKSPACE>java GsonTester

验证输出

{"rollNo":1,"name":"Mahesh Kumar"}