package com.kbs.kbsintranett;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class InputsAdapter implements JsonDeserializer<List<GravityField>> {
    @Override
    public List<GravityField> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if (json.isJsonNull()) {
            return new ArrayList<>();
        }
        // Fikser krasjen: Hvis Gravity Forms sender "" i stedet for [], returner tom liste
        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
            return new ArrayList<>();
        }
        if (json.isJsonArray()) {
            List<GravityField> list = new ArrayList<>();
            for (JsonElement e : json.getAsJsonArray()) {
                list.add(context.deserialize(e, GravityField.class));
            }
            return list;
        }
        return new ArrayList<>();
    }
}