com.konduto.sdk.adapters.KondutoShoppingCartAdapter 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.*;
import com.konduto.sdk.models.KondutoItem;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created by rsampaio on 11/08/14.
*
* Shopping cart (collection of KondutoItem), serialization/deserialization.
*
*/
public class KondutoShoppingCartAdapter
implements JsonSerializer>, JsonDeserializer> {
@Override
public Collection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
Collection shoppingCart = new ArrayList();
for(JsonElement je : json.getAsJsonArray()) {
shoppingCart.add((KondutoItem) context.deserialize(je, KondutoItem.class));
}
return shoppingCart;
}
@Override
public JsonElement serialize(Collection shoppingCart, Type typeOfSrc, JsonSerializationContext context) {
JsonArray json = new JsonArray();
for(KondutoItem item : shoppingCart) {
json.add(context.serialize(item));
}
return json;
}
}