Gson 序列化内部类

在本章中,我们将解释具有内部类的类的序列化/反序列化。

 

1. 嵌套的内部类示例

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());

1) 范例

让我们看一个带有内部类的类的序列化/反序列化的示例。在C:> GSON_WORKSPACE中创建名为 GsonTester 的Java类文件。

文件: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;
   }
}

2) 验证结果

使用 javac 编译器编译类如下 -

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行GsonTester查看结果 -

C:\GSON_WORKSPACE>java GsonTester

验证输出。

{"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

 

2. 嵌套静态内部类示例

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());

1) 范例

让我们看一个带有静态内部类的类的序列化/反序列化的示例。在C:> GSON_WORKSPACE中创建名为GsonTester的Java类文件。

文件: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;
   }
}

2) 验证结果

使用 javac 编译器编译类如下:

C:\GSON_WORKSPACE>javac GsonTester.java

现在运行 GsonTester 查看结果:

C:\GSON_WORKSPACE>java GsonTester

验证输出。

{"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

Gson 使用其内置适配器执行对象的序列化/反序列化。它还支持自定义适配器。我们将讨论如何创建自定义适配器以及如何使用它。 1. 创建自定义适配器通过扩展 TypeAdapter 类并将其传递给目标对象的类 ...