Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  import java.util.*;
  
Basic serializer that can take JSON "Array" structure and construct a java.util.Collection instance, with typed contents.

Note: for untyped content (one indicated by passing Object.class as the type), UntypedObjectDeserializer is used instead. It can also construct java.util.Lists, but not with specific POJO types, only other containers and primitives/wrappers.

 
 public class CollectionDeserializer
     implements ContextualDeserializer
 {
     // // Configuration
 
     protected final JavaType _collectionType;
    
    
Value deserializer.
 
     protected final JsonDeserializer<Object_valueDeserializer;

    
If element instances have polymorphic type information, this is the type deserializer that can handle it
 
     protected final TypeDeserializer _valueTypeDeserializer;
 
     // // Instance construction settings:
     
     protected final ValueInstantiator _valueInstantiator;
    
    
Deserializer that is used iff delegate-based creator is to be used for deserializing from JSON Object.
 
     protected final JsonDeserializer<Object_delegateDeserializer;
 
     // NOTE: no PropertyBasedCreator, as JSON Arrays have no properties
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */

    
Constructor for context-free instances, where we do not yet know which property is using this deserializer.
 
     public CollectionDeserializer(JavaType collectionType,
             JsonDeserializer<ObjectvalueDeser,
             TypeDeserializer valueTypeDeserValueInstantiator valueInstantiator)
     {
         this(collectionTypevalueDeservalueTypeDeservalueInstantiatornull);
     }

    
Constructor used when creating contextualized instances.
 
     protected CollectionDeserializer(JavaType collectionType,
             JsonDeserializer<ObjectvalueDeserTypeDeserializer valueTypeDeser,
             ValueInstantiator valueInstantiator,
             JsonDeserializer<ObjectdelegateDeser)
     {
         super(collectionType.getRawClass());
          = collectionType;
          = valueDeser;
          = valueTypeDeser;
          = valueInstantiator;
          = delegateDeser;
     }

    
Copy-constructor that can be used by sub-classes to allow copy-on-write styling copying of settings of an existing instance.
 
     {
         super(src._valueClass);
          = src._collectionType;
          = src._valueDeserializer;
          = src._valueTypeDeserializer;
          = src._valueInstantiator;
          = src._delegateDeserializer;
    }

    
Fluent-factory method call to construct contextual instance.
    @SuppressWarnings("unchecked")
            JsonDeserializer<?> vdTypeDeserializer vtd)
    {
        if ((dd == ) && (vd == ) && (vtd == )) {
            return this;
        }
        return new CollectionDeserializer(,
                (JsonDeserializer<Object>) vdvtd,
                , (JsonDeserializer<Object>) dd);
                
    }
    
    /*
    /**********************************************************
    /* Validation, post-processing (ResolvableDeserializer)
    /**********************************************************
     */

    
Method called to finalize setup of this deserializer, when it is known for which property deserializer is needed for.
//    @Override
            BeanProperty propertythrows JsonMappingException
    {
        // May need to resolve types for delegate-based creators:
        JsonDeserializer<ObjectdelegateDeser = null;
        if (( != null) && .canCreateUsingDelegate()) {
            JavaType delegateType = .getDelegateType(ctxt.getConfig());
            if (delegateType == null) {
                throw new IllegalArgumentException("Invalid delegate-creator definition for "+
                        +": value instantiator ("+.getClass().getName()
                        +") returned true for 'canCreateUsingDelegate()', but null for 'getDelegateType()'");
            }
            delegateDeser = findDeserializer(ctxtdelegateTypeproperty);
        }
        // also, often value deserializer is resolved here:
        JsonDeserializer<?> valueDeser = ;
        if (valueDeser == null) {
            valueDeser = ctxt.findContextualValueDeserializer(
                    .getContentType(), property);
        } else { // if directly assigned, probably not yet contextual, so:
            if (valueDeser instanceof ContextualDeserializer) {
                valueDeser = ((ContextualDeserializervalueDeser).createContextual(ctxtproperty);
            }
        }
        // and finally, type deserializer needs context as well
        TypeDeserializer valueTypeDeser = ;
        if (valueTypeDeser != null) {
            valueTypeDeser = valueTypeDeser.forProperty(property);
        }
        return withResolved(delegateDeservalueDeservalueTypeDeser);
    }
    
    /*
    /**********************************************************
    /* ContainerDeserializerBase API
    /**********************************************************
     */
    @Override
    public JavaType getContentType() {
        return .getContentType();
    }
    @Override
        return ;
    }
    
    /*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
    
    @SuppressWarnings("unchecked")
    @Override
        throws IOExceptionJsonProcessingException
    {
        if ( != null) {
            return (Collection<Object>) .createUsingDelegate(ctxt,
                    .deserialize(jpctxt));
        }
        /* [JACKSON-620]: empty String may be ok; bit tricky to check, however, since
         *  there is also possibility of "auto-wrapping" of single-element arrays.
         *  Hence we only accept empty String here.
         */
        if (jp.getCurrentToken() == .) {
            String str = jp.getText();
            if (str.length() == 0) {
                return (Collection<Object>) .createFromString(ctxtstr);
            }
        }
        return deserialize(jpctxt, (Collection<Object>) .createUsingDefault(ctxt));
    }
    @Override
            Collection<Objectresult)
        throws IOExceptionJsonProcessingException
    {
        // Ok: must point to START_ARRAY (or equivalent)
        if (!jp.isExpectedStartArrayToken()) {
            return handleNonArray(jpctxtresult);
        }
        JsonDeserializer<ObjectvalueDes = ;
        JsonToken t;
        final TypeDeserializer typeDeser = ;
        while ((t = jp.nextToken()) != .) {
            Object value;
            
            if (t == .) {
                value = null;
            } else if (typeDeser == null) {
                value = valueDes.deserialize(jpctxt);
            } else {
                value = valueDes.deserializeWithType(jpctxttypeDeser);
            }
            result.add(value);
        }
        return result;
    }
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        // In future could check current token... for now this should be enough:
        return typeDeserializer.deserializeTypedFromArray(jpctxt);
    }

    
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, depending on configuration.
            Collection<Objectresult)
        throws IOExceptionJsonProcessingException
    {
        // [JACKSON-526]: implicit arrays from single values?
            throw ctxt.mappingException(.getRawClass());
        }
        JsonDeserializer<ObjectvalueDes = ;
        final TypeDeserializer typeDeser = ;
        JsonToken t = jp.getCurrentToken();
        Object value;
        
        if (t == .) {
            value = null;
        } else if (typeDeser == null) {
            value = valueDes.deserialize(jpctxt);
        } else {
            value = valueDes.deserializeWithType(jpctxttypeDeser);
        }
        result.add(value);
        return result;
    }
New to GrepCode? Check out our FAQ X