2015-09-07 76 views
0

在ReceivePartyPlaylistEvent對象下面我試圖使用GSON庫序列化。這對播放列表和歌曲類有關聯。序列化具有Collection屬性的枚舉到JSON字符串

public final class ReceivePartyPlaylistEvent extends Event { 
     private PartyPlayList partyPlayList; 

} 

PartyPlaylist枚舉如我需要它是單個實例

public enum PartyPlayList implements Playlist{ 
    INSTANCE; 

    private List<Song> songList = new ArrayList<>(); 

    public List<Song> getSongList() { 
     return songList; 
    } 
} 

和歌曲對象像。兩個類擴展了這個Song對象並實現了抽象方法。

public abstract class Song{ 
    private String songID; 
    private String trackTitle; 
    private String trackAlbum; 
    private String trackArtist; 
    private String albumArtUrl; 
    .... setters and getters... 

    } 

現在,當我嘗試使用GSON(2.2.4)它給我一個空白輸出到串行ReceivePartyPlaylistEvent。 所以我如何序列化這個。

序列化:

//_receivePartyPlaylistEvent has dummy values in it. 
    Gson gson = new Gson(); 
    return gson.toJson(_receivePartyPlaylistEvent); 

回答

0

你還沒有指定什麼partyPlayList所以它的默認爲空和GSON不會對其進行序列化。

嘗試:

private PartyPlayList partyPlayList = PartyPlayList.INSTANCE; 
+0

沒有那不是的情況下,在測試情況下,我增加了虛擬的播放列表中的歌曲的集合 –