Javatpoint Logo
Javatpoint Logo

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. Name Description
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. Method Description
1 protected 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.
2 protected void _reportUnsupportedOperation() It is another helper method that is used for the operations supported by parser implementation.
3 boolean 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.
4 boolean canReadTypeId() It is used to check whether the underlying data format natively supports some kind of Type Ids or not.
5 boolean canUseSchema(FormatSchema schema) It is used to check whether the specified schema can be used by the current parser or not.
6 abstract 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.
7 abstract 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.
8 JsonParser configure(JsonParser.Feature f, boolean state) It is used to enable or disable the given JsonParser feature.
9 JsonParser disable(JsonParser.Feature f) It is used to disable the given JsonParser feature.
10 JsonParser enable(JsonParser.Feature f) It is used to enable the given JsonParser feature.
11 abstract 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.
12 byte[] getBinaryValue() It is an alternative method of getBinaryValue(Base64Variant) that defaults to using Base64Variants.getDefaultVariant() as the default encoding.
13 abstract 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.
14 boolean getBooleanValue() It is an accessor that is used to access the value of the JsonToken.VALUE_TRUE and JsonToken.VALUE_FALSE.
15 byte 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.
16 abstract ObjectCodec getCodec() It is an accessor that is used to access the value of ObjectCodec associated with the current parser.
17 abstract JsonLocation getCurrentLocation() It is used to get the location of the processed character that will use for error reporting purposes.
18 abstract String getCurrentName() It is used for getting the name associated with the current token
19 abstract JsonToken getCurrentToken() The method is used to get the token pointed by the cursor. It will return null if no token is there.
20 abstract 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.
21 abstract 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.
22 abstract 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.
23 abstract Object getEmbeddedObject() It is an accessor that is used to access the value of the JsonToken.VALUE_EMBEDDED_OBJECT token.
24 int getFeatureMask() In order to get the state of all standard JsonParser.Features, we use this bulk access method.
25 abstract 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.
26 Object getInputSource() It is used to get access to the object that is used to access input being parsed.
27 abstract 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.
28 abstract JsonToken getLastClearedToken() The last token, which was cleared by using clearCurrentToken(), we use this method to get it.
29 abstract 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.
30 abstract 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.
31 abstract Number getNumberValue() It is used to access the value of all kinds of numeric values. So, it is a generic number value accessor method.
32 Object getObjectId() The method first checks whether the current token has an associated Object id or not. If true, it returns the id.
33 abstract JsonStreamContext getParsingContext() It is used to access the current parsing context readers is in.
34 FormatSchema getSchema() If any Schema is used by the current parser, the method is used for accessing it.
35 short 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.
36 abstract String getText() It is used to access a textual representation of the current token. If there is no current token, it will return null.
37 abstract 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.
38 abstract 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.
39 abstract 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.
40 abstract 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.
41 Object getTypeId() The method first checks whether the current token has an associated type id or not. If true, it returns it.
42 boolean getValueAsBoolean() It is used for converting the value of the current token to a Boolean.
43 boolean getValueAsBoolean(boolean defaultValue) It is used for converting the value of the current token to a Boolean.
44 double getValueAsDouble() It is used for converting the value of the current token to a Java double.
45 double getValueAsDouble(double defaultValue) It is used for converting the value of the current token to a Java double.
46 int getValueAsInt() It is used for converting the value of the current token to a Java int.
47 int getValueAsInt(int defaultValue) It is used for converting the value of the current token to a Java int.
48 long getValueAsLong() It is used for converting the value of the current token into the long.
49 long getValueAsLong(long defaultValue) It is used for converting the value of the current token into the long.
50 String getValueAsString() It is used for converting the value of the current token to a Java string.
51 abstract String getValueAsString(String defaultValue) It is used for converting the value of the current token to a Java string.
52 abstract boolean hasCurrentToken() It is used to check whether the parser is currently pointing to a token or not.
53 abstract 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.
54 abstract boolean isClosed() It is used to check whether the current parser is closed or not.
55 boolean isEnabled(JsonParser.Feature f) It is used to check whether the given JsonParser.Feature is enabled or not.
56 boolean 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.
57 Boolean 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.
58 boolean 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.
59 int 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.
60 long 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.
61 String 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.
62 abstract void overrideCurrentName(String name) It is used to change what is considered to be the current(field) name.
63 int 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.
64 int 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.
70 int releaseBuffered(OutputStream out) It is used for pushing back the data that has not been consumed by the parser but has been read.
71 int releaseBuffered(Writer w) It is also used for pushing back the data that has not been consumed by the parser but has been read.
72 abstract void setCodec(ObjectCodec c) It is a setter method that allows defining ObjectCodec associated with this parser if any.
73 JsonParser setFeatureMask(int mask) When we need to reset the states of all standard JsonParser.Features, we use this bulk set method.
74 void setSchema(FormatSchema schema) This method forces the current parser to use the given schema.
75 abstract 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.
76 abstract Version version() It is an accessor that is used to get the version of the core package.

Next TopicJackson Vs. Gson





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA