GSON - Excluding fields from Serialization



By default, GSON excludes transient and static fields from the serialization/deserialization process. Lets take a look at the following example.

Example - Default Exclusion

GsonTester.java

package com.tutorialspoint;

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; 
   } 
}   

Output

Run the GsonTester and verify the output.

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

Using excludeFieldsWithModifiers() Method

GsonBuilder provides control over excluding fields with particular modifier using excludeFieldsWithModifiers() method from serialization/deserialization process. See the following example.

Example - Customized Exclusion

GsonTester.java

package com.tutorialspoint;

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; 
   } 
} 

Output

Run the GsonTester and verify the output.

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

Using @Expose Annotation

Gson provides @Expose annotation to control the Json serialization/deserialization of a class based on its scope. Consider the following class with a variable having @Expose support. In this class, name and rollno variables are to be exposed for serialization. Then we've used the GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to indicate that only exposed variables are to be serialized/deserialized. See the following example.

Example - Usage of @Expose Annotation

GsonTester.java

package com.tutorialspoint;

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; 
   } 
}

Output

Run the GsonTester and verify the output.

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