Gson 从序列化中排除字段

默认情况下,GSON 从序列化/反序列化过程中排除瞬态和静态字段。我们来看看下面的例子。

1) 范例

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

文件: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;
   }
}

2) 验证结果

使用 javac 编译器编译类如下 -

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 查看结果 -

C:\GSON_WORKSPACE>java GsonTester

验证输出

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

 

1. 使用 excludeFieldsWithModifiers

GsonBuilder使用来自序列化/反序列化过程的excludeFieldsWithModifiers()方法控制使用特定修饰符排除字段。请参阅以下示例。

1) 范例

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

文件: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;
   }
}

2) 验证结果

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

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 查看结果:

C:\GSON_WORKSPACE>java GsonTester

验证输出

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

 

2. 使用@Expose Annotation

Gson提供 @Expose 注释来根据其范围控制类的Json序列化/反序列化。考虑以下具有 @Expose 支持的变量的类。在此类中,将公开 namerollno 变量以进行序列化。然后我们使用 GsonBuilder.excludeFieldsWithoutExposeAnnotation() 方法来指示只有序列化/反序列化的公开变量。请参阅以下示例。

1) 范例

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

文件: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;
   }
}

2) 验证结果

使用 javac 编译器编译类如下 -

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 查看结果 -

C:\GSON_WORKSPACE>java GsonTester

验证输出

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