Java Google Gson.toJson为何丢失数据
我有五门课:Java Google Gson.toJson为何丢失数据,java,json,generics,arraylist,gson,Java,Json,Generics,Arraylist,Gson,我有五门课: 评论,文件,文件,文件,文件 注释是文本的持有者。 论文是空的抽象类。 WoundPaper扩展Paper并存储字符串和注释的数组列表 Document是一个抽象类,当gson对WoundDoc进行序列化时,它会存储的ArrayList,它能告诉我们的是有一个列表,其中有两个扩展纸张类型的对象(list我通常自己创建json,而不需要第三方库。这只是因为json不需要任何头。为什么要为每个需要序列化的对象编写自定义序列化程序呢?如果有记录良好、经过测试且广泛部署的库可以为您实现这一
评论
,文件
,文件
,文件
,文件
注释
是文本的持有者。论文
是空的抽象类。WoundPaper
扩展Paper
并存储字符串和注释的数组列表
Document
是一个抽象类,当gson对WoundDoc
进行序列化时,它会存储的ArrayList,它能告诉我们的是有一个列表
,其中有两个扩展纸张
类型的对象(list我通常自己创建json,而不需要第三方库。这只是因为json不需要任何头。为什么要为每个需要序列化的对象编写自定义序列化程序呢?如果有记录良好、经过测试且广泛部署的库可以为您实现这一点?@beresfordt这似乎是一种很好的方法,而且可能更好或者我。我会试试的。谢谢!
public class Comment {
private final String text;
public static class Builder {
private final String text;
public Builder(String text) {
this.text = text;
}
public Comment build(){
return new Comment(this);
}
}
private Comment(Builder builder) {
this.text = builder.text;
}
public String getText() {
return text;
}
}
public abstract class Paper {
protected Paper(ArrayList<Comment> commentList) {
}
}
public class WoundPaper extends Paper {
private final String imageUri;
private final ArrayList<Comment> commentList;
public static class Builder {
private final String imageUri;
private final ArrayList<Comment> commentList;
public Builder(String imageUri, ArrayList<Comment> commentList) {
this.imageUri = imageUri;
this.commentList = commentList;
}
public WoundPaper build() {
return new WoundPaper(this);
}
}
private WoundPaper(Builder builder) {
super(builder.commentList);
this.imageUri = builder.imageUri;
this.commentList = builder.commentList;
}
}
public abstract class Document {
private final ArrayList<? extends Paper> paperList;
protected Document(ArrayList<? extends Paper> paperList) {
this.paperList = paperList;
}
}
public class WoundDoc extends Document {
public static class Builder {
private final ArrayList<WoundPaper> paperList;
public Builder(ArrayList<WoundPaper> paperList) {
this.paperList = paperList;
}
public WoundDoc build() {
return new WoundDoc(this);
}
}
private WoundDoc(Builder builder) {
super(builder.paperList);
}
}
Comment comment = new Comment.Builder("comment").build();
ArrayList<Comment> commentList = new ArrayList<Comment>();
commentList.add(comment);
commentList.add(comment);
WoundPaper woundPaper = new WoundPaper.Builder("some Uri", commentList).build();
ArrayList<WoundPaper> woundPaperList = new ArrayList<WoundPaper>();
woundPaperList.add(woundPaper);
woundPaperList.add(woundPaper);
WoundDoc woundDoc = new WoundDoc.Builder(woundPaperList).build();
System.out.println("woundDoc to JSON >> " + gson.toJson(woundDoc));
public abstract class Document<T extends Paper> {
private final ArrayList<T> paperList;
protected Document(ArrayList<T> paperList) {
this.paperList = paperList;
}
}
public class WoundDoc extends Document<WoundPaper> {