Created
October 23, 2014 05:41
-
-
Save RyanRamchandar/2b3b6b0aea16d2a82096 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.website.app; | |
import com.google.gson.TypeAdapter; | |
import com.google.gson.stream.JsonReader; | |
import com.google.gson.stream.JsonToken; | |
import com.google.gson.stream.JsonWriter; | |
import com.website.app.models.Monument; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class MonumentsTypeAdapter extends TypeAdapter<List<Monument>> { | |
@Override public void write(JsonWriter out, List<Monument> value) throws IOException { | |
// Not implemented | |
} | |
@Override public List<Monument> read(JsonReader in) throws IOException { | |
JsonToken token = in.peek(); | |
switch (token) { | |
case BEGIN_ARRAY: | |
List<Monument> monuments = new ArrayList<Monument>(); | |
in.beginArray(); | |
while (in.hasNext()) { | |
Monument m = // TODO Deserialize using appropriate default GSON TypeAdapter? | |
monuments.add(m); | |
} | |
in.endArray(); | |
return monuments; | |
case BEGIN_OBJECT: | |
Monument m = // TODO Deserialize using appropriate default GSON TypeAdapter? | |
return Collections.singletonList(m); | |
case NULL: | |
in.nextNull(); | |
return null; | |
default: | |
throw new IllegalStateException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment