Gson - Serializzazione delle classi interne

In questo capitolo, spiegheremo la serializzazione / deserializzazione delle classi che hanno classi interne.

Esempio di classe interna annidata

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

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

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

Esempio

Vediamo un esempio di serializzazione / deserializzazione di una classe con una classe interna in azione. Crea un file di classe Java denominatoGsonTester in C: \> GSON_WORKSPACE.

File − GsonTester.java

import com.google.gson.Gson;  

public class GsonTester { 
   public 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;  
   
   public int getRollNo() { 
      return rollNo; 
   }
   
   public void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   
   public Name getName() { 
      return name; 
   } 
   
   public void setName(Name name) { 
      this.name = name; 
   }
   
   class Name { 
      public String firstName; 
      public String lastName; 
   } 
}

Verifica il risultato

Compila le classi usando javac compilatore come segue -

C:\GSON_WORKSPACE>javac GsonTester.java

Ora esegui GsonTester per vedere il risultato -

C:\GSON_WORKSPACE>java GsonTester

Verifica l'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

Esempio di classe interna statica annidata

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

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

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

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

Esempio

Vediamo un esempio di serializzazione / deserializzazione di una classe con una classe interna statica in azione. Crea un file di classe Java denominato GsonTester in C: \> GSON_WORKSPACE.

File − GsonTester.java

import com.google.gson.Gson;  

public class GsonTester { 
   public 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;  
   
   public int getRollNo() { 
      return rollNo; 
   }  
   
   public void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   
   public Name getName() { 
      return name; 
   }  
   
   public void setName(Name name) { 
      this.name = name; 
   } 
   
   static class Name { 
      public String firstName; 
      public String lastName; 
   } 
}

Verifica il risultato

Compila le classi usando javac compilatore come segue -

C:\GSON_WORKSPACE>javac GsonTester.java

Ora esegui il file GsonTester per vedere il risultato -

C:\GSON_WORKSPACE>java GsonTester

Verifica l'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