2015-04-05 99 views
3

我有五類:爲什麼谷歌Gson.toJson丟失數據

CommentPaperWoundPaperDocumentWoundDoc

Comment是文本的持有者。
Paper是空的和抽象類。
WoundPaper延伸Paper並存儲一個String和一個ArrayList 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) { 
    } 

} 

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.This 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.But的列表爲什麼在輸出沒有comment

+0

我通常在沒有第三方庫的情況下自己創建我的json。僅僅因爲json不需要任何頭文件。 – 2015-04-05 18:35:44

+0

爲什麼要爲您需要序列化的每個對象編寫自定義序列化程序,而這些對象是有文檔記錄的,經過測試的和廣泛部署的庫,可以爲您做到這一點? – beresfordt 2015-04-05 19:47:42

+0

@beresfordt這似乎是一個很好的方法,對我來說可能更好,我會試試。謝謝! – hasanghaforian 2015-04-05 20:31:18

回答

3

當GSON去連載的WoundDoc所有它可以告訴的是,有一種類型的東西延伸PaperList<? extends Paper>)兩個對象的List;具體類型未知。由於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

個人我會使用第一個解決方案和傳遞類型,因爲我很懶,編寫自定義序列化程序我s更多的努力

編輯:小聲喊出jackson這將拋出一個異常,如果你嘗試序列化的東西,它不能解決如何做到這一點。

相關問題