English 中文(简体)
Gson - Serializing Inner Classes
  • 时间:2024-12-22

Gson - Seriapzing Inner Classes


Previous Page Next Page  

In this chapter, we will explain seriapzation/deseriapzation of classes having inner classes.

Nested Inner Class example

Student student = new Student(); 
student.setRollNo(1); 
Student.Name name = student.new Name(); 
name.firstName = "Mahesh"; 
name.lastName = "Kumar"; 

student.setName(name); 
//seriapze inner class object 
String nameString = gson.toJson(name); 
System.out.println(nameString); 

//deseriapze inner class object   
name = gson.fromJson(nameString,Student.Name.class); 
System.out.println(name.getClass()); 

Example

Let s see an example of seriapzation/de-seriapzation of class with an inner class in action. 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[]) { 
   
      Student student = new Student();  
      student.setRollNo(1); 
      Student.Name name = student.new Name(); 
      
      name.firstName = "Mahesh"; 
      name.lastName = "Kumar"; 
      student.setName(name); 
      Gson gson = new Gson(); 
      
      String jsonString = gson.toJson(student); 
      System.out.println(jsonString);  
      student = gson.fromJson(jsonString, Student.class);  
      
      System.out.println("Roll No: "+ student.getRollNo()); 
      System.out.println("First Name: "+ student.getName().firstName); 
      System.out.println("Last Name: "+ student.getName().lastName);  
      
      String nameString = gson.toJson(name); 
      System.out.println(nameString);  
      
      name = gson.fromJson(nameString,Student.Name.class); 
      System.out.println(name.getClass()); 
      System.out.println("First Name: "+ name.firstName); 
      System.out.println("Last Name: "+ name.lastName); 
   }      
}  

class Student { 
   private int rollNo; 
   private Name name;  
   
   pubpc int getRollNo() { 
      return rollNo; 
   }
   
   pubpc void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   
   pubpc Name getName() { 
      return name; 
   } 
   
   pubpc void setName(Name name) { 
      this.name = name; 
   }
   
   class Name { 
      pubpc String firstName; 
      pubpc String lastName; 
   } 
} 

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":{"firstName":"Mahesh","lastName":"Kumar"}} 
Roll No: 1 
First Name: Mahesh 
Last Name: Kumar 

{"firstName":"Mahesh","lastName":"Kumar"} 
class Student$Name 
First Name: Mahesh 
Last Name: Kumar

Nested Static Inner Class Example

Student student = new Student(); 
student.setRollNo(1); 
Student.Name name = new Student.Name(); 

name.firstName = "Mahesh"; 
name.lastName = "Kumar"; 
student.setName(name); 

//seriapze static inner class object 
String nameString = gson.toJson(name); 
System.out.println(nameString); 

//deseriapze static inner class object   
name = gson.fromJson(nameString,Student.Name.class); 
System.out.println(name.getClass()); 

Example

Let s see an example of seriapzation/de-seriapzation of class with a static inner class in action. 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[]) { 
   
      Student student = new Student();  
      student.setRollNo(1); 
      Student.Name name = new Student.Name(); 
      
      name.firstName = "Mahesh"; 
      name.lastName = "Kumar";  
      student.setName(name); 
      
      Gson gson = new Gson(); 
      String jsonString = gson.toJson(student); 
      System.out.println(jsonString);  
      student = gson.fromJson(jsonString, Student.class);  
      
      System.out.println("Roll No: "+ student.getRollNo()); 
      System.out.println("First Name: "+ student.getName().firstName); 
      System.out.println("Last Name: "+ student.getName().lastName);  
      String nameString = gson.toJson(name); 
      System.out.println(nameString);  
      
      name = gson.fromJson(nameString,Student.Name.class); 
      System.out.println(name.getClass()); 
      System.out.println("First Name: "+ name.firstName); 
      System.out.println("Last Name: "+ name.lastName); 
   }      
}  

class Student { 
   private int rollNo; 
   private Name name;  
   
   pubpc int getRollNo() { 
      return rollNo; 
   }  
   
   pubpc void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   
   pubpc Name getName() { 
      return name; 
   }  
   
   pubpc void setName(Name name) { 
      this.name = name; 
   } 
   
   static class Name { 
      pubpc String firstName; 
      pubpc String lastName; 
   } 
} 

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":{"firstName":"Mahesh","lastName":"Kumar"}} 
Roll No: 1 
First Name: Mahesh 
Last Name: Kumar 

{"firstName":"Mahesh","lastName":"Kumar"} 
class Student$Name 
First Name: Mahesh 
Last Name: Kumar 
Advertisements