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註解來根據其範圍控制類的Json序列化/反序列化。 考慮以下具有@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"}