use of retrofit2.Converter in project plaid by nickbutcher.
the class DenvelopingConverter method responseBodyConverter.
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
// This converter requires an annotation providing the name of the payload in the envelope;
// if one is not supplied then return null to continue down the converter chain.
final String payloadName = getPayloadName(annotations);
if (payloadName == null)
return null;
final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody body) throws IOException {
try (JsonReader jsonReader = gson.newJsonReader(body.charStream())) {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
if (payloadName.equals(jsonReader.nextName())) {
return adapter.read(jsonReader);
} else {
jsonReader.skipValue();
}
}
return null;
} finally {
body.close();
}
}
};
}
use of retrofit2.Converter in project mobile-sdk-android by meniga.
the class MenigaChallengesConverter method responseBodyConverter.
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
Type typeOfChallenge = new TypeToken<MenigaChallenge>() {
}.getType();
Type typeOfChallenges = new TypeToken<List<MenigaChallenge>>() {
}.getType();
if (typeOfChallenge.equals(type)) {
return new Converter<ResponseBody, MenigaChallenge>() {
@Override
public MenigaChallenge convert(ResponseBody resBody) throws IOException {
return factory.getMenigaChallengeItem(getAsObject(resBody.byteStream()));
}
};
} else if (typeOfChallenges.equals(type)) {
return new Converter<ResponseBody, Object>() {
@Override
public Object convert(ResponseBody resBody) throws IOException {
JsonArray arr = getAsArray(resBody.byteStream());
List<MenigaChallenge> challenges = new ArrayList<>();
for (JsonElement element : arr) {
MenigaChallenge challenge = factory.getMenigaChallengeItem((JsonObject) element);
if (challenge.getTitle().equals("GlobalSpendingMeterMarker")) {
challenge.setType(ChallengeType.METER);
}
challenges.add(challenge);
}
return challenges;
}
};
}
return null;
}
use of retrofit2.Converter 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;
}
use of retrofit2.Converter 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;
}
Aggregations