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);
}
};
}
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;
}
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;
}
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;
}
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;
}
Aggregations