Top Description Inners Fields Constructors Methods
tools.jackson.core

public abstract Class JsonParser

extends Object
implements Closeable, Versioned
Class Inheritance
All Implemented Interfaces
tools.jackson.core.Versioned, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
tools.jackson.core.base.ParserMinimalBase, tools.jackson.core.util.JsonParserDelegate
Imports
java.io.*, java.math.BigDecimal, .BigInteger, tools.jackson.core.async.NonBlockingInputFeeder, tools.jackson.core.exc.InputCoercionException, .JacksonIOException, .StreamReadException, tools.jackson.core.json.JsonFactory, .JsonReadFeature, tools.jackson.core.sym.PropertyNameMatcher, tools.jackson.core.type.ResolvedType, .TypeReference, tools.jackson.core.util.*

Base class that defines public API for reading JSON content. Instances are created using factory methods of a JsonFactory instance.
Author
Tatu Saloranta

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
JsonParser.NumberType

Enumeration of possible "native" (optimal) types that can be used for numbers.

public static enum
JsonParser.NumberTypeFP

Enumeration of possible physical Floating-Point types that underlying format uses.

Field Summary

Modifier and TypeField and Description
protected static final JacksonFeatureSet<StreamReadCapability>
DEFAULT_READ_CAPABILITIES

Set of default StreamReadCapabilityies enabled: usable as basis for format-specific instances or placeholder if non-null instance needed.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected StreamReadException

Returns:

StreamReadException constructed
_constructReadException
(String
Base exception message to construct exception with
msg
)

Helper method for constructing StreamReadException based on current state of the parser

protected StreamReadException
protected StreamReadException
protected StreamReadException
protected StreamReadException

Returns:

Read exception (of type StreamReadException) constructed
_constructReadException
(String
Base exception message to construct exception with
msg
,
Throwable
Root cause to assign
t
)

Helper method for constructing StreamReadException based on current state of the parser and indicating that the given Throwable is the root cause.

protected StreamReadException

Returns:

Read exception (of type StreamReadException) constructed
_constructReadException
(String
Base exception message to construct exception with
msg
,
TokenStreamLocation
Error location to report
loc
)

Helper method for constructing StreamReadException based on current state of the parser, except for specified TokenStreamLocation for problem location (which may not be the exact current location)

protected void
_reportUnsupportedOperation()

Helper method to call for operations that are not supported by parser implementation.

public abstract void
assignCurrentValue(Object
"Current value" to assign to the current input context of this parser
v
)

Helper method, usually equivalent to: getParsingContext().assignCurrentValue(v);

public boolean

Returns:

True if this is a non-blocking ("asynchronous") parser
canParseAsync
()

Method that can be called to determine if this parser instance uses non-blocking ("asynchronous") input access for decoding or not.

public boolean

Returns:

True if the format being read supports native Object Ids; false if not
canReadObjectId
()

