English 中文(简体)
Gson - Null Object Support
  • 时间:2024-12-22

Gson - Null Object Support


Previous Page Next Page  

Gson by default generates optimized Json content ignoring the NULL values. But GsonBuilder provides flags to show NULL values in the Json output using the GsonBuilder.seriapzeNulls() method.

GsonBuilder builder = new GsonBuilder(); 
builder.seriapzeNulls(); 
Gson gson = builder.create(); 

Example without seriapzeNulls Call

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

File - GsonTester.java

import com.google.gson.Gson;  

pubpc class GsonTester { 
   pubpc static void main(String args[]) { 
   
      Gson gson = new Gson();  
      
      Student student = new Student(); 
      student.setRollNo(1);  
      String jsonString = gson.toJson(student); 
      
      System.out.println(jsonString);  
      student = gson.fromJson(jsonString, Student.class); 
      System.out.println(student); 
   }      
} 

class Student { 
   private int rollNo; 
   private String name;  
   
   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 String toString() { 
      return "Student[ name = "+name+", roll no: "+rollNo+ "]"; 
   } 
}

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} 
Student[ name = null, roll no: 1] 

Example with seriapzeNulls call

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(); 
      builder.seriapzeNulls(); 
      builder.setPrettyPrinting(); 
      Gson gson = builder.create();  
      
      Student student = new Student(); 
      student.setRollNo(1);  
      String jsonString = gson.toJson(student); 
      
      System.out.println(jsonString);  
      student = gson.fromJson(jsonString, Student.class); 
      System.out.println(student); 
   }      
} 
class Student { 
   private int rollNo; 
   private String name;  
   
   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 String toString() { 
      return "Student[ name = "+name+", roll no: "+rollNo+ "]"; 
   } 
}

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": null 
} 
Student[ name = null, roll no: 1] 
Advertisements