Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
Specifically optimized version for java.util.Collections that contain String values; reason is that this is a very common type and we can make use of the fact that Strings are final.
 
 public final class StringCollectionDeserializer
     implements ContextualDeserializer
 {
     private static final long serialVersionUID = 1L;
 
     // // Configuration
 
     protected final JavaType _collectionType;
    
    
Value deserializer to use, if NOT the standard one (if it is, will be null).
 
     protected final JsonDeserializer<String_valueDeserializer;
 
     // // Instance construction settings:
     
    
Instantiator used in case custom handling is needed for creation.
 
     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
     /**********************************************************
      */
     
     public StringCollectionDeserializer(JavaType collectionType,
             JsonDeserializer<?> valueDeserValueInstantiator valueInstantiator)
     {
         this(collectionTypevalueInstantiatornullvalueDeser);
     }
 
     @SuppressWarnings("unchecked")
     protected StringCollectionDeserializer(JavaType collectionType,
             ValueInstantiator valueInstantiatorJsonDeserializer<?> delegateDeser,
             JsonDeserializer<?> valueDeser)
     {
         super(collectionType.getRawClass());
          = collectionType;
          = (JsonDeserializer<String>) valueDeser;
          = valueInstantiator;
          = (JsonDeserializer<Object>) delegateDeser;
     }
 
     protected StringCollectionDeserializer withResolved(JsonDeserializer<?> delegateDeser,
             JsonDeserializer<?> valueDeser)
     {
         if (( == valueDeser) && ( == delegateDeser)) {
             return this;
         }
         return new StringCollectionDeserializer(,
                 delegateDeservalueDeser);
     }
     
     /*
     /**********************************************************
     /* Validation, post-processing
     /**********************************************************
      */
 
 //    @Override
     @SuppressWarnings("unchecked")
             BeanProperty propertythrows JsonMappingException
     {
         // May need to resolve types for delegate-based creators:
         JsonDeserializer<Objectdelegate = null;
         if ( != null) {
             AnnotatedWithParams delegateCreator = .getDelegateCreator();
            if (delegateCreator != null) {
                JavaType delegateType = .getDelegateType(ctxt.getConfig());
                delegate = findDeserializer(ctxtdelegateTypeproperty);
            }
        }
        JsonDeserializer<?> valueDeser = ;
        if (valueDeser == null) {
            // And we may also need to get deserializer for String
            JsonDeserializer<?> deser = ctxt.findContextualValueDeserializer(
                    .getContentType(), property);
            valueDeser = (JsonDeserializer<String>) deser;
        } else { // if directly assigned, probably not yet contextual, so:
            if (valueDeser instanceof ContextualDeserializer) {
                valueDeser = ((ContextualDeserializervalueDeser).createContextual(ctxtproperty);
            }
        } 
        if (isDefaultDeserializer(valueDeser)) {
            valueDeser = null;
        }
        return withResolved(delegatevalueDeser);
    }
    
    /*
    /**********************************************************
    /* ContainerDeserializerBase API
    /**********************************************************
     */
    @Override
    public JavaType getContentType() {
        return .getContentType();
    }
    @SuppressWarnings("unchecked")
    @Override
        JsonDeserializer<?> deser = ;
        return (JsonDeserializer<Object>) deser;
    }
    
    /*
    /**********************************************************
    /* JsonDeserializer API
    /**********************************************************
     */
    
    @SuppressWarnings("unchecked")
    @Override
        throws IOExceptionJsonProcessingException
    {
        if ( != null) {
            return (Collection<String>) .createUsingDelegate(ctxt,
                    .deserialize(jpctxt));
        }
        final Collection<Stringresult = (Collection<String>) .createUsingDefault(ctxt);
        return deserialize(jpctxtresult);
    }
    @Override
                                          Collection<Stringresult)
        throws IOExceptionJsonProcessingException
    {
        // Ok: must point to START_ARRAY
        if (!jp.isExpectedStartArrayToken()) {
            return handleNonArray(jpctxtresult);
        }
        if ( != null) {
            return deserializeUsingCustom(jpctxtresult);
        }
        JsonToken t;
        while ((t = jp.nextToken()) != .) {
            result.add((t == .) ? null : _parseString(jpctxt));
        }
        return result;
    }
    
            Collection<Stringresultfinal JsonDeserializer<Stringdeser)
        throws IOExceptionJsonProcessingException
    {
        JsonToken t;
        while ((t = jp.nextToken()) != .) {
            String value;
            if (t == .) {
                value = null;
            } else {
                value = deser.deserialize(jpctxt);
            }
            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<Stringresult)
        throws IOExceptionJsonProcessingException
    {
        // [JACKSON-526]: implicit arrays from single values?
            throw ctxt.mappingException(.getRawClass());
        }
        // Strings are one of "native" (intrinsic) types, so there's never type deserializer involved
        JsonDeserializer<StringvalueDes = ;
        JsonToken t = jp.getCurrentToken();
        String value;
        
        if (t == .) {
            value = null;
        } else {
            value = (valueDes == null) ? _parseString(jpctxt) : valueDes.deserialize(jpctxt);
        }
        result.add(value);
        return result;
    }
New to GrepCode? Check out our FAQ X