Search in sources :

Example 1 with MenigaOfferPage

use of com.meniga.sdk.models.offers.MenigaOfferPage in project mobile-sdk-android by meniga.

the class MenigaOfferConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfOffers = new TypeToken<MenigaOfferPage>() {
    }.getType();
    Type typeOfOffer = new TypeToken<MenigaOffer>() {
    }.getType();
    if (typeOfOffers.equals(type)) {
        return new Converter<ResponseBody, MenigaOfferPage>() {

            @Override
            public MenigaOfferPage convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaOfferPage page = gson.fromJson(new JsonArray(), MenigaOfferPage.class);
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement element;
                try {
                    element = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                JsonObject object = element.getAsJsonObject();
                JsonArray arr = object.getAsJsonArray("data");
                Collections.addAll(page, gson.fromJson(arr, MenigaOffer[].class));
                JsonObject meta = object.getAsJsonObject("meta");
                return MenigaConverter.mergeMeta(gson, page, meta);
            }
        };
    } else if (typeOfOffer.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public Object convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                return gson.fromJson(getAsObject(resBody.byteStream()), MenigaOffer.class);
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaOfferPage(com.meniga.sdk.models.offers.MenigaOfferPage) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) MenigaOffer(com.meniga.sdk.models.offers.MenigaOffer) Converter(retrofit2.Converter) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 MenigaOffer (com.meniga.sdk.models.offers.MenigaOffer)1 MenigaOfferPage (com.meniga.sdk.models.offers.MenigaOfferPage)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ResponseBody (okhttp3.ResponseBody)1 Converter (retrofit2.Converter)1