Last active
February 2, 2016 14:06
-
-
Save cosminstefanxp/3a77bf6a3b524d84fd3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bang_olufsen.bnrc.content.playableContent; | |
import com.bang_olufsen.bnrc.BnrProduct; | |
import com.bang_olufsen.bnrc.GsonFactory; | |
import com.bang_olufsen.bnrc.common.Image; | |
import com.bang_olufsen.bnrc.core.BnrObject; | |
import com.google.common.base.MoreObjects; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonSyntaxException; | |
import com.google.gson.TypeAdapter; | |
import com.google.gson.stream.JsonReader; | |
import com.google.gson.stream.JsonWriter; | |
import java.io.IOException; | |
public class ArtistV2 implements BnrObject { | |
public final String id; | |
public final String name; | |
public final String artistNameNormalized; | |
public final transient Image image; | |
public ArtistV2(String id, String name, String artistNameNormalized, Image image) { | |
this.id = id; | |
this.name = name; | |
this.artistNameNormalized = artistNameNormalized; | |
this.image = image; | |
} | |
@Override | |
public void bindContext(BnrProduct product, String path) { | |
} | |
public static class DeezerArtist extends ArtistV2 { | |
public final Deezer deezer; | |
public DeezerArtist(String id, String name, String artistNameNormalized, | |
Image image, Deezer deezer) { | |
super(id, name, artistNameNormalized, image); | |
this.deezer = deezer; | |
} | |
} | |
public static class DlnaArtist extends ArtistV2 { | |
public final Dlna dlna; | |
public DlnaArtist(String id, String name, String artistNameNormalized, | |
Image image, Dlna dlna) { | |
super(id, name, artistNameNormalized, image); | |
this.dlna = dlna; | |
} | |
} | |
public static TypeAdapter<ArtistV2> buildTypeAdapter() { | |
return new TypeAdapter<ArtistV2>() { | |
@Override | |
public void write(JsonWriter out, ArtistV2 value) throws IOException { | |
GsonFactory.getInstance().toJson(value, ArtistV2.class, out); | |
} | |
@Override | |
public ArtistV2 read(JsonReader in) throws IOException { | |
Gson gson = GsonFactory.getInstance(); | |
String id = null; | |
String name = null; | |
String normalizedName = null; | |
Image image = null; | |
Deezer deezer = null; | |
Dlna dlna = null; | |
in.beginObject(); | |
while (in.hasNext()) { | |
switch (in.nextName()) { | |
case "id": | |
id = in.nextString(); | |
break; | |
case "name": | |
name = in.nextString(); | |
break; | |
case "artistNameNormalized": | |
normalizedName = in.nextString(); | |
break; | |
case "image": | |
image = gson.fromJson(in, Image.class); | |
break; | |
case "deezer": | |
deezer = gson.fromJson(in, Deezer.class); | |
break; | |
case "dlna": | |
dlna = gson.fromJson(in, Dlna.class); | |
break; | |
} | |
} | |
in.endObject(); | |
if (deezer != null) | |
return new DeezerArtist(id, name, normalizedName, image, deezer); | |
else if (dlna != null) | |
return new DlnaArtist(id, name, normalizedName, image, dlna); | |
throw new JsonSyntaxException("Unrecognized Artist type."); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment