为什么 Google Gson.toJson 丢失数据
Why Google Gson.toJson loses data
我有五个 classes:
Comment
,Paper
,WoundPaper
,Document
,WoundDoc
.
Comment
是文本容器。
Paper
空洞而抽象 class.
WoundPaper
扩展 Paper
并存储 Comments
.
的字符串和数组列表
Document
是抽象的class,存放<? extends Paper>
.
的ArrayList
WoundDoc
扩展 Document
.
您可以在下面看到那些 class:
评论class:
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;
}
}
论文class:
public abstract class Paper {
protected Paper(ArrayList<Comment> commentList) {
}
}
伤口纸class:
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;
}
}
文档class:
public abstract class Document {
private final ArrayList<? extends Paper> paperList;
protected Document(ArrayList<? extends Paper> paperList) {
this.paperList = paperList;
}
}
WoundDoc class:
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
实例并将其转换为 JSON 字符串 Gson.This 是一个示例代码:
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.But 的列表,为什么没有 [=36] =] 在输出中?
当 gson 去序列化 WoundDoc
时,它可以告诉我们有一个 List
的两个对象类型扩展 Paper
(List<? extends Paper>
) ;具体类型未知。由于 Paper
没有可供 gson 使用的字段,它只能说该列表中有两个条目,但由于它们是类型 Paper
,没有字段,因此无法工作了解如何序列化这些对象。
解决这个问题的一种方法是将类型从您的实现传递给抽象 classes,这样当 gson 检查它们时,它可以看到它遇到的对象 class 是哪个对象的实例,并且所以弄清楚如何序列化它们。
更新文档以获取类型参数:
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
编写自定义序列化程序
我个人会使用第一个解决方案并传递类型,因为我很懒,而且编写自定义序列化程序更费力
edit: Minor 对 jackson 大喊大叫,如果您尝试序列化某些内容但它无法弄清楚如何去做,这将引发异常。
我有五个 classes:
Comment
,Paper
,WoundPaper
,Document
,WoundDoc
.
Comment
是文本容器。
Paper
空洞而抽象 class.
WoundPaper
扩展 Paper
并存储 Comments
.
的字符串和数组列表
Document
是抽象的class,存放<? extends Paper>
.
的ArrayList
WoundDoc
扩展 Document
.
您可以在下面看到那些 class:
评论class:
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;
}
}
论文class:
public abstract class Paper {
protected Paper(ArrayList<Comment> commentList) {
}
}
伤口纸class:
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;
}
}
文档class:
public abstract class Document {
private final ArrayList<? extends Paper> paperList;
protected Document(ArrayList<? extends Paper> paperList) {
this.paperList = paperList;
}
}
WoundDoc class:
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
实例并将其转换为 JSON 字符串 Gson.This 是一个示例代码:
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.But 的列表,为什么没有 [=36] =] 在输出中?
当 gson 去序列化 WoundDoc
时,它可以告诉我们有一个 List
的两个对象类型扩展 Paper
(List<? extends Paper>
) ;具体类型未知。由于 Paper
没有可供 gson 使用的字段,它只能说该列表中有两个条目,但由于它们是类型 Paper
,没有字段,因此无法工作了解如何序列化这些对象。
解决这个问题的一种方法是将类型从您的实现传递给抽象 classes,这样当 gson 检查它们时,它可以看到它遇到的对象 class 是哪个对象的实例,并且所以弄清楚如何序列化它们。
更新文档以获取类型参数:
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
我个人会使用第一个解决方案并传递类型,因为我很懒,而且编写自定义序列化程序更费力
edit: Minor 对 jackson 大喊大叫,如果您尝试序列化某些内容但它无法弄清楚如何去做,这将引发异常。