JsonParser Class in Jackson

All the public APIs that are used to read JSON data are defined by the JsonParser class. So, it is a base class for all those public APIs. We use factory methods of JsonFactory class for creating an instance of JsonParser.

The declaration of JsonParser class for com.fasterxml.jackson.core.JsonParser class is as follows:

Nested Classes of the JsonParser Class

There are the following two nested classes of the JsonParser class:

  • Features
  • NumberTypes

JsonParser.Feature is an enumeration that defines all togglable features for parsers.

Syntax:

JsonParser.NumberType is also an enumeration of possible native types that can be used for numbers.

Syntax:

Fields

protected int _features

It is a bit flag, i.e., composed of bits indicating which JsonParser.Features are enabled.

Constructors of the JsonParser Class

S.N.NameDescription
1.protected JsonParser()Create JsonParser instance with default featured.
2.protected JsonParser(int features)Create a JsonParser instance with given features.

Methods of the JsonParser Class

S.N.MethodDescription
1protected JsonParseException _constructError(String msg)It is a helper method, i.e., used to construct JsonParserException. The exception is based on the current state of the parser.
2protected void _reportUnsupportedOperation()It is another helper method that is used for the operations supported by parser implementation.
3boolean canReadObjectId()It is another useful method that is used to check whether the underlying data format natively supports some kind of Object Ids or not.
4boolean canReadTypeId()It is used to check whether the underlying data format natively supports some kind of Type Ids or not.
5boolean canUseSchema(FormatSchema schema)It is used to check whether the specified schema can be used by the current parser or not.
6abstract void clearCurrentToken()It is used to "consume" the current token by effectively removing it. After removing the token, the hasCurrentToken() and the getCurrentToken() method returns false and null respectively.
7abstract void close()It is used for closing the parser to stop further iterations and data access.

