Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.deser.std;
  
  
Default com.fasterxml.jackson.databind.deser.ValueInstantiator implementation, which supports Creator methods that can be indicated by standard Jackson annotations.
 
 public class StdValueInstantiator
     extends ValueInstantiator
     implements java.io.Serializable
 {
     private static final long serialVersionUID = 1L;

    
Type of values that are instantiated; used for error reporting purposes.
 
     protected final String _valueTypeDesc;

    
Are we allowed to convert empty Strings to null objects?
 
     protected final boolean _cfgEmptyStringsAsObjects;
     
     // // // Default (no-args) construction
 
    
Default (no-argument) constructor to use for instantiation (with createUsingDefault(com.fasterxml.jackson.databind.DeserializationContext))
 
     protected AnnotatedWithParams _defaultCreator;
 
     // // // With-args (property-based) construction
 
     protected CreatorProperty[] _constructorArguments;
 
     // // // Delegate construction
     
     protected JavaType _delegateType;
     protected CreatorProperty[] _delegateArguments;
     
     // // // Scalar construction
 
     protected AnnotatedWithParams _fromIntCreator;
     
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     public StdValueInstantiator(DeserializationConfig configClass<?> valueType)
     {
          = (config == null) ? false
                 : config.isEnabled(.);
          = (valueType == null) ? "UNKNOWN TYPE" : valueType.getName();
     }
     
     public StdValueInstantiator(DeserializationConfig configJavaType valueType)
     {
          = (config == null) ? false
                 : config.isEnabled(.);
          = (valueType == null) ? "UNKNOWN TYPE" : valueType.toString();
     }
    
    
Copy-constructor that sub-classes can use when creating new instances by fluent-style construction
 
     protected StdValueInstantiator(StdValueInstantiator src)
     {
          = src._cfgEmptyStringsAsObjects;
          = src._valueTypeDesc;
 
          = src._defaultCreator;
 
          = src._constructorArguments;
          = src._withArgsCreator;
 
         = src._delegateType;
         = src._delegateCreator;
         = src._delegateArguments;
        
         = src._fromStringCreator;
         = src._fromIntCreator;
         = src._fromLongCreator;
         = src._fromDoubleCreator;
         = src._fromBooleanCreator;
    }

    
Method for setting properties related to instantiating values from JSON Object. We will choose basically only one approach (out of possible three), and clear other properties
    public void configureFromObjectSettings(AnnotatedWithParams defaultCreator,
            AnnotatedWithParams delegateCreatorJavaType delegateTypeCreatorProperty[] delegateArgs,
            AnnotatedWithParams withArgsCreatorCreatorProperty[] constructorArgs)
    {
         = defaultCreator;
         = delegateCreator;
         = delegateType;
         = delegateArgs;
         = withArgsCreator;
         = constructorArgs;
    }
    public void configureFromStringCreator(AnnotatedWithParams creator) {
         = creator;
    }
    public void configureFromIntCreator(AnnotatedWithParams creator) {
         = creator;
    }
    public void configureFromLongCreator(AnnotatedWithParams creator) {
         = creator;
    }
    public void configureFromDoubleCreator(AnnotatedWithParams creator) {
         = creator;
    }
    public void configureFromBooleanCreator(AnnotatedWithParams creator) {
         = creator;
    }
    
    /*
    /**********************************************************
    /* Public API implementation; metadata
    /**********************************************************
     */
    @Override
    public String getValueTypeDesc() {
        return ;
    }
    
    @Override
    public boolean canCreateFromString() {
        return ( != null);
    }
    @Override
    public boolean canCreateFromInt() {
        return ( != null);
    }
    @Override
    public boolean canCreateFromLong() {
        return ( != null);
    }
    @Override
    public boolean canCreateFromDouble() {
        return ( != null);
    }
    @Override
    public boolean canCreateFromBoolean() {
        return ( != null);
    }
    
    @Override
    public boolean canCreateUsingDefault() {
        return ( != null);
    }
    @Override
    public boolean canCreateUsingDelegate() {
        return  != null;
    }
    
    @Override
    public boolean canCreateFromObjectWith() {
        return ( != null);
    }
    @Override
        return ;
    }
    @Override
        return ;
    }
    
    /*
    /**********************************************************
    /* Public API implementation; instantiation from JSON Object
    /**********************************************************
     */
    
    @Override
        throws IOExceptionJsonProcessingException
    {
        if ( == null) { // sanity-check; caller should check
            throw new IllegalStateException("No default constructor for "+getValueTypeDesc());
        }
        try {
            return .call();
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        } catch (Exception e) {
            throw wrapException(e);
        }
    }
    
    @Override
        throws IOExceptionJsonProcessingException
    {
        if ( == null) { // sanity-check; caller should check
            throw new IllegalStateException("No with-args constructor for "+getValueTypeDesc());
        }
        try {
            return .call(args);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        } catch (Exception e) {
            throw wrapException(e);
        }
    }
    @Override
    public Object createUsingDelegate(DeserializationContext ctxtObject delegate)
        throws IOExceptionJsonProcessingException
    {
        if ( == null) { // sanity-check; caller should check
            throw new IllegalStateException("No delegate constructor for "+getValueTypeDesc());
        }
        try {
            // First simple case: just delegate, no injectables
            if ( == null) {
                return .call1(delegate);
            }
            // And then the case with at least one injectable...
            final int len = .;
            Object[] args = new Object[len];
            for (int i = 0; i < len; ++i) {
                CreatorProperty prop = [i];
                if (prop == null) { // delegate
                    args[i] = delegate;
                } else { // nope, injectable:
                    args[i] = ctxt.findInjectableValue(prop.getInjectableValueId(), propnull);
                }
            }
            // and then try calling with full set of arguments
            return .call(args);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        } catch (Exception e) {
            throw wrapException(e);
        }
    }
    
    /*
    /**********************************************************
    /* Public API implementation; instantiation from JSON scalars
    /**********************************************************
     */
    
    @Override
    public Object createFromString(DeserializationContext ctxtString value)
            throws IOExceptionJsonProcessingException
    {
        if ( != null) {
            try {
                return .call1(value);
            } catch (Exception e) {
                throw wrapException(e);
            } catch (ExceptionInInitializerError e) {
                throw wrapException(e);
            }
        }
        return _createFromStringFallbacks(ctxtvalue);
    }
    
    @Override
    public Object createFromInt(DeserializationContext ctxtint value)
            throws IOExceptionJsonProcessingException
    {
        try {
            // First: "native" int methods work best:
            if ( != null) {
                return .call1(Integer.valueOf(value));
            }
            // but if not, can do widening conversion
            if ( != null) {
                return .call1(Long.valueOf(value));
            }
        } catch (Exception e) {
            throw wrapException(e);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        }
        throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()
                +" from Integral number; no single-int-arg constructor/factory method");
    }
    @Override
    public Object createFromLong(DeserializationContext ctxtlong value)
            throws IOExceptionJsonProcessingException
    {
        try {
            if ( != null) {
                return .call1(Long.valueOf(value));
            }
        } catch (Exception e) {
            throw wrapException(e);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        }
        throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()
                +" from Long integral number; no single-long-arg constructor/factory method");
    }
    @Override
    public Object createFromDouble(DeserializationContext ctxtdouble value)
            throws IOExceptionJsonProcessingException
    {
        try {
            if ( != null) {
                return .call1(Double.valueOf(value));
            }
        } catch (Exception e) {
            throw wrapException(e);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        }
        throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()
                +" from Floating-point number; no one-double/Double-arg constructor/factory method");
    }
    @Override
    public Object createFromBoolean(DeserializationContext ctxtboolean value)
            throws IOExceptionJsonProcessingException
    {
        try {
            if ( != null) {
                return .call1(Boolean.valueOf(value));
            }
        } catch (Exception e) {
            throw wrapException(e);
        } catch (ExceptionInInitializerError e) {
            throw wrapException(e);
        }
        throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()
                +" from Boolean value; no single-boolean/Boolean-arg constructor/factory method");
    }
    
    /*
    /**********************************************************
    /* Extended API: configuration mutators, accessors
    /**********************************************************
     */
    @Override
        return ;
    }
    @Override
        return ;
    }
    @Override
        return ;
    }
    
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
            throws IOExceptionJsonProcessingException
    {
        /* 28-Sep-2011, tatu: Ok this is not clean at all; but since there are legacy
         *   systems that expect conversions in some cases, let's just add a minimal
         *   patch (note: same could conceivably be used for numbers too).
         */
        if ( != null) {
            String str = value.trim();
            if ("true".equals(str)) {
                return createFromBoolean(ctxttrue);
            }
            if ("false".equals(str)) {
                return createFromBoolean(ctxtfalse);
            }
        }
        
        // and finally, empty Strings might be accepted as null Object...
        if ( && value.length() == 0) {
            return null;
        }
        throw new JsonMappingException("Can not instantiate value of type "+getValueTypeDesc()
                +" from String value; no single-String constructor/factory method");
    }
    
    {
        while (t.getCause() != null) {
            t = t.getCause();
        }
        if (t instanceof JsonMappingException) {
            return (JsonMappingExceptiont;
        }
        return new JsonMappingException("Instantiation of "+getValueTypeDesc()+" value failed: "+t.getMessage(), t);
    }
New to GrepCode? Check out our FAQ X