Gson 定制型適配器
gson 定制型適配器
gson 使用其內置適配器執(zhí)行對象的序列化/反序列化。它還支持自定義適配器。我們將討論如何創(chuàng)建自定義適配器以及如何使用它。
1. 創(chuàng)建自定義適配器
通過擴展 typeadapter 類并將其傳遞給目標對象的類型來創(chuàng)建自定義適配器。重寫 讀取 和 寫入 方法以分別執(zhí)行自定義反序列化和序列化。
class studentadapter extends typeadapter<student> { @override public student read(jsonreader reader) throws ioexception { ... } @override public void write(jsonwriter writer, student student) throws ioexception { } }
2. 注冊自定義適配器
使用gsonbuilder注冊自定義適配器和使用創(chuàng)造gson實例 gsonbuilder 。
gsonbuilder builder = new gsonbuilder(); builder.registertypeadapter(student.class, new studentadapter()); gson gson = builder.create();
3. 使用適配器
gson現(xiàn)在將使用自定義適配器將json文本轉換為對象,反之亦然。
string jsonstring = "{\"name\":\"mahesh\", \"rollno\":1}"; student student = gson.fromjson(jsonstring, student.class); system.out.println(student); jsonstring = gson.tojson(student); system.out.println(jsonstring);
1) 范例
讓我們看一下自定義類型適配器的實例。在c:> gson_workspace中創(chuàng)建名為 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\":\"mahesh\", \"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+ "]"; } }
2) 驗證結果
使用 javac 編譯器編譯類如下:
c:\gson_workspace>javac gsontester.java
現(xiàn)在運行 gsontester 查看結果:
c:\gson_workspace>java gsontester
驗證輸出。
student[ name = mahesh, roll no: 1] { "name": "mahesh", "rollno": 1 }
相關文章