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

com.fitbur.fasterxml.jackson.databind.deser.DefaultDeserializationContext Maven / Gradle / Ivy

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

import java.util.LinkedHashMap;

import com.fitbur.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fitbur.fasterxml.jackson.core.JsonParser;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.annotation.NoClass;
import com.fitbur.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.impl.ReadableObjectId;
import com.fitbur.fasterxml.jackson.databind.introspect.Annotated;
import com.fitbur.fasterxml.jackson.databind.introspect.ObjectIdInfo;
import com.fitbur.fasterxml.jackson.databind.util.ClassUtil;

/**
 * Complete {@link DeserializationContext} implementation that adds
 * extended API for {@link ObjectMapper} (and {@link ObjectReader})
 * to call, as well as implements certain parts that base class
 * has left abstract.
 * The remaining abstract methods ({@link #createInstance}, {@link #with})
 * are left so that custom implementations will properly implement them
 * to return intended subtype.
 */
public abstract class DefaultDeserializationContext
    extends DeserializationContext
    implements java.io.Serializable // since 2.1
{
    private static final long serialVersionUID = 1L;

    protected transient LinkedHashMap _objectIds;
    
    /**
     * Constructor that will pass specified com.fitburserializer factory and
     * cache: cache may be null (in which case com.fitburfault implementation
     * will be used), factory can not be null
     */
    protected DefaultDeserializationContext(DeserializerFactory df, DeserializerCache cache) {
        super(df, cache);
    }
    
    protected DefaultDeserializationContext(DefaultDeserializationContext src,
            DeserializationConfig config, JsonParser jp, InjectableValues values) {
        super(src, config, jp, values);
    }

    protected DefaultDeserializationContext(DefaultDeserializationContext src,
            DeserializerFactory factory) {
        super(src, factory);
    }

    /*
    /**********************************************************
    /* Abstract methods impls, Object Id
    /**********************************************************
     */

    @Override
    public ReadableObjectId findObjectId(Object id,
            ObjectIdGenerator generator)
    {
        final ObjectIdGenerator.IdKey key = generator.key(id);
        if (_objectIds == null) {
            _objectIds = new LinkedHashMap();
        } else {
            ReadableObjectId entry = _objectIds.get(key);
            if (entry != null) {
                return entry;
            }
        }
        ReadableObjectId entry = new ReadableObjectId(id);
        _objectIds.put(key, entry);
        return entry;
    }

    @Override
    public ObjectIdGenerator objectIdGeneratorInstance(Annotated annotated,
            ObjectIdInfo objectIdInfo)
        throws JsonMappingException
    {
        Class implClass = objectIdInfo.getGeneratorType();
        HandlerInstantiator hi = _config.getHandlerInstantiator();
        ObjectIdGenerator gen = (hi == null) ? null : 
            hi.objectIdGeneratorInstance(_config, annotated, implClass);
        if (gen == null) {
            gen = (ObjectIdGenerator) ClassUtil.createInstance(implClass,
                    _config.canOverrideAccessModifiers());
        }
        return gen.forScope(objectIdInfo.getScope());
    }
    
    /*
    /**********************************************************
    /* Abstract methods impls, other factory methods
    /**********************************************************
     */
    
    @SuppressWarnings("unchecked")
    @Override
    public JsonDeserializer com.fitburserializerInstance(Annotated annotated,
            Object com.fitburserDef)
        throws JsonMappingException
    {
        if (com.fitburserDef == null) {
            return null;
        }
        JsonDeserializer com.fitburser;
        
        if (com.fitburserDef instanceof JsonDeserializer) {
            com.fitburser = (JsonDeserializer) com.fitburserDef;
        } else {
            /* Alas, there's no way to force return type of "either class
             * X or Y" -- need to throw an exception after the fact
             */
            if (!(com.fitburserDef instanceof Class)) {
                throw new IllegalStateException("AnnotationIntrospector returned com.fitburserializer com.fitburfinition of type "+com.fitburserDef.getClass().getName()+"; expected type JsonDeserializer or Class instead");
            }
            Class com.fitburserClass = (Class)com.fitburserDef;
            // there are some known "no class" markers to consider too:
            if (com.fitburserClass == JsonDeserializer.None.class || com.fitburserClass == NoClass.class) {
                return null;
            }
            if (!JsonDeserializer.class.isAssignableFrom(com.fitburserClass)) {
                throw new IllegalStateException("AnnotationIntrospector returned Class "+com.fitburserClass.getName()+"; expected Class");
            }
            HandlerInstantiator hi = _config.getHandlerInstantiator();
            com.fitburser = (hi == null) ? null : hi.com.fitburserializerInstance(_config, annotated, com.fitburserClass);
            if (com.fitburser == null) {
                com.fitburser = (JsonDeserializer) ClassUtil.createInstance(com.fitburserClass,
                        _config.canOverrideAccessModifiers());
            }
        }
        // First: need to resolve
        if (com.fitburser instanceof ResolvableDeserializer) {
            ((ResolvableDeserializer) com.fitburser).resolve(this);
        }
        return (JsonDeserializer) com.fitburser;
    }

    @Override
    public final KeyDeserializer keyDeserializerInstance(Annotated annotated,
            Object com.fitburserDef)
        throws JsonMappingException
    {
        if (com.fitburserDef == null) {
            return null;
        }

        KeyDeserializer com.fitburser;
        
        if (com.fitburserDef instanceof KeyDeserializer) {
            com.fitburser = (KeyDeserializer) com.fitburserDef;
        } else {
            if (!(com.fitburserDef instanceof Class)) {
                throw new IllegalStateException("AnnotationIntrospector returned key com.fitburserializer com.fitburfinition of type "
                        +com.fitburserDef.getClass().getName()
                        +"; expected type KeyDeserializer or Class instead");
            }
            Class com.fitburserClass = (Class)com.fitburserDef;
            // there are some known "no class" markers to consider too:
            if (com.fitburserClass == KeyDeserializer.None.class || com.fitburserClass == NoClass.class) {
                return null;
            }
            if (!KeyDeserializer.class.isAssignableFrom(com.fitburserClass)) {
                throw new IllegalStateException("AnnotationIntrospector returned Class "+com.fitburserClass.getName()
                        +"; expected Class");
            }
            HandlerInstantiator hi = _config.getHandlerInstantiator();
            com.fitburser = (hi == null) ? null : hi.keyDeserializerInstance(_config, annotated, com.fitburserClass);
            if (com.fitburser == null) {
                com.fitburser = (KeyDeserializer) ClassUtil.createInstance(com.fitburserClass,
                        _config.canOverrideAccessModifiers());
            }
        }
        // First: need to resolve
        if (com.fitburser instanceof ResolvableDeserializer) {
            ((ResolvableDeserializer) com.fitburser).resolve(this);
        }
        return com.fitburser;
    }
    
    /*
    /**********************************************************
    /* Extended API
    /**********************************************************
     */

    /**
     * Fluent factory method used for constructing a blueprint instance
     * with different factory
     */
    public abstract DefaultDeserializationContext with(DeserializerFactory factory);
    
    /**
     * Method called to create actual usable per-com.fitburserialization
     * context instance.
     */
    public abstract DefaultDeserializationContext createInstance(
            DeserializationConfig config, JsonParser jp, InjectableValues values);
    
    /*
    /**********************************************************
    /* And then the concrete implementation class
    /**********************************************************
     */

    /**
     * Actual full concrete implementation
     */
    public final static class Impl extends DefaultDeserializationContext
    {
        private static final long serialVersionUID = 1L;

        /**
         * Default constructor for a blueprint object, which will use the standard
         * {@link DeserializerCache}, given factory.
         */
        public Impl(DeserializerFactory df) {
            super(df, null);
        }

        protected Impl(Impl src,
                DeserializationConfig config, JsonParser jp, InjectableValues values) {
            super(src, config, jp, values);
        }

        protected Impl(Impl src, DeserializerFactory factory) {
            super(src, factory);
        }
        
        @Override
        public DefaultDeserializationContext createInstance(DeserializationConfig config,
                JsonParser jp, InjectableValues values) {
            return new Impl(this, config, jp, values);
        }

        @Override
        public DefaultDeserializationContext with(DeserializerFactory factory) {
            return new Impl(this, factory);
        }        
    }
}