Search in sources :

Example 1 with MenigaReimbursementAccountTypePage

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

the class MenigaReimbursementAccountConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfAccount = new TypeToken<MenigaReimbursementAccount>() {
    }.getType();
    Type typeOfAccounts = new TypeToken<MenigaReimbursementAccountPage>() {
    }.getType();
    Type typeOfAccountTypes = new TypeToken<MenigaReimbursementAccountTypePage>() {
    }.getType();
    if (typeOfAccount.equals(type)) {
        return new Converter<ResponseBody, MenigaReimbursementAccount>() {

            @Override
            public MenigaReimbursementAccount convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                return gson.fromJson(getAsObject(resBody.byteStream()), MenigaReimbursementAccount.class);
            }
        };
    } else if (typeOfAccounts.equals(type)) {
        return new Converter<ResponseBody, MenigaReimbursementAccountPage>() {

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

            @Override
            public MenigaReimbursementAccountTypePage convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaReimbursementAccountTypePage page = new MenigaReimbursementAccountTypePage();
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement jelement;
                try {
                    jelement = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                JsonObject jobject = jelement.getAsJsonObject();
                JsonArray arr = jobject.getAsJsonArray("data");
                Collections.addAll(page, gson.fromJson(arr, MenigaReimbursementAccountType[].class));
                JsonObject meta = jobject.getAsJsonObject("meta");
                return MenigaConverter.mergeMeta(gson, page, meta);
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaReimbursementAccountPage(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountPage) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) MenigaReimbursementAccountTypePage(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountTypePage) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) MenigaReimbursementAccountType(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountType) JsonElement(com.google.gson.JsonElement) MenigaReimbursementAccount(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccount) Converter(retrofit2.Converter) 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 MenigaReimbursementAccount (com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccount)1 MenigaReimbursementAccountPage (com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountPage)1 MenigaReimbursementAccountType (com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountType)1 MenigaReimbursementAccountTypePage (com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountTypePage)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