我有五节课:
CommentPaperWoundPaperDocumentWoundDoc
Comment是文本的持有人。Paper是空的抽象类。WoundPaper扩展了Paper并存储了Comments的String和ArrayList。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) {
    }

}

WoundPaper类:
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));

但是输出很奇怪:

将伤口文档转换为JSON >> {“paperList”:[{},{}]}

正如我之前显示的,WoundDoc存储WoundPaper的列表,每个WoundPaper存储comment的列表,但是为什么输出中没有comment

最佳答案

当gson序列化WoundDoc时,它只能说明存在两个对象类型的List,它们扩展了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以将类型传递给Document:
public class WoundDoc extends Document<WoundPaper> {

如果您无法进行上述更改,另一种解决方法是为WoundDoc编写自定义序列化程序

我个人会使用第一个解决方案和通过类型,因为我很懒,编写自定义序列化程序会更费力

编辑:对jackson稍加喊叫,如果您尝试序列化某些内容,它将抛出异常,但无法解决该问题。

09-05 15:09