我有以下架构:
public class Student {
String name;
List<Integer> sequence;
}
我需要我的 Student
对象的 Json
{
name : "Bruce"
sequence : {
index_0 : 5
index_1 : 2
index_2 : 7
index_3 : 8
}
}
The documentation没有明确说明如何为集合编写序列化程序。
最佳答案
您可以创建一个 TypeAdapter
,类似于:
public static class StudentAdapter extends TypeAdapter<Student> {
public void write(JsonWriter writer, Student student)
throws IOException {
if (student == null) {
writer.nullValue();
return;
}
writer.beginObject();
writer.name("name");
writer.value(student.name);
writer.name("sequence");
writeSequence(writer, student.sequence);
writer.endObject();
}
private void writeSequence(JsonWriter writer, List<Integer> seq)
throws IOException {
writer.beginObject();
for (int i = 0; i < seq.size(); i++) {
writer.name("index_" + i);
writer.value(seq.get(i));
}
writer.endObject();
}
@Override
public Student read(JsonReader in) throws IOException {
// This is left blank as an exercise for the reader
return null;
}
}
然后注册
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapter(Student.class, new StudentAdapter());
Gson g = b.create();
如果您对示例学生运行此命令:
Student s = new Student();
s.name = "John Smith";
s.sequence = ImmutableList.of(1,3,4,7); // This is a guava method
System.out.println(g.toJson(s));
输出:
{"name":"John Smith","sequence":{"index_0":1,"index_1":3,"index_2":4,"index_3":7}}
关于java - 具有 Collection 字段的对象的 GSON 自定义序列化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30030945/