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

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

There is a newer version: 1.13.13
Show newest version
package com.eligible.json.deserializer;

import com.eligible.model.APIErrorResponse;
import com.eligible.model.claim.Error;
import com.eligible.net.APIResource;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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;

/**
 * {@link JsonDeserializer} for deserializing {@link Error}.
 */
public class ClaimErrorDeserializer implements JsonDeserializer {

    /**
     * {@link Gson} deserializer for {@link APIErrorResponse}.
     */
    public static final Gson GSON = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .create();

    /**
     * {@inheritDoc}
     */
    public Error deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        // Error could be a list of String.class, or a list of Error.class. Mapping String.class to Error.class
        if (json.isJsonPrimitive()) {
            return new Error(json.getAsString());
        } else {
            return GSON.fromJson(json, Error.class);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy