亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Gson 反序列化繼承公共基類的類型?

如何使用 Gson 反序列化繼承公共基類的類型?

嚕嚕噠 2022-05-21 21:07:48
我有以下層次結構:回復public class Response implements Serializable {    @SerializedName("message")    @Expose    private List<Message> messages;    public List<Message> getMessages() {        return messages;    }    public void setMessages(List<Message> messages) {        this.messages = messages;    }}信息public class Message implements Serializable {        @SerializedName("type")        @Expose        @MessageType        private int type;        @SerializedName("position")        @Expose        @MessagePosition        private String position;        public int getType() {            return type;        }        public String getPosition() {            return position;        }        public void setType(@MessageType int type) {            this.type = type;        }        public void setPosition(@MessagePosition String position) {            this.position = position;        }}文本 -> 消息public class TextMessage extends Message {@SerializedName("text")@Exposeprivate String text;public String getText() {    return text;}public void setText(String text) {    this.text = text;}}圖片 -> 信息public class ImageMessage extends Message {    @SerializedName("attachment")    @Expose    private Attachment attachment;    public Attachment getAttachment() {        return attachment;    }    public void setAttachment(Attachment attachment) {        this.attachment = attachment;    }}嘗試使用 GSon 以這種方式反序列化 Message 會導致(自然)在textor字段處清空attachment字段。我希望有一個最合適的反序列化,它基于響應將在運行時選擇哪種消息類型(即文本或圖像)與要完成的大多數字段匹配。到目前為止,我唯一的想法是:1 - 使用 @JsonAdapter -> 沒用2 - 創建另一個層次結構以在編譯時指向類,例如:---- Response   |    - TextResponse -> List<TextMessage>   |    - ImageResponse -> List<ImageMessage>第二個選項并不是我真正想要的,它讓我增加了類的數量,這種方式可能會變得太復雜而無法應用以后的維護。有誰知道處理這個問題的方法?任何可以應用的框架或概念?提前致謝
查看完整描述

2 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

也許你可以使用 Gson extras RunTimeTypeAdapterFactory。檢查這個例子:


RuntimeTypeAdapterFactory<Message> factory = RuntimeTypeAdapterFactory

    .of(Message.class, "type") // actually type is the default field to determine

                               // the sub class so not needed to set here

                               // but set just to point that it is used

    // assuming value 1 in field "int type" identifies TextMessage

    .registerSubtype(TextMessage.class, "1")

    // and assuming int 2 identifies ImageMessage

    .registerSubtype(ImageMessage.class, "2");

然后GsonBuilder.registerTypeAdapterfactory(factory)用這個。


這只是從 Gson 核心庫中找不到。你需要在這里取。您可能還可以從全球 repo 中找到一些 Maven/Gradle dep 有人已經完成但也許最簡單的就是復制這個文件。


如果您需要修改其行為,它會啟用以后的黑客攻擊。


查看完整回答
反對 回復 2022-05-21
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

我使用GodClass具有所有消息類型字段的方法實現了這一點。

但是您不會在應用程序中將此 POJO 類用作 DTO(數據傳輸對象)。

Json是一個協議,不支持Inheritance等等。

在同一場景中,我為 DTO 實現了這種繼承和層次結構。

PS:我的答案中的 DTO 是我們傳遞的模型,例如 inAdapter或 anActivity等等。


查看完整回答
反對 回復 2022-05-21
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號