Gson自定義型別介面卡


Gson使用其內建介面卡執行物件的序列化/反序列化。 它也支援自定義介面卡。 讓我們來討論如何建立一個自定義介面卡以及如何使用它。

建立自定義介面卡

通過擴充套件TypeAdapter類並傳遞目標型別的物件來建立自定義介面卡。 重寫讀寫方法分別執行自定義的反序列化和序列化。

class StudentAdapter extends TypeAdapter<Student> { 
   @Override 
   public Student read(JsonReader reader) throws IOException { 
      ... 
   } 
   @Override 
   public void write(JsonWriter writer, Student student) throws IOException { 
   } 
}

註冊自定義介面卡

使用GsonBuilder註冊自定義介面卡並使用GsonBuilder建立一個Gson範例。參考以下實現程式碼 -

GsonBuilder builder = new GsonBuilder(); 
builder.registerTypeAdapter(Student.class, new StudentAdapter()); 
Gson gson = builder.create();

使用介面卡

Gson現在將使用自定義介面卡將Json文字轉換為物件,反之亦然。參考以下實現程式碼 -

String jsonString = "{\"name\":\"Maxsu\", \"rollNo\":1}"; 
Student student = gson.fromJson(jsonString, Student.class); 
System.out.println(student);  
jsonString = gson.toJson(student); 
System.out.println(jsonString);

範例

我們來看一個自定義型別介面卡的例子。 建立一個名為GsonTester的Java類檔案:GsonTester.java -

import java.io.IOException;  

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.google.gson.TypeAdapter; 
import com.google.gson.stream.JsonReader; 
import com.google.gson.stream.JsonToken; 
import com.google.gson.stream.JsonWriter;  

public class GsonTester { 
   public static void main(String args[]) { 
      GsonBuilder builder = new GsonBuilder(); 
      builder.registerTypeAdapter(Student.class, new StudentAdapter()); 
      builder.setPrettyPrinting(); 
      Gson gson = builder.create();  

      String jsonString = "{\"name\":\"Maxsu\", \"rollNo\":1}";  
      Student student = gson.fromJson(jsonString, Student.class); 
      System.out.println(student);  

      jsonString = gson.toJson(student); 
      System.out.println(jsonString);  
   }      
}  
class StudentAdapter extends TypeAdapter<Student> { 
   @Override 
   public Student read(JsonReader reader) throws IOException { 
      Student student = new Student(); 
      reader.beginObject(); 
      String fieldname = null; 

      while (reader.hasNext()) { 
         JsonToken token = reader.peek();            

         if (token.equals(JsonToken.NAME)) {     
            //get the current token 
            fieldname = reader.nextName(); 
         } 
         if ("name".equals(fieldname)) {       
            //move to next token 
            token = reader.peek(); 
            student.setName(reader.nextString()); 
         } 
         if("rollNo".equals(fieldname)) { 
            //move to next token 
            token = reader.peek(); 
            student.setRollNo(reader.nextInt()); 
         }               
      } 
      reader.endObject(); 
      return student; 
   }  
   @Override 
   public void write(JsonWriter writer, Student student) throws IOException { 
      writer.beginObject(); 
      writer.name("name"); 
      writer.value(student.getName()); 
      writer.name("rollNo"); 
      writer.value(student.getRollNo()); 
      writer.endObject(); 
   } 
}  
class Student { 
   private int rollNo; 
   private String name;  

   public int getRollNo() { 
      return rollNo; 
   }  
   public void setRollNo(int rollNo) { 
      this.rollNo = rollNo; 
   }  
   public String getName() { 
      return name; 
   }  
   public void setName(String name) { 
      this.name = name; 
   }   
   public String toString() { 
      return "Student[ name = "+name+", roll no: "+rollNo+ "]"; 
   } 
}

執行上面範例程式碼,得到以下結果 -

Student[ name = Maxsu, roll no: 1] 
{ 
   "name": "Maxsu", 
   "rollNo": 1 
}