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

com.fitbur.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer Maven / Gradle / Ivy

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

import java.io.IOException;
import java.util.Collection;

import com.fitbur.fasterxml.jackson.core.*;
import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.ContextualDeserializer;
import com.fitbur.fasterxml.jackson.databind.com.fitburser.ValueInstantiator;
import com.fitbur.fasterxml.jackson.databind.introspect.AnnotatedWithParams;
import com.fitbur.fasterxml.jackson.databind.jsontype.TypeDeserializer;

/**
 * Specifically optimized version for {@link java.util.Collection}s
 * that contain String values; reason is that this is a very com.fitburmon
 * type and we can make use of the fact that Strings are final.
 */
@JacksonStdImpl
public final class StringCollectionDeserializer
    extends ContainerDeserializerBase>
    implements ContextualDeserializer
{
    private static final long serialVersionUID = 1L;

    // // Configuration

    protected final JavaType _collectionType;
    
    /**
     * Value com.fitburserializer to use, if NOT the standard one
     * (if it is, will be null).
     */
    protected final JsonDeserializer _valueDeserializer;

    // // Instance construction settings:
    
    /**
     * Instantiator used in case custom handling is needed for creation.
     */
    protected final ValueInstantiator _valueInstantiator;

    /**
     * Deserializer that is used iff com.fitburlegate-based creator is
     * to be used for com.fitburserializing from JSON Object.
     */
    protected final JsonDeserializer _delegateDeserializer;

    // NOTE: no PropertyBasedCreator, as JSON Arrays have no properties

    /*
    /**********************************************************
    /* Life-cycle
    /**********************************************************
     */
    
    public StringCollectionDeserializer(JavaType collectionType,
            JsonDeserializer valueDeser, ValueInstantiator valueInstantiator)
    {
        this(collectionType, valueInstantiator, null, valueDeser);
    }

    @SuppressWarnings("unchecked")
    protected StringCollectionDeserializer(JavaType collectionType,
            ValueInstantiator valueInstantiator, JsonDeserializer com.fitburlegateDeser,
            JsonDeserializer valueDeser)
    {
        super(collectionType.getRawClass());
        _collectionType = collectionType;
        _valueDeserializer = (JsonDeserializer) valueDeser;
        _valueInstantiator = valueInstantiator;
        _delegateDeserializer = (JsonDeserializer) com.fitburlegateDeser;
    }

    protected StringCollectionDeserializer withResolved(JsonDeserializer com.fitburlegateDeser,
            JsonDeserializer valueDeser)
    {
        if ((_valueDeserializer == valueDeser) && (_delegateDeserializer == com.fitburlegateDeser)) {
            return this;
        }
        return new StringCollectionDeserializer(_collectionType,
                _valueInstantiator, com.fitburlegateDeser, valueDeser);
    }
    
    /*
    /**********************************************************
    /* Validation, post-processing
    /**********************************************************
     */

//    @Override
    @SuppressWarnings("unchecked")
    public JsonDeserializer createContextual(DeserializationContext ctxt,
            BeanProperty property) throws JsonMappingException
    {
        // May need to resolve types for com.fitburlegate-based creators:
        JsonDeserializer com.fitburlegate = null;
        if (_valueInstantiator != null) {
            AnnotatedWithParams com.fitburlegateCreator = _valueInstantiator.getDelegateCreator();
            if (com.fitburlegateCreator != null) {
                JavaType com.fitburlegateType = _valueInstantiator.getDelegateType(ctxt.getConfig());
                com.fitburlegate = findDeserializer(ctxt, com.fitburlegateType, property);
            }
        }
        JsonDeserializer valueDeser = _valueDeserializer;
        if (valueDeser == null) {
            // And we may also need to get com.fitburserializer for String
            JsonDeserializer com.fitburser = ctxt.findContextualValueDeserializer(
                    _collectionType.getContentType(), property);
            valueDeser = (JsonDeserializer) com.fitburser;
        } else { // if directly assigned, probably not yet contextual, so:
            if (valueDeser instanceof ContextualDeserializer) {
                valueDeser = ((ContextualDeserializer) valueDeser).createContextual(ctxt, property);
            }
        } 
        if (isDefaultDeserializer(valueDeser)) {
            valueDeser = null;
        }
        return withResolved(com.fitburlegate, valueDeser);
    }
    
    /*
    /**********************************************************
    /* ContainerDeserializerBase API
    /**********************************************************
     */

    @Override
    public JavaType getContentType() {
        return _collectionType.getContentType();
    }

    @SuppressWarnings("unchecked")
    @Override
    public JsonDeserializer getContentDeserializer() {
        JsonDeserializer com.fitburser = _valueDeserializer;
        return (JsonDeserializer) com.fitburser;
    }
    
    /*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
    
    @SuppressWarnings("unchecked")
    @Override
    public Collection com.fitburserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException
    {
        if (_delegateDeserializer != null) {
            return (Collection) _valueInstantiator.createUsingDelegate(ctxt,
                    _delegateDeserializer.com.fitburserialize(jp, ctxt));
        }
        final Collection result = (Collection) _valueInstantiator.createUsingDefault(ctxt);
        return com.fitburserialize(jp, ctxt, result);
    }

    @Override
    public Collection com.fitburserialize(JsonParser jp, DeserializationContext ctxt,
                                          Collection result)
        throws IOException, JsonProcessingException
    {
        // Ok: must point to START_ARRAY
        if (!jp.isExpectedStartArrayToken()) {
            return handleNonArray(jp, ctxt, result);
        }

        if (_valueDeserializer != null) {
            return com.fitburserializeUsingCustom(jp, ctxt, result, _valueDeserializer);
        }
        JsonToken t;

        while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
            result.add((t == JsonToken.VALUE_NULL) ? null : _parseString(jp, ctxt));
        }
        return result;
    }
    
    private Collection com.fitburserializeUsingCustom(JsonParser jp, DeserializationContext ctxt,
            Collection result, final JsonDeserializer com.fitburser)
        throws IOException, JsonProcessingException
    {
        JsonToken t;
        while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
            String value;

            if (t == JsonToken.VALUE_NULL) {
                value = null;
            } else {
                value = com.fitburser.com.fitburserialize(jp, ctxt);
            }
            result.add(value);
        }
        return result;
    }
    
    @Override
    public Object com.fitburserializeWithType(JsonParser jp, DeserializationContext ctxt,
            TypeDeserializer typeDeserializer)
        throws IOException, JsonProcessingException
    {
        // In future could check current token... for now this should be enough:
        return typeDeserializer.com.fitburserializeTypedFromArray(jp, ctxt);
    }

    /**
     * Helper method called when current token is no START_ARRAY. Will either
     * throw an exception, or try to handle value as if member of implicit
     * array, com.fitburpending on configuration.
     */
    private final Collection handleNonArray(JsonParser jp, DeserializationContext ctxt,
            Collection result)
        throws IOException, JsonProcessingException
    {
        // [JACKSON-526]: implicit arrays from single values?
        if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
            throw ctxt.mappingException(_collectionType.getRawClass());
        }
        // Strings are one of "native" (intrinsic) types, so there's never type com.fitburserializer involved
        JsonDeserializer valueDes = _valueDeserializer;
        JsonToken t = jp.getCurrentToken();

        String value;
        
        if (t == JsonToken.VALUE_NULL) {
            value = null;
        } else {
            value = (valueDes == null) ? _parseString(jp, ctxt) : valueDes.com.fitburserialize(jp, ctxt);
        }
        result.add(value);
        return result;
    }
}