Skip to content

Instantly share code, notes, and snippets.

@dron247
Created March 28, 2018 04:32
Show Gist options
  • Save dron247/c68475421734b53df27aa657d9abdc45 to your computer and use it in GitHub Desktop.
Save dron247/c68475421734b53df27aa657d9abdc45 to your computer and use it in GitHub Desktop.
Custom gson deserializer
package com.messapps.carpo.network.custom_converters;
import android.support.annotation.Nullable;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.messapps.carpo.components.objects.payout.JsonFunding;
import com.messapps.carpo.components.objects.payout.JsonIndividual;
import com.messapps.carpo.components.objects.payout.JsonPayout;
import com.messapps.carpo.network.responses.payment.PayoutDetailsResponse;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Custom converter for {@link PayoutDetailsResponse}
* We have some troubles with backend response typing and the app is already live, so we can not
* change the response as we please
*/
public final class PayoutDetailsResponseDeserializer
implements JsonDeserializer<PayoutDetailsResponse> {
Gson mGson;
public PayoutDetailsResponseDeserializer() {
super();
mGson = new Gson();
}
/**
* Gson invokes this call-back method during deserialization when it encounters a field of the
* specified type.
* <p>In the implementation of this call-back method, you should consider invoking
* {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects
* for any non-trivial field of the returned object. However, you should never invoke it on the
* the same type passing {@code json} since that will cause an infinite loop (Gson will call your
* call-back method again).
*
* @param json The Json data being deserialized
* @param typeOfT The type of the Object to deserialize to
* @param context
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
* @throws JsonParseException if json is not in the expected format of {@code typeofT}
*/
@Override
public PayoutDetailsResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
PayoutDetailsResponse result = new PayoutDetailsResponse();
JsonObject response = json.getAsJsonObject();
//retrieve status code
result.statusCode = response.getAsJsonPrimitive("statusCode").getAsString();
//retrieve errors list, if errors - return empty list
try {
List<String> errors = new ArrayList<>(2);
JsonArray array = response.get("errors").getAsJsonArray();
for (JsonElement element : array) {
String error = element.getAsString();
errors.add(error);
}
result.errors = errors;
} catch (IllegalStateException ise) {
result.errors = Collections.emptyList();
}
//retrieve a message
result.message = response.getAsJsonPrimitive("message").getAsString();
//retrieve payout object
result.result = getPayout(response.get("result").getAsJsonObject());
return result;
}
/**
* Parse payout object
*
* @param input
* @return
*/
private JsonPayout getPayout(JsonObject input) {
JsonPayout result = JsonPayout.createForJsonParser();
result.setMerchantId(input.getAsJsonPrimitive("merchantId").getAsString());
result.setStatus(input.getAsJsonPrimitive("status").getAsString());
result.setIndividual(getIndividual(input.get("individual").getAsJsonObject()));
result.setFunding(getFunding(input.get("funding").getAsJsonObject()));
return result;
}
/**
* Converts provided {@link JsonObject} into {@link JsonIndividual}
*
* @param input json source
* @return parsed {@link JsonIndividual} or <bold>null</bold>
*/
@Nullable
private JsonIndividual getIndividual(JsonObject input) {
JsonIndividual individual;
try {
individual = mGson.fromJson(input, JsonIndividual.class);
} catch (Exception e) {
//we can not recover from any exception here, just return null
individual = null;
}
return individual;
}
/**
* Converts provided {@link JsonObject} into {@link JsonFunding}
*
* @param input json source
* @return parsed {@link JsonFunding} or <bold>null</bold>
*/
@Nullable
private JsonFunding getFunding(JsonObject input) {
JsonFunding funding;
try {
funding = mGson.fromJson(input, JsonFunding.class);
} catch (Exception e) {
//we can not recover from any exception here, just return null
funding = null;
}
return funding;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment