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

com.eligible.json.deserializer.FinancialFlowListDeserializer Maven / Gradle / Ivy

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