Introspection method that may be called to see if the underlying data format supports some kind of Object Ids natively (many do not; for example, JSON doesn't).

public boolean

Returns:

True if the format being read supports native Type Ids; false if not
canReadTypeId
()

Introspection method that may be called to see if the underlying data format supports some kind of Type Ids natively (many do not; for example, JSON doesn't).

public abstract void
clearCurrentToken()

Method called to "consume" the current token by effectively removing it so that hasCurrentToken returns false, and currentToken null).

public abstract void
close()

Redeclares java.io.Closeable.close.

Closes the parser so that no further iteration or data access can be made; will also close the underlying input source if parser either owns the input source, or feature StreamReadFeature#AUTO_CLOSE_SOURCE is enabled.

public abstract TokenStreamLocation

Returns:

Location of the last processed input unit (byte or character)
currentLocation
()

Method that returns location of the last processed character; usually for error reporting purposes.

public abstract String

Returns:

Name of the current property name, if any, in the parsing context (null if none)
currentName
()

Method that can be called to get the name associated with the current token: for JsonToken#PROPERTY_NAMEs it will be the same as what getString() returns; for Object property values it will be the preceding property name; and for others (array element, root-level values) null.

public abstract int

Returns:

Index of the matched property name, if non-negative, or a negative error code otherwise (see PropertyNameMatcher for details)
currentNameMatch
(PropertyNameMatcher
Matcher that will handle actual matching
matcher
)

Method that verifies that the current token (see currentToken) is JsonToken#PROPERTY_NAME and if so, further match that associated name (see currentName) to one of pre-specified (property) names.

public abstract JsonToken

Returns:

Type of the token this parser currently points to, if any: null before any tokens have been read, and after end-of-input has been encountered, as well as if the current token has been explicitly cleared.
currentToken
()

Accessor to find which token parser currently points to, if any; null will be returned if none.

public abstract long

Returns:

the number of tokens that have been read (-1 if the count is not available)
currentTokenCount
()

Get an approximate count of the number of tokens that have been read.

public abstract int

Returns:

int matching one of constants from JsonTokenId.
currentTokenId
()

Method similar to currentToken() but that returns an int instead of JsonToken (enum value).

public abstract TokenStreamLocation

Returns:

Starting location of the token parser currently points to
currentTokenLocation
()

Method that return the starting location of the current token; that is, position of the first character from input that starts the current token.

public abstract Object

Returns:

"Current value" for the current input context this parser has
currentValue
()

Helper method, usually equivalent to: getParsingContext().currentValue();

Note that "current value" is NOT populated (or used) by Streaming parser; it is only used by higher-level data-binding functionality.

public abstract void
finishToken()

Method that may be used to force full handling of the current token so that even if lazy processing is enabled, the whole contents are read for possible retrieval.

public abstract BigInteger

Returns:

Current number value as BigInteger (if numeric token); otherwise exception thrown
getBigIntegerValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_INT and it cannot be used as a Java long primitive type due to its magnitude.

public abstract byte[]

Returns:

Decoded binary data
getBinaryValue
(Base64Variant
Expected variant of base64 encoded content (see Base64Variants for definitions of "standard" variants).
bv
)

Method that can be used to read (and consume -- results may not be accessible using other methods after the call) base64-encoded binary data included in the current textual JSON value.

public byte[]

Returns:

Decoded binary data
getBinaryValue
()

Convenience alternative to getBinaryValue(Base64Variant) that defaults to using Base64Variants#getDefaultVariant as the default encoding.

public abstract boolean

Returns:

True if current token is JsonToken.VALUE_TRUE, false if current token is JsonToken.VALUE_FALSE; otherwise throws InputCoercionException
getBooleanValue
()

Convenience accessor that can be called when the current token is JsonToken#VALUE_TRUE or JsonToken#VALUE_FALSE, to return matching boolean value.

public abstract byte

Returns:

Current number value as byte (if numeric token within range of [-128, 255]); otherwise exception thrown
getByteValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_INT and it can be expressed as a value of Java byte primitive type.

public abstract BigDecimal

Returns:

Current number value as BigDecimal (if numeric token); otherwise exception thrown
getDecimalValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_FLOAT or JsonToken#VALUE_NUMBER_INT.

public abstract double

Returns:

Current number value as double (if numeric token within Java double range); otherwise exception thrown
getDoubleValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_FLOAT and it can be expressed as a Java double primitive type.

public abstract Object

Returns:

Embedded value (usually of "native" type supported by format) for the current token, if any; null otherwise
getEmbeddedObject
()

Accessor that can be called if (and only if) the current token is JsonToken#VALUE_EMBEDDED_OBJECT.

public abstract float

Returns:

Current number value as float (if numeric token within Java float range); otherwise exception thrown
getFloatValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_FLOAT and it can be expressed as a Java float primitive type.

public abstract int

Returns:

Current number value as int (if numeric token within Java 32-bit signed int range); otherwise exception thrown
getIntValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_INT and it can be expressed as a value of Java int primitive type.

public abstract JsonToken

Returns:

Last cleared token, if any; null otherwise
getLastClearedToken
()

Method that can be called to get the last token that was cleared using clearCurrentToken.

public abstract long

Returns:

Current number value as long (if numeric token within Java 32-bit signed long range); otherwise exception thrown
getLongValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_INT and it can be expressed as a Java long primitive type.

public abstract JsonParser.NumberType

Returns:

Type of current number, if parser points to numeric token; null otherwise
getNumberType
()

If current token is of type JsonToken#VALUE_NUMBER_INT or JsonToken#VALUE_NUMBER_FLOAT, returns one of NumberType constants; otherwise returns null.

public abstract JsonParser.NumberTypeFP

Returns:

Type of current number, if parser points to numeric token; null otherwise
getNumberTypeFP
()

If current token is of type JsonToken#VALUE_NUMBER_FLOAT, returns one of NumberTypeFP constants; otherwise returns NumberTypeFP#UNKNOWN.

public abstract Number

Returns:

Numeric value of the current token in its most optimal representation
getNumberValue
()

Generic number value accessor method that will work for all kinds of numeric values.

public abstract Object

Returns:

Either Number (for already decoded numbers) or String (for deferred decoding).
getNumberValueDeferred
()

Method similar to getNumberValue but that returns either same Number value as getNumberValue() (if already decoded), or String representation of as-of-yet undecoded number.

public abstract Number

Returns:

Numeric value of the current token using most accurate representation
getNumberValueExact
()

Method similar to getNumberValue with the difference that for floating-point numbers value returned may be BigDecimal if the underlying format does not store floating-point numbers using native representation: for example, textual formats represent numbers as Strings (which are 10-based), and conversion to java.lang.Double is potentially lossy operation.

public Object

Returns:

Native Object id associated with the current token, if any; null if none
getObjectId
()

Method that can be called to check whether current token (one that was just read) has an associated Object id, and if so, return it.

public FormatSchema

Returns:

FormatSchema assigned to this parser, if any; null if none
getSchema
()

Method for accessing Schema that this parser uses, if any.

public abstract short

Returns:

Current number value as short (if numeric token within Java 16-bit signed short range); otherwise exception thrown
getShortValue
()

Numeric accessor that can be called when the current token is of type JsonToken#VALUE_NUMBER_INT and it can be expressed as a value of Java short primitive type.

public abstract String

Returns:

String value associated with the current token (one returned by nextToken() or other iteration methods)
getString
()

Method for accessing textual representation of the current token; if no current token (before first call to nextToken, or after encountering end-of-input), returns null.

public abstract int

Returns:

The number of characters written to the Writer
getString
(Writer
Writer to write String value to
writer
)

Method to read the textual representation of the current token in chunks and pass it to the given Writer.

public abstract char[]

Returns:

Buffer that contains the current textual value (but not necessarily at offset 0, and not necessarily until the end of buffer)
getStringCharacters
()

Method similar to getString(), but that will return underlying (unmodifiable) character array that contains textual value, instead of constructing a String object to contain this information.

public abstract int

Returns:

Number of characters within buffer returned by getStringCharacters that are part of textual content of the current token.
getStringLength
()

Accessor used with getStringCharacters, to know length of String stored in returned buffer.

public abstract int

Returns:

Offset of the first character within buffer returned by getStringCharacters that is part of textual content of the current token.
getStringOffset
()

Accessor used with getStringCharacters, to know offset of the first text content character within buffer.

public String
getText()
Deprecated since 3.0 use getString() instead.

Deprecated alias for getString(): MAY be removed in a 3.x version past 3.0; only included to help initial migration.

public char[]
getTextCharacters()
Deprecated since 3.0 use getStringCharacters() instead.

Deprecated alias for getStringCharacters(): MAY be removed in a 3.x version past 3.0; only included to help initial migration.

public int
getTextLength()
Deprecated since 3.0 use getStringLength() instead.

Deprecated alias for getStringLength(): MAY be removed in a 3.x version past 3.0; only included to help initial migration.

public int
getTextOffset()
Deprecated since 3.0 use getStringOffset() instead.

Deprecated alias for getStringOffset(): MAY be removed in a 3.x version past 3.0; only included to help initial migration.

public Object

Returns:

Native Type Id associated with the current token, if any; null if none
getTypeId
()

Method that can be called to check whether current token (one that was just read) has an associated type id, and if so, return it.

public boolean

Returns:

boolean value current token is converted to, if possible; or false if not
getValueAsBoolean
()

Method that will try to convert value of current token to a boolean.

public abstract boolean

Returns:

boolean value current token is converted to, if possible; def otherwise
getValueAsBoolean
(boolean
Default value to return if conversion to boolean is not possible
def
)

Method that will try to convert value of current token to a boolean.

public double

Returns:

double value current token is converted to, if possible; default value otherwise
getValueAsDouble
()

Method that will try to convert value of current token to a Java double.

public double

Returns:

double value current token is converted to, if possible; def otherwise
getValueAsDouble
(double
Default value to return if conversion to double is not possible
def
)

Method that will try to convert value of current token to a Java double.

public int

Returns:

int value current token is converted to, if possible; default value otherwise otherwise
getValueAsInt
()

Method that will try to convert value of current token to a Java int value.

public int

Returns:

int value current token is converted to, if possible; def otherwise
getValueAsInt
(int
Default value to return if conversion to int is not possible
def
)

Method that will try to convert value of current token to a int.

public long

Returns:

long value current token is converted to, if possible; default value otherwise
getValueAsLong
()

Method that will try to convert value of current token to a long.

public long

Returns:

long value current token is converted to, if possible; def otherwise
getValueAsLong
(long
Default value to return if conversion to long is not possible
def
)

Method that will try to convert value of current token to a long.

public String

Returns:

String value current token is converted to, if possible; null otherwise
getValueAsString
()

Method that will try to convert value of current token to a java.lang.String.

public abstract String

Returns:

String value current token is converted to, if possible; def otherwise
getValueAsString
(String
Default value to return if conversion to String is not possible
def
)

Method that will try to convert value of current token to a java.lang.String.

public abstract boolean

Returns:

True if the parser just returned a valid token via nextToken; false otherwise (parser was just constructed, encountered end-of-input and returned null from nextToken, or the token has been consumed)
hasCurrentToken
()

Method for checking whether parser currently points to a token (and data for that token is available).

public abstract boolean

Returns:

True if parser currently has character array that can be efficiently returned via getStringCharacters; false means that it may or may not exist
hasStringCharacters
()

Method that can be used to determine whether calling of getStringCharacters would be the most efficient way to access String value for the event parser currently points to (compared to getString()).

public abstract boolean

Returns:

True if the parser current points to specified token
hasToken
(JsonToken
Token to match
t
)

Method that is functionally equivalent to: return currentToken() == t but may be more efficiently implemented.

public abstract boolean

Returns:

True if the parser current points to specified token
hasTokenId
(int
Token id to match (from (@link JsonTokenId})
id
)

Method that is functionally equivalent to: return currentTokenId() == id but may be more efficiently implemented.

public abstract boolean

Returns:

True if this parser instance has been closed
isClosed
()

Method that can be called to determine whether this parser is closed or not.

public abstract boolean

Returns:

True if feature is enabled; false otherwise
isEnabled
(StreamReadFeature
Feature to check
f
)

Method for checking whether specified StreamReadFeature is enabled.

public abstract boolean

Returns:

True if the current token can be considered as a start-array marker (such JsonToken#VALUE_NUMBER_INT); false if not
isExpectedNumberIntToken
()

Similar to isExpectedStartArrayToken(), but checks whether stream currently points to JsonToken#VALUE_NUMBER_INT.

public abstract boolean

Returns:

True if the current token can be considered as a start-array marker (such JsonToken#START_ARRAY); false if not
isExpectedStartArrayToken
()

Specialized accessor that can be used to verify that the current token indicates start array (usually meaning that current token is JsonToken#START_ARRAY) when start array is expected.

public abstract boolean

Returns:

True if the current token can be considered as a start-array marker (such JsonToken#START_OBJECT); false if not
isExpectedStartObjectToken
()

Similar to isExpectedStartArrayToken(), but checks whether stream currently points to JsonToken#START_OBJECT.

public abstract boolean

Returns:

True if the current token is reported as JsonToken#VALUE_NUMBER_FLOAT and represents a "Not a Number" value; false for other tokens and regular floating-point numbers.
isNaN
()

Accessor for checking whether current token is a special "not-a-number" (NaN) token (including both "NaN" AND positive/negative infinity!).

public Boolean

Returns:

Boolean value of the JsonToken.VALUE_TRUE or JsonToken.VALUE_FALSE token parser advanced to; or null if next token is of some other type
nextBooleanValue
()

Method that fetches next token (as if calling nextToken) and if it is JsonToken#VALUE_TRUE or JsonToken#VALUE_FALSE returns matching Boolean value; otherwise return null.

public int

Returns:

Integer (int) value of the JsonToken.VALUE_NUMBER_INT token parser advanced to; or defaultValue if next token is of some other type
nextIntValue
(int
Value to return if next token is NOT of type JsonToken.VALUE_NUMBER_INT
defaultValue
)

Method that fetches next token (as if calling nextToken) and if it is JsonToken#VALUE_NUMBER_INT returns 32-bit int value; otherwise returns specified default value It is functionally equivalent to:

  return (nextToken() == JsonToken.VALUE_NUMBER_INT) ?
public long

Returns:

long value of the JsonToken.VALUE_NUMBER_INT token parser advanced to; or defaultValue if next token is of some other type
nextLongValue
(long
Value to return if next token is NOT of type JsonToken.VALUE_NUMBER_INT
defaultValue
)

Method that fetches next token (as if calling nextToken) and if it is JsonToken#VALUE_NUMBER_INT returns 64-bit long value; otherwise returns specified default value It is functionally equivalent to:

  return (nextToken() == JsonToken.VALUE_NUMBER_INT) ?
public abstract String

Returns:

Name of the the JsonToken.PROPERTY_NAME parser advanced to, if any; null if next token is of some other type
nextName
()

Method that fetches next token (as if calling nextToken) and verifies whether it is JsonToken#PROPERTY_NAME; if it is, returns same as currentName(), otherwise null.

public abstract boolean

Returns:

True if parser advanced to JsonToken.PROPERTY_NAME with specified name; false otherwise (different token or non-matching name)
nextName
(SerializableString
Property name to compare next token to (if next token is JsonToken.PROPERTY_NAME)
str
)

Method that fetches next token (as if calling nextToken) and verifies whether it is JsonToken#PROPERTY_NAME with specified name and returns result of that comparison.

public abstract int

Returns:

Index of the matched property name, if non-negative, or a negative error code otherwise (see PropertyNameMatcher for details)
nextNameMatch
(PropertyNameMatcher
Matcher that will handle actual matching
matcher
)

Method that tries to match next token from stream as JsonToken#PROPERTY_NAME, and if so, further match it to one of pre-specified (field) names.

public String

Returns:

String value of JsonToken.VALUE_STRING token parser advanced to; or null if next token is of some other type
nextStringValue
()

Method that fetches next token (as if calling nextToken) and if it is JsonToken#VALUE_STRING returns contained String value; otherwise returns null.

public abstract JsonToken

Returns:

Next token from the stream, if any found, or null to indicate end-of-input
nextToken
()

Main iteration method, which will advance stream enough to determine type of the next token, if any.

public abstract JsonToken

Returns:

Next non-field-name token from the stream, if any found, or null to indicate end-of-input (or, for non-blocking parsers, JsonToken#NOT_AVAILABLE if no tokens were available yet)
nextValue
()

Iteration method that will advance stream enough to determine type of the next token that is a value type (including JSON Array and Object start/end markers).

public NonBlockingInputFeeder

Returns:

Input feeder to use with non-blocking (async) parsing
nonBlockingInputFeeder
()

Method that will either return a feeder instance (if parser uses non-blocking, aka asynchronous access); or null for parsers that use blocking I/O.

public abstract ObjectReadContext

Returns:

Object write context (ObjectReadContext) associated with this parser
objectReadContext
()

Accessor for context object provided by higher level data-binding functionality (or, in some cases, simple placeholder of the same) that allows some level of interaction including ability to trigger deserialization of Object values through generator instance.

public int

Returns:

Number of bytes that were decoded and written via OutputStream
readBinaryValue
(OutputStream
Output stream to use for passing decoded binary data
out
)

Method that can be used as an alternative to getBinaryValue(), especially when value can be large.

public int

Returns:

Number of bytes that were decoded and written via OutputStream
readBinaryValue
(Base64Variant
base64 variant to use
bv
,
OutputStream
Output stream to use for passing decoded binary data
out
)

Similar to readBinaryValue(OutputStream) but allows explicitly specifying base64 variant to use.

public abstract <
Nominal type parameter to specify expected node type to reduce need to cast result value
T
>
T

Returns:

Java value read from content
readValueAs
(Class<T>
Type to bind content to
valueType
)

Method to deserialize stream content into a non-container type (it can be an array type, however): typically a bean, array or a wrapper type (like java.lang.Boolean)

Note

method can only be called if the parser has been constructed with a linkage to ObjectReadContext; this is true if constructed by databinding layer above, or by factory method that takes in context object.

public abstract <
Nominal type parameter to specify expected node type to reduce need to cast result value
T
>
T

Returns:

Java value read from content
readValueAs
(TypeReference<T>
Type to bind content to
valueTypeRef
)

Method to deserialize stream content into a Java type, reference to which is passed as argument.

public abstract <T> T
public abstract <
Nominal type parameter for result node type (to reduce need for casting)
T extends TreeNode
>
T

Returns:

root of the document, or null if empty or whitespace.
readValueAsTree
()

Method to deserialize stream content into equivalent "tree model", represented by root TreeNode of resulting model.

public int

Returns:

-1 if the underlying content source is not byte based (that is, input cannot be sent to OutputStream; otherwise number of bytes released (0 if there was nothing to release)
releaseBuffered
(OutputStream
OutputStream to which buffered, undecoded content is written to
out
)

Method that can be called to push back any content that has been read but not consumed by the parser.

public int

Returns:

-1 if the underlying content source is not char-based (that is, input cannot be sent to Writer; otherwise number of chars released (0 if there was nothing to release)
releaseBuffered
(Writer
Writer to which buffered but unprocessed content is written to
w
)

Method that can be called to push back any content that has been read but not consumed by the parser.

public abstract JsonParser

Returns:

This parser, to allow call chaining
skipChildren
()

Method that will skip all child tokens of an array or object token that the parser currently points to, iff stream points to JsonToken#START_OBJECT or JsonToken#START_ARRAY.

public abstract JacksonFeatureSet<StreamReadCapability>

Returns:

Set of read capabilities for content to read via this parser
streamReadCapabilities
()

Accessor for getting metadata on capabilities of this parser, based on underlying data format being read (directly or indirectly).

public abstract StreamReadConstraints

Returns:

Constraints applied to this parser
streamReadConstraints
()

Get the constraints to apply when performing streaming reads.

public abstract TokenStreamContext

Returns:

Stream output context (TokenStreamContext) associated with this parser
streamReadContext
()

Method that can be used to access current parsing context reader is in. There are 3 different types: root, array and object contexts, with slightly different available information.

public abstract int

Returns:

Bit mask that defines current states of all standard StreamReadFeatures.
streamReadFeatures
()

Bulk access method for getting state of all standard StreamReadFeatures.

public abstract Object

Returns:

Input source this parser was configured with
streamReadInputSource
()

Method that can be used to get access to object that is used to access input being parsed; this is usually either InputStream or Reader, depending on what parser was constructed with.

public abstract Version
version()

Redeclares tools.jackson.core.Versioned.version.

Accessor for getting version of the core package, given a parser instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait