com.unzer.payment.communication.JsonWebhookEnumListConverter Maven / Gradle / Ivy
package com.unzer.payment.communication;
import com.google.gson.*;
import com.unzer.payment.webhook.WebhookEventEnum;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class JsonWebhookEnumListConverter
implements JsonDeserializer>, JsonSerializer> {
@Override
public JsonElement serialize(List src, Type typeOfSrc,
JsonSerializationContext context) {
JsonArray jsonArray = new JsonArray(src.size());
for (WebhookEventEnum webhookEvent : src) {
jsonArray.add(webhookEvent.getEventName());
}
return jsonArray;
}
@Override
public List deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) {
List result = new ArrayList();
try {
if (json.isJsonArray()) {
JsonArray jsonArray = json.getAsJsonArray();
for (JsonElement jsonValue : jsonArray) {
result.add(WebhookEventEnum.fromEventName(jsonValue.getAsString()));
}
}
return result;
} catch (Exception e) {
throw new JsonParseException("Cannot parse webhook event " + json.toString() + ".");
}
}
}