com.eligible.json.deserializer.FinancialFlowListDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eligible-java Show documentation
Show all versions of eligible-java Show documentation
Java bindings for Eligible APIs (https://eligible.com).
package com.eligible.json.deserializer;
import com.eligible.model.coverage.FinancialFlow;
import com.eligible.net.APIResource;
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.JsonParseException;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* {@link JsonDeserializer} for deserializing {@link FinancialFlow}.
*/
public class FinancialFlowListDeserializer implements JsonDeserializer> {
private static final Type DATE_LIST_TYPE = new TypeToken>() { }.getType();
/** {@inheritDoc} */
public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
final Gson gson = APIResource.GSON;
// List could be a list of FinancialFlow.class, or a list of list of FinancialFlow.class. Merging all into one
List collection = new ArrayList();
JsonArray jsonFinancialFlows = json.getAsJsonArray();
for (JsonElement jsonFinancialFlow : jsonFinancialFlows) {
if (jsonFinancialFlow.isJsonArray()) {
List dates = gson.fromJson(jsonFinancialFlow, DATE_LIST_TYPE);
collection.addAll(dates);
} else {
FinancialFlow date = gson.fromJson(jsonFinancialFlow, FinancialFlow.class);
collection.add(date);
}
}
return collection;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy