com.konduto.sdk.adapters.KondutoPaymentCollectionDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
Easily integrate with Konduto (https://konduto.com), a fraud prevention service.
package com.konduto.sdk.adapters;
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.konduto.sdk.models.KondutoPayment;
import com.konduto.sdk.models.KondutoPaymentType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
/**
*
* Deserialization of KondutoPayment collections.
*
*/
public class KondutoPaymentCollectionDeserializer implements JsonDeserializer> {
/**
* Method to deserialize a JSON object into a collection of KondutoPayment.
*
* @param json a serialized object
* @param typeOfT the object type
* @param context GSON serialization context
* @return an ArrayList of payments
* @throws JsonParseException
*/
@Override
public Collection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Collection payments = new ArrayList();
for(JsonElement je : json.getAsJsonArray()) {
JsonObject jo = (JsonObject) je;
String pmtTypeUpper = jo.get("type").getAsString().toUpperCase();
KondutoPaymentType type = KondutoPaymentType.valueOf(pmtTypeUpper);
KondutoPayment pmt = type.deserialize(jo, context);
if(jo.has("description")) {
pmt.setDescription(jo.get("description").getAsString());
}
if(jo.has("amount")) {
pmt.setAmount(jo.get("amount").getAsDouble());
}
payments.add(pmt);
}
return payments;
}
}