自定义类型适配器(Custom Type Adapters)
优质
小牛编辑
133浏览
2023-12-01
Gson使用其内置适配器执行对象的序列化/反序列化。 它还支持自定义适配器。 我们将讨论如何创建自定义适配器以及如何使用它。
创建自定义适配器
通过扩展TypeAdapter类并将其传递给目标对象的类型来创建自定义适配器。 重写read和write方法以分别执行自定义反序列化和序列化。
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\":\"Mahesh\", \"rollNo\":1}";
Student student = gson.fromJson(jsonString, Student.class);
System.out.println(student);
jsonString = gson.toJson(student);
System.out.println(jsonString);
例子 (Example)
让我们看一下自定义类型适配器的实例。 在C:\“GSON_WORKSPACE中创建名为GsonTester的Java类文件。
File: 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+ "]";
}
}
验证结果
使用javac编译器编译类如下 -
C:\GSON_WORKSPACE>javac GsonTester.java
现在运行GsonTester查看结果 -
C:\GSON_WORKSPACE>java GsonTester
验证输出。
Student[ name = Mahesh, roll no: 1]
{
"name": "Mahesh",
"rollNo": 1
}