Search in sources :

Example 1 with MenigaTransaction

use of com.meniga.sdk.models.transactions.MenigaTransaction in project mobile-sdk-android by meniga.

the class MenigaTransactionUpdateConverter method responseBodyConverter.

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

            @Override
            public Object convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(resBody.byteStream());
                    JsonElement element = new JsonParser().parse(isr);
                    isr.close();
                    JsonObject json = element.getAsJsonObject();
                    List<MenigaTransaction> updatedTransactions = new ArrayList<>();
                    if (json.get("data").isJsonArray()) {
                        updatedTransactions = gson.fromJson(json.getAsJsonArray("data"), new TypeToken<List<MenigaTransaction>>() {
                        }.getType());
                    } else {
                        MenigaTransaction updated = gson.fromJson(json.getAsJsonObject("data"), new TypeToken<MenigaTransaction>() {
                        }.getType());
                        updatedTransactions.add(updated);
                    }
                    List<MenigaTransaction> transactions = new ArrayList<>();
                    if (json.has("meta")) {
                        transactions = gson.fromJson(json.getAsJsonArray("meta"), new TypeToken<List<MenigaTransaction>>() {
                        }.getType());
                    }
                    return MenigaTransactionUpdate.fromList(updatedTransactions, transactions);
                } finally {
                    if (isr != null) {
                        isr.close();
                    }
                }
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaTransaction(com.meniga.sdk.models.transactions.MenigaTransaction) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) MenigaTransactionUpdate(com.meniga.sdk.models.transactions.MenigaTransactionUpdate) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.google.gson.JsonParser)

Example 2 with MenigaTransaction

use of com.meniga.sdk.models.transactions.MenigaTransaction in project mobile-sdk-android by meniga.

the class MenigaTransactionOperationsMock method gson.

private MenigaTransactionPage gson() {
    Gson gson = GsonProvider.getGsonBuilder();
    MenigaTransactionPage transactionPage = null;
    try {
        InputStream inputStream = FileImporter.getInputStreamFromRaw("transactions.json");
        MenigaTransaction[] arr = gson.fromJson(MenigaConverter.getAsArray(inputStream), MenigaTransaction[].class);
        transactionPage = new MenigaTransactionPage();
        transactionPage.addAll(Arrays.asList(arr));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return transactionPage;
}
Also used : MenigaTransaction(com.meniga.sdk.models.transactions.MenigaTransaction) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) IOException(java.io.IOException) MenigaTransactionPage(com.meniga.sdk.models.transactions.MenigaTransactionPage)

Aggregations

Gson (com.google.gson.Gson)2 MenigaTransaction (com.meniga.sdk.models.transactions.MenigaTransaction)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 TypeToken (com.google.gson.reflect.TypeToken)1 MenigaTransactionPage (com.meniga.sdk.models.transactions.MenigaTransactionPage)1 MenigaTransactionUpdate (com.meniga.sdk.models.transactions.MenigaTransactionUpdate)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseBody (okhttp3.ResponseBody)1 Converter (retrofit2.Converter)1