Created
October 4, 2015 11:38
-
-
Save Marchuck/44d6aafc444dddcf9940 to your computer and use it in GitHub Desktop.
Generic deserializers with Retrofit & rxJava
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
public abstract class Deserializer<T> implements JsonDeserializer<T> { | |
public abstract Class<T> setDestinationClass(); | |
@Override | |
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
Log.d("Deserializer", "deserialize json: " + json); | |
return new Gson().fromJson(json, setDestinationClass()); | |
} | |
} |
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
public class FastAdapter<T> { | |
private RestAdapter adapter; | |
public PokeApi createPokeApi() { | |
return adapter.create(PokeApi.class); | |
} | |
public FastAdapter(String endpoint, final Class<T> klazz) { | |
GsonBuilder builder = new GsonBuilder(); | |
builder.registerTypeAdapter(klazz, new Deserializer<T>() { | |
@Override | |
public Class<T> setDestinationClass() { | |
return klazz; | |
} | |
}); | |
Gson gson = builder.create(); | |
GsonConverter converter = new GsonConverter(gson); | |
adapter = new RestAdapter.Builder() | |
.setEndpoint(endpoint) | |
.setConverter(converter) | |
.build(); | |
} | |
public PokeApi getPokedexService() { | |
return adapter.create(PokeApi.class); | |
} | |
} |
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
public abstract class ListDeserializer<T> implements JsonDeserializer<List<T>> { | |
public abstract Class<T> setDestinationClass(); | |
@Override | |
public List<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
Log.d("ListDeserializer", "deserialize json: " + json); | |
List<T> list = new ArrayList<>(); | |
JsonArray favouritesArray = json.getAsJsonArray(); | |
for (JsonElement nextFavourieAddress : favouritesArray) | |
list.add((T) context.deserialize(nextFavourieAddress, setDestinationClass())); | |
return list; | |
} | |
} |
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
public class Main{ | |
public static final String TAG = Main.class.getSimpleName(); | |
public static void main(String[] args){ | |
final FastAdapter<PokeMove> adapter = new FastAdapter<>(PokeApi.POKEMON_API_ENDPOINT, PokeMove.class); | |
adapter.getPokedexService().getPokemonMove(1).subscribe(new Subscriber<PokeMove>() { | |
@Override | |
public void onCompleted() { | |
Log.d(TAG, "onCompleted "); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.d(TAG, "onError "); | |
} | |
@Override | |
public void onNext(PokeMove pokeMove) { | |
Log.d(TAG, "onNext pokeMove: " + pokeMove); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment