Gson 定制型适配器
gson 定制型适配器
gson 使用其内置适配器执行对象的序列化/反序列化。它还支持自定义适配器。我们将讨论如何创建自定义适配器以及如何使用它。
1. 创建自定义适配器
通过扩展 typeadapter 类并将其传递给目标对象的类型来创建自定义适配器。重写 读取 和 写入 方法以分别执行自定义反序列化和序列化。
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注册自定义适配器和使用创造gson实例 gsonbuilder 。
gsonbuilder builder = new gsonbuilder(); builder.registertypeadapter(student.class, new studentadapter()); gson gson = builder.create();
3. 使用适配器
gson现在将使用自定义适配器将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中创建名为 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
现在运行 gsontester 查看结果:
c:\gson_workspace>java gsontester
验证输出。
student[ name = mahesh, roll no: 1] { "name": "mahesh", "rollno": 1 }