com.fasterxml.jackson.databind

Class DeserializationContext

    • Field Detail

      • _factory

        protected final DeserializerFactory _factory
        Read-only factory instance; exposed to let owners (ObjectMapper, ObjectReader) access it.
      • _config

        protected final DeserializationConfig _config
        Generic deserialization processing configuration
      • _readCapabilities

        protected final com.fasterxml.jackson.core.util.JacksonFeatureSet<com.fasterxml.jackson.core.StreamReadCapability> _readCapabilities
        Capabilities of the input format.
        Since:
        2.12
      • _view

        protected final Class<?> _view
        Currently active view, if any.
      • _parser

        protected transient com.fasterxml.jackson.core.JsonParser _parser
        Currently active parser used for deserialization. May be different from the outermost parser when content is buffered.
      • _injectableValues

        protected final InjectableValues _injectableValues
        Object used for resolving references to injectable values.
      • _arrayBuilders

        protected transient ArrayBuilders _arrayBuilders
      • _objectBuffer

        protected transient ObjectBuffer _objectBuffer
      • _dateFormat

        protected transient DateFormat _dateFormat
      • _attributes

        protected transient ContextAttributes _attributes
        Lazily-constructed holder for per-call attributes.
        Since:
        2.3
    • Method Detail

      • getActiveView

        public final Class<?> getActiveView()
        Description copied from class: DatabindContext
        Accessor for locating currently active view, if any; returns null if no view has been set.
        Specified by:
        getActiveView in class DatabindContext
      • canOverrideAccessModifiers

        public final boolean canOverrideAccessModifiers()
        Description copied from class: DatabindContext
        Convenience method for accessing serialization view in use (if any); equivalent to:
           getConfig().canOverrideAccessModifiers();
        
        Specified by:
        canOverrideAccessModifiers in class DatabindContext
      • isEnabled

        public final boolean isEnabled(MapperFeature feature)
        Description copied from class: DatabindContext
        Convenience method for checking whether specified Mapper feature is enabled or not. Shortcut for:
          getConfig().isEnabled(feature);
        
        Specified by:
        isEnabled in class DatabindContext
      • getLocale

        public Locale getLocale()
        Method for accessing default Locale to use: convenience method for
           getConfig().getLocale();
        
        Specified by:
        getLocale in class DatabindContext
      • getTimeZone

        public TimeZone getTimeZone()
        Method for accessing default TimeZone to use: convenience method for
           getConfig().getTimeZone();
        
        Specified by:
        getTimeZone in class DatabindContext
      • getAttribute

        public Object getAttribute(Object key)
        Description copied from class: DatabindContext
        Method for accessing attributes available in this context. Per-call attributes have highest precedence; attributes set via ObjectReader or ObjectWriter have lower precedence.
        Specified by:
        getAttribute in class DatabindContext
        Parameters:
        key - Key of the attribute to get
        Returns:
        Value of the attribute, if any; null otherwise
      • setAttribute

        public DeserializationContext setAttribute(Object key,
                                                   Object value)
        Description copied from class: DatabindContext
        Method for setting per-call value of given attribute. This will override any previously defined value for the attribute within this context.
        Specified by:
        setAttribute in class DatabindContext
        Parameters:
        key - Key of the attribute to set
        value - Value to set attribute to
        Returns:
        This context object, to allow chaining
      • getContextualType

        public JavaType getContextualType()
        Accessor to JavaType of currently contextualized ContextualDeserializer, if any. This is sometimes useful for generic JsonDeserializers that do not get passed (or do not retain) type information when being constructed: happens for example for deserializers constructed from annotations.
        Returns:
        Type of ContextualDeserializer being contextualized, if process is on-going; null if not.
        Since:
        2.5
      • isEnabled

        public final boolean isEnabled(DeserializationFeature feat)
        Convenience method for checking whether specified on/off feature is enabled
      • isEnabled

        public final boolean isEnabled(com.fasterxml.jackson.core.StreamReadCapability cap)
        Accessor for checking whether input format has specified capability or not.
        Returns:
        True if input format has specified capability; false if not
        Since:
        2.12
      • getDeserializationFeatures

        public final int getDeserializationFeatures()
        Bulk access method for getting the bit mask of all DeserializationFeatures that are enabled.
        Since:
        2.6
      • hasDeserializationFeatures

        public final boolean hasDeserializationFeatures(int featureMask)
        Bulk access method for checking that all features specified by mask are enabled.
        Since:
        2.3
      • hasSomeOfFeatures

        public final boolean hasSomeOfFeatures(int featureMask)
        Bulk access method for checking that at least one of features specified by mask is enabled.
        Since:
        2.6
      • getParser

        public final com.fasterxml.jackson.core.JsonParser getParser()
        Method for accessing the currently active parser. May be different from the outermost parser when content is buffered.

        Use of this method is discouraged: if code has direct access to the active parser, that should be used instead.

      • getBase64Variant

        public final com.fasterxml.jackson.core.Base64Variant getBase64Variant()
        Convenience method for accessing the default Base64 encoding used for decoding base64 encoded binary content. Same as calling:
          getConfig().getBase64Variant();
        
      • getNodeFactory

        public final JsonNodeFactory getNodeFactory()
        Convenience method, functionally equivalent to:
          getConfig().getNodeFactory();
         
      • findCoercionAction

        public CoercionAction findCoercionAction(LogicalType targetType,
                                                 Class<?> targetClass,
                                                 CoercionInputShape inputShape)
        General-purpose accessor for finding what to do when specified coercion from shape that is now always allowed to be coerced from is requested.
        Parameters:
        targetType - Logical target type of coercion
        targetClass - Physical target type of coercion
        inputShape - Input shape to coerce from
        Returns:
        CoercionAction configured for specific coercion
        Since:
        2.12
      • findCoercionFromBlankString

        public CoercionAction findCoercionFromBlankString(LogicalType targetType,
                                                          Class<?> targetClass,
                                                          CoercionAction actionIfBlankNotAllowed)
        More specialized accessor called in case of input being a blank String (one consisting of only white space characters with length of at least one). Will basically first determine if "blank as empty" is allowed: if not, returns actionIfBlankNotAllowed, otherwise returns action for CoercionInputShape.EmptyString.
        Parameters:
        targetType - Logical target type of coercion
        targetClass - Physical target type of coercion
        actionIfBlankNotAllowed - Return value to use in case "blanks as empty" is not allowed
        Returns:
        CoercionAction configured for specified coercion from blank string
        Since:
        2.12
      • bufferForInputBuffering

        public TokenBuffer bufferForInputBuffering(com.fasterxml.jackson.core.JsonParser p)
        Factory method used for creating TokenBuffer to temporarily contain copy of content read from specified parser; usually for purpose of reading contents later on (possibly augmeneted with injected additional content)
        Since:
        2.13
      • bufferForInputBuffering

        public final TokenBuffer bufferForInputBuffering()
        Convenience method that is equivalent to:
           ctxt.bufferForInputBuffering(ctxt.getParser());
        
      • bufferAsCopyOfValue

        public TokenBuffer bufferAsCopyOfValue(com.fasterxml.jackson.core.JsonParser p)
                                        throws IOException
        Convenience method, equivalent to:
         TokenBuffer buffer = ctxt.bufferForInputBuffering(parser);
         buffer.copyCurrentStructure(parser);
         return buffer;
        

        NOTE: the whole "current value" that parser points to is read and buffered, including Object and Array values (if parser pointing to start marker).

        Throws:
        IOException
        Since:
        2.9
      • hasValueDeserializerFor

        public boolean hasValueDeserializerFor(JavaType type,
                                               AtomicReference<Throwable> cause)
        Method for checking whether we could find a deserializer for given type.
        Parameters:
        type - Type to check
        cause - (optional) Reference set to root cause if no deserializer could be found due to exception (to find the reason for failure)
        Since:
        2.3
      • constructType

        public final JavaType constructType(Class<?> cls)
        Convenience method, functionally equivalent to:
          getConfig().constructType(cls);
         
      • findClass

        public Class<?> findClass(String className)
                           throws ClassNotFoundException
        Helper method that is to be used when resolving basic class name into Class instance, the reason being that it may be necessary to work around various ClassLoader limitations, as well as to handle primitive type signatures.
        Throws:
        ClassNotFoundException
        Since:
        2.6
      • leaseObjectBuffer

        public final ObjectBuffer leaseObjectBuffer()
        Method that can be used to get access to a reusable ObjectBuffer, useful for efficiently constructing Object arrays and Lists. Note that leased buffers should be returned once deserializer is done, to allow for reuse during same round of deserialization.
      • returnObjectBuffer

        public final void returnObjectBuffer(ObjectBuffer buf)
        Method to call to return object buffer previously leased with leaseObjectBuffer().
        Parameters:
        buf - Returned object buffer
      • getArrayBuilders

        public final ArrayBuilders getArrayBuilders()
        Method for accessing object useful for building arrays of primitive types (such as int[]).
      • handleSecondaryContextualization

        public JsonDeserializer<?> handleSecondaryContextualization(JsonDeserializer<?> deser,
                                                                    BeanProperty prop,
                                                                    JavaType type)
                                                             throws JsonMappingException
        Method called for secondary property deserializers (ones NOT directly created to deal with an annotatable POJO property, but instead created as a component -- such as value deserializers for structured types, or deserializers for root values) to handle details of resolving ContextualDeserializer with given property context. Given that these deserializers are not directly related to given property (or, in case of root value property, to any property), annotations accessible may or may not be relevant.
        Parameters:
        prop - Property for which deserializer is used, if any; null when deserializing root values
        Throws:
        JsonMappingException
        Since:
        2.5
      • parseDate

        public Date parseDate(String dateStr)
                       throws IllegalArgumentException
        Convenience method for parsing a Date from given String, using currently configured date format (accessed using MapperConfig.getDateFormat()).

        Implementation will handle thread-safety issues related to date formats such that first time this method is called, date format is cloned, and cloned instance will be retained for use during this deserialization round.

        Throws:
        IllegalArgumentException
      • constructCalendar

        public Calendar constructCalendar(Date d)
        Convenience method for constructing Calendar instance set to specified time, to be modified and used by caller.
      • extractScalarFromObject

        public String extractScalarFromObject(com.fasterxml.jackson.core.JsonParser p,
                                              JsonDeserializer<?> deser,
                                              Class<?> scalarType)
                                       throws IOException
        Method to call in case incoming shape is Object Value (and parser thereby points to JsonToken.START_OBJECT token), but a Scalar value (potentially coercible from String value) is expected. This would typically be used to deserializer a Number, Boolean value or some other "simple" unstructured value type.
        Parameters:
        p - Actual parser to read content from
        deser - Deserializer that needs extracted String value
        scalarType - Immediate type of scalar to extract; usually type deserializer handles but not always (for example, deserializer for int[] would pass scalar type of int)
        Returns:
        String value found; not null (exception should be thrown if no suitable value found)
        Throws:
        IOException - If there are problems either reading content (underlying parser problem) or finding expected scalar value
      • readValue

        public <T> T readValue(com.fasterxml.jackson.core.JsonParser p,
                               Class<T> type)
                        throws IOException
        Convenience method that may be used by composite or container deserializers, for reading one-off values contained (for sequences, it is more efficient to actually fetch deserializer once for the whole collection).

        NOTE: when deserializing values of properties contained in composite types, rather use readPropertyValue(JsonParser, BeanProperty, Class); this method does not allow use of contextual annotations.

        Throws:
        IOException
        Since:
        2.4
      • readPropertyValue

        public <T> T readPropertyValue(com.fasterxml.jackson.core.JsonParser p,
                                       BeanProperty prop,
                                       Class<T> type)
                                throws IOException
        Convenience method that may be used by composite or container deserializers, for reading one-off values for the composite type, taking into account annotations that the property (passed to this method -- usually property that has custom serializer that called this method) has.
        Parameters:
        p - Parser that points to the first token of the value to read
        prop - Logical property of a POJO being type
        Returns:
        Value of type type that was read
        Throws:
        IOException
        Since:
        2.4
      • readTree

        public JsonNode readTree(com.fasterxml.jackson.core.JsonParser p)
                          throws IOException
        Convenience method for reading the value that passed JsonParser points to as a JsonNode.
        Parameters:
        p - Parser that points to the first token of the value to read
        Returns:
        Value read as JsonNode
        Throws:
        IOException
        Since:
        2.10
      • readTreeAsValue

        public <T> T readTreeAsValue(JsonNode n,
                                     Class<T> targetType)
                              throws IOException
        Helper method similar to ObjectReader.treeToValue(TreeNode, Class) which will read contents of given tree (JsonNode) and bind them into specified target type. This is often used in two-phase deserialization in which content is first read as a tree, then manipulated (adding and/or removing properties of Object values, for example), and finally converted into actual target type using default deserialization logic for the type.

        NOTE: deserializer implementations should be careful not to try to recursively deserialize into target type deserializer has registered itself to handle.

        Parameters:
        n - Tree value to convert, if not null: if null, will simply return null
        targetType - Type to deserialize contents of n into (if n not null)
        Returns:
        Either null (if n was null or a value of type type that was read from non-null n argument
        Throws:
        IOException
        Since:
        2.13
      • readTreeAsValue

        public <T> T readTreeAsValue(JsonNode n,
                                     JavaType targetType)
                              throws IOException
        Same as readTreeAsValue(JsonNode, Class) but will fully resolved JavaType as targetType

        NOTE: deserializer implementations should be careful not to try to recursively deserialize into target type deserializer has registered itself to handle.

        Parameters:
        n - Tree value to convert
        targetType - Type to deserialize contents of n into
        Returns:
        Value of type type that was read
        Throws:
        IOException
        Since:
        2.13
      • handleUnknownProperty

        public boolean handleUnknownProperty(com.fasterxml.jackson.core.JsonParser p,
                                             JsonDeserializer<?> deser,
                                             Object instanceOrClass,
                                             String propName)
                                      throws IOException
        Method that deserializers should call if they encounter an unrecognized property (and once that is not explicitly designed as ignorable), to inform possibly configured DeserializationProblemHandlers and let it handle the problem.
        Returns:
        True if there was a configured problem handler that was able to handle the problem
        Throws:
        IOException
      • handleMissingInstantiator

        public Object handleMissingInstantiator(Class<?> instClass,
                                                ValueInstantiator valueInst,
                                                com.fasterxml.jackson.core.JsonParser p,
                                                String msg,
                                                Object... msgArgs)
                                         throws IOException
        Method that deserializers should call if they fail to instantiate value due to lack of viable instantiator (usually creator, that is, constructor or static factory method). Method should be called at point where value has not been decoded, so that handler has a chance to handle decoding using alternate mechanism, and handle underlying content (possibly by just skipping it) to keep input state valid
        Parameters:
        instClass - Type that was to be instantiated
        valueInst - (optional) Value instantiator to be used, if any; null if type does not use one for instantiation (custom deserialiers don't; standard POJO deserializer does)
        p - Parser that points to the JSON value to decode
        Returns:
        Object that should be constructed, if any; has to be of type instClass
        Throws:
        IOException
        Since:
        2.9 (2.8 had alternate that did not take ValueInstantiator)
      • handleUnexpectedToken

        public Object handleUnexpectedToken(Class<?> instClass,
                                            com.fasterxml.jackson.core.JsonParser p)
                                     throws IOException
        Method that deserializers should call if the first token of the value to deserialize is of unexpected type (that is, type of token that deserializer cannot handle). This could occur, for example, if a Number deserializer encounter JsonToken.START_ARRAY instead of JsonToken.VALUE_NUMBER_INT or JsonToken.VALUE_NUMBER_FLOAT.
        Parameters:
        instClass - Type that was to be instantiated
        p - Parser that points to the JSON value to decode
        Returns:
        Object that should be constructed, if any; has to be of type instClass
        Throws:
        IOException
        Since:
        2.8
      • handleUnexpectedToken

        public Object handleUnexpectedToken(Class<?> instClass,
                                            com.fasterxml.jackson.core.JsonToken t,
                                            com.fasterxml.jackson.core.JsonParser p,
                                            String msg,
                                            Object... msgArgs)
                                     throws IOException
        Method that deserializers should call if the first token of the value to deserialize is of unexpected type (that is, type of token that deserializer cannot handle). This could occur, for example, if a Number deserializer encounter JsonToken.START_ARRAY instead of JsonToken.VALUE_NUMBER_INT or JsonToken.VALUE_NUMBER_FLOAT.
        Parameters:
        instClass - Type that was to be instantiated
        t - Token encountered that does match expected
        p - Parser that points to the JSON value to decode
        Returns:
        Object that should be constructed, if any; has to be of type instClass
        Throws:
        IOException
        Since:
        2.8
      • handleUnexpectedToken

        public Object handleUnexpectedToken(JavaType targetType,
                                            com.fasterxml.jackson.core.JsonParser p)
                                     throws IOException
        Method that deserializers should call if the first token of the value to deserialize is of unexpected type (that is, type of token that deserializer cannot handle). This could occur, for example, if a Number deserializer encounter JsonToken.START_ARRAY instead of JsonToken.VALUE_NUMBER_INT or JsonToken.VALUE_NUMBER_FLOAT.
        Parameters:
        targetType - Type that was to be instantiated
        p - Parser that points to the JSON value to decode
        Returns:
        Object that should be constructed, if any; has to be of type instClass
        Throws:
        IOException
        Since:
        2.10
      • handleUnexpectedToken

        public Object handleUnexpectedToken(JavaType targetType,
                                            com.fasterxml.jackson.core.JsonToken t,
                                            com.fasterxml.jackson.core.JsonParser p,
                                            String msg,
                                            Object... msgArgs)
                                     throws IOException
        Method that deserializers should call if the first token of the value to deserialize is of unexpected type (that is, type of token that deserializer cannot handle). This could occur, for example, if a Number deserializer encounter JsonToken.START_ARRAY instead of JsonToken.VALUE_NUMBER_INT or JsonToken.VALUE_NUMBER_FLOAT.
        Parameters:
        targetType - Type that was to be instantiated
        t - Token encountered that does not match expected
        p - Parser that points to the JSON value to decode
        Returns:
        Object that should be constructed, if any; has to be of type instClass
        Throws:
        IOException
        Since:
        2.10
      • _isCompatible

        protected boolean _isCompatible(Class<?> target,
                                        Object value)
        Since:
        2.9.2
      • reportWrongTokenException

        public void reportWrongTokenException(JsonDeserializer<?> deser,
                                              com.fasterxml.jackson.core.JsonToken expToken,
                                              String msg,
                                              Object... msgArgs)
                                       throws JsonMappingException
        Method for deserializers to call when the token encountered was of type different than what should be seen at that position, usually within a sequence of expected tokens. Note that this method will throw a JsonMappingException and no recovery is attempted (via DeserializationProblemHandler, as problem is considered to be difficult to recover from, in general.
        Throws:
        JsonMappingException
        Since:
        2.9
      • reportWrongTokenException

        public void reportWrongTokenException(JavaType targetType,
                                              com.fasterxml.jackson.core.JsonToken expToken,
                                              String msg,
                                              Object... msgArgs)
                                       throws JsonMappingException
        Method for deserializers to call when the token encountered was of type different than what should be seen at that position, usually within a sequence of expected tokens. Note that this method will throw a JsonMappingException and no recovery is attempted (via DeserializationProblemHandler, as problem is considered to be difficult to recover from, in general.
        Throws:
        JsonMappingException
        Since:
        2.9
      • reportWrongTokenException

        public void reportWrongTokenException(Class<?> targetType,
                                              com.fasterxml.jackson.core.JsonToken expToken,
                                              String msg,
                                              Object... msgArgs)
                                       throws JsonMappingException
        Method for deserializers to call when the token encountered was of type different than what should be seen at that position, usually within a sequence of expected tokens. Note that this method will throw a JsonMappingException and no recovery is attempted (via DeserializationProblemHandler, as problem is considered to be difficult to recover from, in general.
        Throws:
        JsonMappingException
        Since:
        2.9
      • reportInputMismatch

        public <T> T reportInputMismatch(Class<?> targetType,
                                         String msg,
                                         Object... msgArgs)
                                  throws JsonMappingException
        Helper method used to indicate a problem with input in cases where more specific reportXxx() method was not available.
        Throws:
        JsonMappingException
        Since:
        2.9
      • reportPropertyInputMismatch

        public <T> T reportPropertyInputMismatch(Class<?> targetType,
                                                 String propertyName,
                                                 String msg,
                                                 Object... msgArgs)
                                          throws JsonMappingException
        Helper method used to indicate a problem with input in cases where more specific reportXxx() method was not available.
        Throws:
        JsonMappingException
        Since:
        2.10
      • reportPropertyInputMismatch

        public <T> T reportPropertyInputMismatch(JavaType targetType,
                                                 String propertyName,
                                                 String msg,
                                                 Object... msgArgs)
                                          throws JsonMappingException
        Helper method used to indicate a problem with input in cases where more specific reportXxx() method was not available.
        Throws:
        JsonMappingException
        Since:
        2.10
      • reportTrailingTokens

        public <T> T reportTrailingTokens(Class<?> targetType,
                                          com.fasterxml.jackson.core.JsonParser p,
                                          com.fasterxml.jackson.core.JsonToken trailingToken)
                                   throws JsonMappingException
        Throws:
        JsonMappingException
      • wrongTokenException

        public JsonMappingException wrongTokenException(com.fasterxml.jackson.core.JsonParser p,
                                                        Class<?> targetType,
                                                        com.fasterxml.jackson.core.JsonToken expToken,
                                                        String extra)
      • wrongTokenException

        @Deprecated
        public JsonMappingException wrongTokenException(com.fasterxml.jackson.core.JsonParser p,
                                                                    com.fasterxml.jackson.core.JsonToken expToken,
                                                                    String msg)
        Deprecated. 
      • invalidTypeIdException

        public JsonMappingException invalidTypeIdException(JavaType baseType,
                                                           String typeId,
                                                           String extraDesc)
        Description copied from class: DatabindContext
        Helper method for constructing exception to indicate that given type id could not be resolved to a valid subtype of specified base type. Most commonly called during polymorphic deserialization.

        Note that most of the time this method should NOT be called directly: instead, method handleUnknownTypeId() should be called which will call this method if necessary.

        Specified by:
        invalidTypeIdException in class DatabindContext
      • endOfInputException

        @Deprecated
        public JsonMappingException endOfInputException(Class<?> instClass)
        Deprecated. Since 2.8; currently no way to catch EOF at databind level
        Helper method for constructing exception to indicate that end-of-input was reached while still expecting more tokens to deserialize value of specified type.
      • mappingException

        @Deprecated
        public JsonMappingException mappingException(String message)
        Deprecated. Since 2.9 use more specific error reporting methods instead
        Helper method for constructing generic mapping exception with specified message and current location information. Note that application code should almost always call one of handleXxx methods, or reportMappingException(String, Object...) instead.
        Since:
        2.6
      • mappingException

        @Deprecated
        public JsonMappingException mappingException(String msg,
                                                                 Object... msgArgs)
        Deprecated. Since 2.9 use more specific error reporting methods instead
        Helper method for constructing generic mapping exception with specified message and current location information Note that application code should almost always call one of handleXxx methods, or reportMappingException(String, Object...) instead.
        Since:
        2.6
      • _getDateFormat

        protected DateFormat _getDateFormat()
      • _shapeForToken

        protected String _shapeForToken(com.fasterxml.jackson.core.JsonToken t)
        Helper method for constructing description like "Object value" given JsonToken encountered.

Copyright © 2008–2022 FasterXML. All rights reserved.