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

graphql.servlet.internal.VariablesDeserializer Maven / Gradle / Ivy

There is a newer version: 16.0.0
Show newest version
package graphql.servlet.internal;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Map;

/**
 * @author Andrew Potter
 */
public class VariablesDeserializer extends JsonDeserializer> {
    @Override
    public Map deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return deserializeVariablesObject(p.readValueAs(Object.class), (ObjectMapper) ctxt.findInjectableValue(ObjectMapper.class.getName(), null, null));
    }

    public static Map deserializeVariablesObject(Object variables, ObjectMapper mapper) {
        if (variables instanceof Map) {
            @SuppressWarnings("unchecked")
            Map genericVariables = (Map) variables;
            return genericVariables;
        } else if (variables instanceof String) {
            try {
                return mapper.readValue((String) variables, new TypeReference>() {});
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException("variables should be either an object or a string");
        }
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy