Search in sources :

Example 6 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaBaseConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, T> responseBodyConverter(final Type type, Annotation[] annotations, Retrofit retrofit) {
    return new Converter<ResponseBody, T>() {

        @Override
        public T convert(ResponseBody resBody) throws IOException {
            Gson gson = GsonProvider.getGsonBuilder();
            InputStreamReader isr = new InputStreamReader(resBody.byteStream());
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(isr).getAsJsonObject();
            JsonElement jsonElement;
            if (jsonObject.has("data") && !type.equals(typeOfObject)) {
                jsonElement = jsonObject.get("data");
            } else {
                jsonElement = jsonObject;
            }
            return gson.fromJson(jsonElement, type);
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) JsonParser(com.google.gson.JsonParser)

Example 7 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaCategoryConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfMenigaCategoryList = new TypeToken<List<MenigaCategory>>() {
    }.getType();
    Type typeOfMenigaCategory = new TypeToken<MenigaCategory>() {
    }.getType();
    Type typeOfMenigaUserCategoryList = new TypeToken<List<MenigaUserCategory>>() {
    }.getType();
    Type typeOfMenigaUserCategory = new TypeToken<MenigaUserCategory>() {
    }.getType();
    if (typeOfMenigaCategoryList.equals(type) || typeOfMenigaUserCategoryList.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public List<MenigaCategory> convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory[] catsRaw = gson.fromJson(getAsArray(resBody.byteStream()), MenigaCategory[].class);
                List<MenigaCategory> cats = new ArrayList<>();
                for (MenigaCategory cat : catsRaw) {
                    if (cat.getIsPublic()) {
                        cats.add(cat);
                    } else {
                        cats.add(new MenigaUserCategory(cat));
                    }
                    for (int i = 0; i < cat.getChildren().size(); i++) {
                        MenigaCategory child = cat.getChildren().get(i);
                        if (!child.getIsPublic()) {
                            cat.getChildren().set(i, new MenigaUserCategory(child));
                        }
                    }
                }
                return cats;
            }
        };
    } else if (typeOfMenigaCategory.equals(type) || typeOfMenigaUserCategory.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public MenigaCategory convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory catRaw = gson.fromJson(getAsObject(resBody.byteStream()), MenigaCategory.class);
                if (catRaw.getIsPublic()) {
                    return catRaw;
                } else {
                    return new MenigaUserCategory(catRaw);
                }
            }
        };
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) MenigaCategory(com.meniga.sdk.models.categories.MenigaCategory) IOException(java.io.IOException) MenigaUserCategory(com.meniga.sdk.models.categories.MenigaUserCategory) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaFeedConverter method responseBodyConverter.

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

            @Override
            public MenigaFeed convert(ResponseBody resBody) throws IOException {
                MenigaFeed feed = new MenigaFeed();
                JsonArray arr = getAsArray(resBody.byteStream());
                for (JsonElement element : arr) {
                    feed.add(feedItemFactory.getMenigaFeetItem((JsonObject) element));
                }
                try {
                    InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                    JsonElement jelement = new JsonParser().parse(isr);
                    JsonObject jobject = jelement.getAsJsonObject();
                    if (jobject.has("meta")) {
                        JsonObject meta = jobject.getAsJsonObject("meta");
                        feed.setHasMoreData(meta.get("hasMoreData").getAsBoolean());
                    } else {
                        feed.setHasMoreData(true);
                    }
                } catch (Exception ex) {
                    feed.setHasMoreData(true);
                }
                return feed;
            }
        };
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) JsonObject(com.google.gson.JsonObject) MenigaFeed(com.meniga.sdk.models.feed.MenigaFeed) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonParser(com.google.gson.JsonParser)

Example 9 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaIdNameKeyValConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeListOfNetWorthType = new TypeToken<List<KeyVal<Long, String>>>() {
    }.getType();
    if (typeListOfNetWorthType.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public Object convert(ResponseBody value) throws IOException {
                List<KeyVal<Long, String>> list = new ArrayList<>();
                JsonArray arr = getAsArray(value.byteStream());
                for (int i = 0; i < arr.size(); i++) {
                    JsonObject entry = (JsonObject) arr.get(i);
                    KeyVal<Long, String> item = new KeyVal<>(entry.get("id").getAsLong(), entry.get("name").getAsString());
                    list.add(item);
                }
                return list;
            }
        };
    }
    return null;
}
Also used : KeyVal(com.meniga.sdk.helpers.KeyVal) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) Converter(retrofit2.Converter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaRedemptionConverter method responseBodyConverter.

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

            @Override
            public MenigaRedemptions convert(ResponseBody resBody) throws IOException {
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement element;
                try {
                    element = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaRedemptions page = gson.fromJson(getAsArray(element), MenigaRedemptions.class);
                JsonObject object = element.getAsJsonObject();
                JsonObject meta = object.getAsJsonObject("meta");
                List<MenigaScheduledReimbursement> cashBackRaw = gson.fromJson(meta.getAsJsonArray("scheduledReimbursements"), new TypeToken<List<MenigaScheduledReimbursement>>() {
                }.getType());
                MenigaRedemptions main = MenigaConverter.mergeMeta(gson, page, meta);
                main.setScheduledReimbursement(cashBackRaw);
                return main;
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaScheduledReimbursement(com.meniga.sdk.models.offers.redemptions.MenigaScheduledReimbursement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) MenigaRedemptions(com.meniga.sdk.models.offers.redemptions.MenigaRedemptions) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Converter(retrofit2.Converter) JsonParser(com.google.gson.JsonParser)

Aggregations

Type (java.lang.reflect.Type)19 ResponseBody (okhttp3.ResponseBody)18 Test (org.junit.Test)12 Converter (retrofit2.Converter)10 JsonObject (com.google.gson.JsonObject)8 ParameterizedType (java.lang.reflect.ParameterizedType)8 MediaType (okhttp3.MediaType)8 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)8 JsonElement (com.google.gson.JsonElement)7 IOException (java.io.IOException)7 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)7 Gson (com.google.gson.Gson)6 JsonParser (com.google.gson.JsonParser)6 InputStreamReader (java.io.InputStreamReader)6 JsonArray (com.google.gson.JsonArray)5 Annotation (java.lang.annotation.Annotation)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)4