If the parser either owns the input source or the AUTO_CLOSE_SOURCE feature is enabled, it closes the underlying input source also.
8JsonParser configure(JsonParser.Feature f, boolean state)It is used to enable or disable the given JsonParser feature.
9JsonParser disable(JsonParser.Feature f)It is used to disable the given JsonParser feature.
10JsonParser enable(JsonParser.Feature f)It is used to enable the given JsonParser feature.
11abstract BigInteger getBigIntegerValue()It is a numeric accessor that is used when the type of the token is JsonToken.VALUE_NUMBER_INT, and it cannot be used as a Java long primitive type due to its magnitude.
12byte[] getBinaryValue()It is an alternative method of getBinaryValue(Base64Variant) that defaults to using Base64Variants.getDefaultVariant() as the default encoding.
13abstract byte[] getBinaryValue(Base64Variant b64variant)It is an important method that is used for reading base64-encoded binary data included in the current textual JSON value.
14boolean getBooleanValue()It is an accessor that is used to access the value of the JsonToken.VALUE_TRUE and JsonToken.VALUE_FALSE.
15byte getByteValue()It is a numeric accessor that is used for accessing the value of JsonToken.VALUE_NUMBER_INT token, i.e., expressed as a value of Java byte primitive type.
16abstract ObjectCodec getCodec()It is an accessor that is used to access the value of ObjectCodec associated with the current parser.
17abstract JsonLocation getCurrentLocation()It is used to get the location of the processed character that will use for error reporting purposes.
18abstract String getCurrentName()It is used for getting the name associated with the current token
19abstract JsonToken getCurrentToken()The method is used to get the token pointed by the cursor. It will return null if no token is there.
20abstract int getCurrentTokenId()It is similar to the getCurrentToken() method. The only difference between the getCureentTokenId() and getCurrentToke() is that the getCureentTokenId() returns an int where getCurrentToken() returns JsonToken.
21abstract BigDecimal getDecimalValue()It is an accessor that is used to access the value of the JsonToken.VALUE_NUMBER_FLOAT or JsonToken.VALUE_NUMBER_INT tokens.
22abstract double getDoubleValue()It is an accessor that is used to access the value of the JsonToken.VALUE_NUMBER_FLOAT token expressed as a Java double primitive type.
23abstract Object getEmbeddedObject()It is an accessor that is used to access the value of the JsonToken.VALUE_EMBEDDED_OBJECT token.
24int getFeatureMask()In order to get the state of all standard JsonParser.Features, we use this bulk access method.
25abstract float getFloatValue()It is an accessor that is used to access the value of the JsonToken.VALUE_NUMBER_FLOAT token expressed as a Java float primitive type.
26Object getInputSource()It is used to get access to the object that is used to access input being parsed.
27abstract int getIntValue()It is an accessor that is used to access the value of the JsonToken.VALUE_NUMBER_ INT token expressed as a Java int primitive type.
28abstract JsonToken getLastClearedToken()The last token, which was cleared by using clearCurrentToken(), we use this method to get it.
29abstract long getLongValue()It is a numeric accessor that is used to access the value of the JsonToken.VALUE_NUMBER_ INT token expressed as a Java long primitive type.
30abstract JsonParser.NumberType getNumberType()The method returns one of JsonParser.NumberType constants, when the token is of type JsonToken.VALUE_NUMBER_INT or JsonToken.VALUE_NUMBER_FLOAT.
31abstract Number getNumberValue()It is used to access the value of all kinds of numeric values. So, it is a generic number value accessor method.
32Object getObjectId()The method first checks whether the current token has an associated Object id or not. If true, it returns the id.
33abstract JsonStreamContext getParsingContext()It is used to access the current parsing context readers is in.
34FormatSchema getSchema()If any Schema is used by the current parser, the method is used for accessing it.
35short getShortValue()It is also a numeric accessor that is used to access the value of the JsonToken.VALUE_NUMBER_ INT token expressed as a Java short primitive type.
36abstract String getText()It is used to access a textual representation of the current token. If there is no current token, it will return null.
37abstract char[] getTextCharacters()It is similar to the getText() method, but that will return an underlying (unmodifiable) character array that contains the textual value instead of constructing a String object to contain this information.
38abstract int getTextLength()It is used to get the length of the String, i.e., stored in the returned buffer. It is an accessor method that is used with the getTextCharacters() method.
39abstract int getTextOffset()It is used to get the offset of the first text content character within the buffer. It is an accessor method that is used with the getTextCharacters() method.
40abstract JsonLocation getTokenLocation()It is used to get the starting location of the current token. The starting location denotes the position of the first character from the input that starts the current token.
41Object getTypeId()The method first checks whether the current token has an associated type id or not. If true, it returns it.
42boolean getValueAsBoolean()It is used for converting the value of the current token to a Boolean.
43boolean getValueAsBoolean(boolean defaultValue)It is used for converting the value of the current token to a Boolean.
44double getValueAsDouble()It is used for converting the value of the current token to a Java double.
45double getValueAsDouble(double defaultValue)It is used for converting the value of the current token to a Java double.
46int getValueAsInt()It is used for converting the value of the current token to a Java int.
47int getValueAsInt(int defaultValue)It is used for converting the value of the current token to a Java int.
48long getValueAsLong()It is used for converting the value of the current token into the long.
49long getValueAsLong(long defaultValue)It is used for converting the value of the current token into the long.
50String getValueAsString()It is used for converting the value of the current token to a Java string.
51abstract String getValueAsString(String defaultValue)It is used for converting the value of the current token to a Java string.
52abstract boolean hasCurrentToken()It is used to check whether the parser is currently pointing to a token or not.
53abstract boolean hasTextCharacters()It returns a Boolean value that defines whether calling of getTextCharacters() would be the most efficient way to access textual content for the event parser currently points to.
54abstract boolean isClosed()It is used to check whether the current parser is closed or not.
55boolean isEnabled(JsonParser.Feature f)It is used to check whether the given JsonParser.Feature is enabled or not.
56boolean isExpectedStartArrayToken()It is a specialized accessor method that is used for verifying that the current token indicates the start array or not when the array is expected.
57Boolean nextBooleanValue()The method first fetches the next token and if the token is JsonToken.VALUE_TRUE or JsonToken.VALUE_FALSE, it will return a Boolean value; else return null.
58boolean nextFieldName(SerializableString str)The method first fetches the next token and if the token is JsonToken.FIELD_NAME with the specified name, it will return a Boolean value.
59int nextIntValue(int defaultValue)The method first fetches the next token and if the token is JsonToken.VALUE_NUMBER_INT, it will return a 32-bit int value; else, it will return the specified default value.
60long nextLongValue(long defaultValue)The method first fetches the next token and if the token is JsonToken.VALUE_NUMBER_INT, it will return a 64-bit long value; else, it will return the specified default value.
61String nextTextValue()The method first fetches the next token and if the token is JsonToken.VALUE_ STRING, it will return contained string value; else, it will return null.
62abstract void overrideCurrentName(String name)It is used to change what is considered to be the current(field) name.
63int readBinaryValue(Base64Variant b64variant, OutputStream out)It is an alternative to the readBinaryValue(OutputStream). The only difference is that it allows explicitly specifying base64 variants to use.
64int readBinaryValue(OutputStream out)It is an alternative of getBigIntegerValue() which we use when the value is large.
65<T> T readValueAs(Class<T> valueType)It is used for de-serializing the JSON data into a non-container type that can be a bean, wrapper or an array.
66<T> T readValueAs(TypeReference<?> valueTypeRef)It is used for deserializing the JSON data into a Java type, a reference to which is passed as an argument.
67<T extends TreeNode> T readValueAsTree()It is used for deserializing the JSON content into an equivalent tree model. The tree model will be represented by the root TreeNode of the resulting model.
68<T> Iterator<T> readValuesAs(Class<T> valueType)It is used to read sequence of Objects(same specified value type) from parser stream.
69<T> Iterator<T> readValuesAs(TypeReference<?> valueTypeRef)It is used to read sequence of Objects(same specified value type) from parser stream.
70int releaseBuffered(OutputStream out)It is used for pushing back the data that has not been consumed by the parser but has been read.
71int releaseBuffered(Writer w)It is also used for pushing back the data that has not been consumed by the parser but has been read.
72abstract void setCodec(ObjectCodec c)It is a setter method that allows defining ObjectCodec associated with this parser if any.
73JsonParser setFeatureMask(int mask)When we need to reset the states of all standard JsonParser.Features, we use this bulk set method.
74void setSchema(FormatSchema schema)This method forces the current parser to use the given schema.
75abstract JsonParser skipChildren()If the JsonToken.START_OBJECT or JsonToken.START_ARRAY are pointed by the stream; the method either skip the object token or all the child tokens of the array pointed by the current parser.
76abstract Version version()It is an accessor that is used to get the version of the core package.