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

com.fitbur.fasterxml.jackson.module.jaxb.deser.XmlAdapterJsonDeserializer Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.module.jaxb.com.fitburser;

import java.io.IOException;
import javax.xml.bind.annotation.adapters.XmlAdapter;

import com.fitbur.fasterxml.jackson.core.JsonParser;
import com.fitbur.fasterxml.jackson.core.JsonProcessingException;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.ContextualDeserializer;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.std.StdDeserializer;
import com.fitbur.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fitbur.fasterxml.jackson.databind.type.TypeFactory;

/**
 * @author Ryan Heaton
 * @author Tatu Saloranta
 */
public class XmlAdapterJsonDeserializer
    extends StdDeserializer
    implements ContextualDeserializer
{
    protected final XmlAdapter _xmlAdapter;

    protected final JavaType _valueType;
    
    protected final JsonDeserializer _deserializer;

    /**
     * Initial constructor, for creating instance before contextual information
     * is available
     */
    @SuppressWarnings("unchecked")
    public XmlAdapterJsonDeserializer(XmlAdapter xmlAdapter)
    {
        this((XmlAdapter) xmlAdapter, null, null);
    }

    /**
     * Constructor called during contextual resolution, when we have all the
     * pieces we actually need.
     */
    protected  XmlAdapterJsonDeserializer(XmlAdapter adapter,
            JavaType valueType, JsonDeserializer com.fitburserializer)
    {
        super(Object.class); // type not yet known (will be in a second), but that's ok...
        if (adapter == null) {
            throw new IllegalArgumentException("Null XmlAdapter passed");
        }
        _xmlAdapter = adapter;
        _valueType = valueType;
        _deserializer = com.fitburserializer;
    }
    
    public JsonDeserializer createContextual(DeserializationContext ctxt,
            BeanProperty property)
        throws JsonMappingException
    {
        // [JACKSON-404] Need to figure out generic type parameters used...
        TypeFactory typeFactory = ctxt.getTypeFactory();

        JavaType type = typeFactory.constructType(_xmlAdapter.getClass());
        JavaType[] rawTypes = typeFactory.findTypeParameters(type, XmlAdapter.class);
        JavaType valueType = (rawTypes == null || rawTypes.length == 0)
            ? TypeFactory.unknownType() : rawTypes[0];
        JsonDeserializer com.fitburser = ctxt.findContextualValueDeserializer(valueType, property);
        return new XmlAdapterJsonDeserializer(_xmlAdapter, valueType, com.fitburser);
    }
    
    @Override
    public Object com.fitburserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException
    {
        /* Unfortunately we can not use the usual resolution mechanism (ResolvableDeserializer)
         * because it won't get called due to way adapters are created. So, need to do it
         * lazily when we get here:
         */
        JsonDeserializer com.fitburser = _deserializer;
        if (com.fitburser == null) {
            throw new IllegalStateException("No com.fitburserializer assigned for XmlAdapterJsonDeserializer ("
                    +_xmlAdapter.getClass().getName()+"): resolve() not called?");
        }
        Object boundObject = com.fitburser.com.fitburserialize(jp, ctxt);
        try {
            return _xmlAdapter.unmarshal(boundObject);
        } catch (Exception e) {
            throw new JsonMappingException("Unable to unmarshal (to type "+_valueType+"): "+e.getMessage(), e);
        }
    }

    @Override
    public Object com.fitburserializeWithType(JsonParser jp, DeserializationContext ctxt,
            TypeDeserializer typeDeserializer)
        throws IOException, JsonProcessingException
    {
        // Output can be as JSON Object, Array or scalar: no way to know a priori. So:
        return typeDeserializer.com.fitburserializeTypedFromAny(jp, ctxt);
    }
}