Gson從序列化中排除字段

默認情況下,GSON排除序列化/反序列化過程中的瞬態和靜態字段。 我們來看看下面的例子。

示例

創建一個名爲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("Maxsu"); 
      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; 
   } 
}

執行上面示例代碼,得到以下結果 -

{"rollNo":1,"name":"Maxsu","verified":true}

使用excludeFieldsWithModifiers()方法

GsonBuilder使用序列化/反序列化過程中的excludeFieldsWithModifiers()方法提供對使用特定修飾符排除字段的控制。 看下面的例子。

示例

創建一個名爲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("Maxsu"); 
      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; 
   } 
}

執行上面示例代碼,得到以下結果 -

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

使用@Expose註解

Gson提供[@Expose](https://github.com/Expose "@Expose")註解來根據其範圍控制類的Json序列化/反序列化。 考慮以下具有[@Expose](https://github.com/Expose "@Expose")支持的變量的類。 在這個類中,namerollno變量將被暴露以進行序列化。 然後,我們使用GsonBuilder.excludeFieldsWithoutExposeAnnotation()方法來指示只有暴露的變量要被序列化/反序列化。 看下面的例子。

示例

創建一個名爲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("Maxsu"); 
      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; 
   } 
}

執行上面示例代碼,得到以下結果 -

{"rollNo":1,"name":"Maxsu"}