我有五个类(class):
Comment
, Paper
, WoundPaper
, Document
, WoundDoc
.
Comment
是文本的容器。
Paper
是空抽象类。
WoundPaper
延伸Paper
并存储 Comments
的字符串和数组列表.
Document
是抽象类,存储<? extends Paper>
的ArrayList .
WoundDoc
延伸Document
.
您可以在下面看到这些类:
评论类:
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;
}
}
WoundDoc 类:
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);
}
}
现在我必须创建一个 WoundDoc
的实例并通过 Gson 将其转换为 JSON 字符串。这是执行此操作的示例代码:
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));
但是输出很奇怪:
woundDoc to JSON >> {"paperList":[{},{}]}
正如我之前展示的那样,WoundDoc
店铺列表 WoundPaper
每个 WoundPaper
店铺列表 comment
s.但是为什么没有comment
在输出中?
最佳答案
当 gson 开始序列化 WoundDoc
时它只能告诉我们有一个 List
扩展类型为 something 的两个对象 Paper
( List<? extends Paper>
);具体类型未知。作为Paper
没有可供 gson 使用的字段,它只能说该列表中有两个条目,但因为它们是 Paper
类型,它没有字段,因此无法确定如何序列化这些对象。
解决这个问题的一种方法是将类型从您的实现传递到抽象类,这样当 gson 检查它们时,它可以看到它遇到的对象是哪个类的实例,从而找出如何序列化它们。
更新文档以获取类型参数:
public abstract class Document<T extends Paper> {
private final ArrayList<T> paperList;
protected Document(ArrayList<T> paperList) {
this.paperList = paperList;
}
}
更新 WoundDoc 以将类型传递给文档:
public class WoundDoc extends Document<WoundPaper> {
如果您无法进行上述更改,另一种解决方法是为 WoundDoc
编写自定义序列化程序。
我个人会使用第一种解决方案并传递类型,因为我很懒,而且编写自定义序列化程序更费力
编辑:未成年人大声喊叫jackson如果您尝试序列化某些内容并且无法弄清楚如何去做,这将引发异常。
关于java - 为什么 Google Gson.toJson 会丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460625/