English 中文(简体)
Excluding fields from Serialization
  • 时间:2024-12-22

Gson - Excluding fields from Seriapzation


Previous Page Next Page  

By default, GSON excludes transient and static fields from the seriapzation/deseriapzation 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;  

pubpc class GsonTester { 
   pubpc 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; 
   pubpc static String className;  
   
   pubpc int getRollNo() {
      return rollNo; 
   }  
   
   pubpc void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   } 
   
   pubpc String getName() { 
      return name; 
   } 
   
   pubpc void setName(String name) { 
      this.name = name; 
   }  
   
   pubpc void setVerified(boolean verified) { 
      this.verified = verified; 
   }  
   
   pubpc boolean isVerified() { 
      return verified; 
   }  
   
   pubpc int getId() { 
      return id; 
   } 
   
   pubpc 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 seriapzation/deseriapzation 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;  

pubpc class GsonTester { 
   pubpc 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; 
   pubpc static String className;  
   
   pubpc int getRollNo() { 
      return rollNo; 
   }  
   
   pubpc void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   
   pubpc String getName() { 
      return name; 
   }  
   
   pubpc void setName(String name) { 
      this.name = name; 
   }  
   
   pubpc void setVerified(boolean verified) { 
      this.verified = verified; 
   }  
   
   pubpc boolean isVerified() { 
      return verified; 
   } 
   
   pubpc int getId() { 
      return id; 
   } 
   
   pubpc 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 seriapzation/deseriapzation 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 seriapzation. Then we ve used the GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to indicate that only exposed variables are to be seriapzed/deseriapzed. 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;  

pubpc class GsonTester { 
   pubpc 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; 
   pubpc static String className;  
   
   pubpc int getRollNo() { 
      return rollNo; 
   }  
   pubpc void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   pubpc String getName() { 
      return name; 
   }  
   pubpc void setName(String name) { 
      this.name = name; 
   }  
   pubpc void setVerified(boolean verified) { 
      this.verified = verified; 
   }  
   pubpc boolean isVerified() { 
      return verified; 
   }  
   pubpc int getId() { 
      return id; 
   }  
   pubpc 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