Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
  
Deserializer implementation where given Java type is first deserialized by a standard Jackson deserializer into a delegate type; and then this delegate type is converted using a configured com.fasterxml.jackson.databind.util.Converter into desired target type. Common delegate types to use are java.util.Map and com.fasterxml.jackson.databind.JsonNode.

Note that although types (delegate, target) may be related, they must not be same; trying to do this will result in an exception.

Parameters:
<T> Target type to convert to, from delegate type
Since:
2.1
 
 public class StdDelegatingDeserializer<T>
     extends StdDeserializer<T>
 {
     private static final long serialVersionUID = 1L;
 
     protected final Converter<Object,T> _converter;
    
    
Fully resolved delegate type, with generic information if any available.
 
     protected final JavaType _delegateType;
    
    
Underlying serializer for type T<.code>.
 
     protected final JsonDeserializer<Object_delegateDeserializer;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     @SuppressWarnings("unchecked")
     public StdDelegatingDeserializer(Converter<?,T> converter)
     {
         super(Object.class);
          = (Converter<Object,T>)converter;
          = null;
          = null;
     }
     
     @SuppressWarnings("unchecked")
     public StdDelegatingDeserializer(Converter<Object,T> converter,
             JavaType delegateTypeJsonDeserializer<?> delegateDeserializer)
     {
         super(delegateType);
          = converter;
          = delegateType;
          = (JsonDeserializer<Object>) delegateDeserializer;
     }

    
Method used for creating resolved contextual instances. Must be overridden when sub-classing.
 
     protected StdDelegatingDeserializer<T> withDelegate(Converter<Object,T> converter,
             JavaType delegateTypeJsonDeserializer<?> delegateDeserializer)
     {
         if (getClass() != StdDelegatingDeserializer.class) {
             throw new IllegalStateException("Sub-class "+getClass().getName()+" must override 'withDelegate'");
         }
         return new StdDelegatingDeserializer<T>(converterdelegateTypedelegateDeserializer);
     }
 
     /*
     /**********************************************************
     /* Contextualization
     /**********************************************************
      */
 
     @Override
     public void resolve(DeserializationContext ctxt)
         throws JsonMappingException
     {
         if ( != null &&  instanceof ResolvableDeserializer) {
             ((ResolvableDeserializer).resolve(ctxt);
         }
     }
     
    @Override
        throws JsonMappingException
    {
        // First: if already got serializer to delegate to, contextualize it:
        if ( != null) {
            if ( instanceof ContextualDeserializer) {
                JsonDeserializer<?> deser = ((ContextualDeserializer)).createContextual(ctxtproperty);
                if (deser != ) {
                    return withDelegate(deser);
                }
            }
            return this;
        }
        // Otherwise: figure out what is the fully generic delegate type, then find deserializer
        JavaType delegateType = .getInputType(ctxt.getTypeFactory());
        return withDelegate(delegateType,
                ctxt.findContextualValueDeserializer(delegateTypeproperty));
    }
    /*
    /**********************************************************
    /* Accessors
    /**********************************************************
     */
    @Override
    public JsonDeserializer<?> getDelegatee() {
        return ;
    }
    /*
    /**********************************************************
    /* Serialization
    /**********************************************************
     */
    
    @Override
    public T deserialize(JsonParser jpDeserializationContext ctxt)
            throws IOExceptionJsonProcessingException
    {
        Object delegateValue = .deserialize(jpctxt);
        if (delegateValue == null) {
            return null;
        }
        return convertValue(delegateValue);
    }
    @Override
            TypeDeserializer typeDeserializer)
        throws IOExceptionJsonProcessingException
    {
        /* 03-Oct-2012, tatu: This is actually unlikely to work ok... but for now,
         *    let's give it a chance?
         */
        Object delegateValue = .deserializeWithType(jpctxt,
                typeDeserializer);
        if (delegateValue == null) {
            return null;
        }
        return convertValue(delegateValue);
    }
    /*
    /**********************************************************
    /* Overridable methods
    /**********************************************************
     */

    
Method called to convert from "delegate value" (which was deserialized from JSON using standard Jackson deserializer for delegate type) into desired target type.

The default implementation uses configured com.fasterxml.jackson.databind.util.Converter to do conversion.

Parameters:
delegateValue
Returns:
Result of conversion
    protected T convertValue(Object delegateValue) {
        return .convert(delegateValue);
    }
New to GrepCode? Check out our FAQ X