All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.konduto.sdk.adapters.KondutoPaymentCollectionDeserializer Maven / Gradle / Ivy

Go to download

Easily integrate with Konduto (https://konduto.com), a fraud prevention service.

There is a newer version: 2.17.4
Show newest version
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;
	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy