在ReceivePartyPlaylistEvent对象下面我尝试使用GSON库进行序列化。这与播放列表和歌曲类有关联。
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 :(得分:0)
您还没有为partyPlayList
分配任何内容,因此默认情况下它为空,Gson不会将其序列化。
尝试:
private PartyPlayList partyPlayList = PartyPlayList.INSTANCE;