Gson - Excluding fields from Serialization



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

Example

Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.

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

Compile the classes using javac compiler as follows −

C:\GSON_WORKSPACE>javac GsonTester.java 

Now run the GsonTester to see the result −

C:\GSON_WORKSPACE>java GsonTester 

Verify the output

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

Using excludeFieldsWithModifiers

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

Example

Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.

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

Compile the classes using javac compiler as follows −

C:\GSON_WORKSPACE>javac GsonTester.java

Now run the GsonTester to see the result −

C:\GSON_WORKSPACE>java GsonTester 

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

Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.

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

Compile the classes using javac compiler as follows −

C:\GSON_WORKSPACE>javac GsonTester.java

Now run the GsonTester to see the result −

C:\GSON_WORKSPACE>java GsonTester 

Verify the output

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