Class JsonParser (1.42.3)

public abstract class JsonParser implements Closeable

Abstract low-level JSON parser. See https://developers.google.com/api-client-library/java/google-http-java-client/json

Implementation has no fields and therefore thread-safe, but sub-classes are not necessarily thread-safe.

If a JSON map is encountered while using a destination class of type Map, then an java.util.ArrayMap is used by default for the parsed values.

Inheritance

Object > JsonParser

Implements

Closeable

Constructors

JsonParser()

public JsonParser()

Methods

<T>parse(Class<T> destinationClass)

public final T <T>parse(Class<T> destinationClass)

Parse a JSON object, array, or value into a new instance of the given destination class.

If it parses an object, after this method ends, the current token will be the object's ending JsonToken#END_OBJECT. If it parses an array, after this method ends, the current token will be the array's ending JsonToken#END_ARRAY.

Parameter
NameDescription
destinationClassClass<T>

destination class that has a public default constructor to use to create a new instance

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
IOException

<T>parse(Class<T> destinationClass, CustomizeJsonParser customizeParser)

public final T <T>parse(Class<T> destinationClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON object, array, or value into a new instance of the given destination class, optionally using the given parser customizer.

If it parses an object, after this method ends, the current token will be the object's ending JsonToken#END_OBJECT. If it parses an array, after this method ends, the current token will be the array's ending JsonToken#END_ARRAY.

Parameters
NameDescription
destinationClassClass<T>

destination class that has a public default constructor to use to create a new instance

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
IOException

<T>parseAndClose(Class<T> destinationClass)

public final T <T>parseAndClose(Class<T> destinationClass)

Parse a JSON object, array, or value into a new instance of the given destination class, and then closes the parser.

Parameter
NameDescription
destinationClassClass<T>

destination class that has a public default constructor to use to create a new instance

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
IOException

<T>parseAndClose(Class<T> destinationClass, CustomizeJsonParser customizeParser)

public final T <T>parseAndClose(Class<T> destinationClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON object, array, or value into a new instance of the given destination class using <xref uid="com.google.api.client.json.JsonParser.

Parameters
NameDescription
destinationClassClass<T>

destination class that has a public default constructor to use to create a new instance

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Returns
TypeDescription
T

new instance of the parsed destination class

Exceptions
TypeDescription
IOException

<T>parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass)

public final Collection<T> <T>parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass)

Parse a JSON Array from the given JSON parser into the given destination collection.

Parameters
NameDescription
destinationCollectionClassClass<?>

class of destination collection (must have a public default constructor)

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

Returns
TypeDescription
Collection<T>
Exceptions
TypeDescription
IOException

<T>parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

public final Collection<T> <T>parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.

Parameters
NameDescription
destinationCollectionClassClass<?>

class of destination collection (must have a public default constructor)

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Returns
TypeDescription
Collection<T>
Exceptions
TypeDescription
IOException

<T>parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass)

public final void <T>parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass)

Parse a JSON Array from the given JSON parser into the given destination collection.

Parameters
NameDescription
destinationCollectionCollection<? super T>

destination collection

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

Exceptions
TypeDescription
IOException

<T>parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

public final void <T>parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.

Parameters
NameDescription
destinationCollectionCollection<? super T>

destination collection

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Exceptions
TypeDescription
IOException

<T>parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass)

public final Collection<T> <T>parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass)

Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.

Parameters
NameDescription
destinationCollectionClassClass<?>

class of destination collection (must have a public default constructor)

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

Returns
TypeDescription
Collection<T>
Exceptions
TypeDescription
IOException

<T>parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

public final Collection<T> <T>parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection, optionally using the given parser customizer.

Parameters
NameDescription
destinationCollectionClassClass<?>

class of destination collection (must have a public default constructor)

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Returns
TypeDescription
Collection<T>
Exceptions
TypeDescription
IOException

<T>parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass)

public final void <T>parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass)

Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.

Parameters
NameDescription
destinationCollectionCollection<? super T>

destination collection

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

Exceptions
TypeDescription
IOException

<T>parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

public final void <T>parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser)

Beta
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection, optionally using the given parser customizer.

Parameters
NameDescription
destinationCollectionCollection<? super T>

destination collection

destinationItemClassClass<T>

class of destination collection item (must have a public default constructor)

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Exceptions
TypeDescription
IOException

close()

public abstract void close()

Closes the parser and the underlying input stream or reader, and releases any memory associated with it.

Exceptions
TypeDescription
IOException

getBigIntegerValue()

public abstract BigInteger getBigIntegerValue()

Returns the BigInteger value of the current token.

Returns
TypeDescription
BigInteger
Exceptions
TypeDescription
IOException

getByteValue()

public abstract byte getByteValue()

Returns the byte value of the current token.

Returns
TypeDescription
byte
Exceptions
TypeDescription
IOException

getCurrentName()

public abstract String getCurrentName()

Returns the most recent field name or null for array values or for root-level values.

Returns
TypeDescription
String
Exceptions
TypeDescription
IOException

getCurrentToken()

public abstract JsonToken getCurrentToken()

Returns the token the parser currently points to or null for none (at start of input or after end of input).

Returns
TypeDescription
JsonToken

getDecimalValue()

public abstract BigDecimal getDecimalValue()

Returns the BigDecimal value of the current token.

Returns
TypeDescription
BigDecimal
Exceptions
TypeDescription
IOException

getDoubleValue()

public abstract double getDoubleValue()

Returns the double value of the current token.

Returns
TypeDescription
double
Exceptions
TypeDescription
IOException

getFactory()

public abstract JsonFactory getFactory()

Returns the JSON factory from which this generator was created.

Returns
TypeDescription
JsonFactory

getFloatValue()

public abstract float getFloatValue()

Returns the float value of the current token.

Returns
TypeDescription
float
Exceptions
TypeDescription
IOException

getIntValue()

public abstract int getIntValue()

Returns the int value of the current token.

Returns
TypeDescription
int
Exceptions
TypeDescription
IOException

getLongValue()

public abstract long getLongValue()

Returns the long value of the current token.

Returns
TypeDescription
long
Exceptions
TypeDescription
IOException

getShortValue()

public abstract short getShortValue()

Returns the short value of the current token.

Returns
TypeDescription
short
Exceptions
TypeDescription
IOException

getText()

public abstract String getText()

Returns a textual representation of the current token or null if #getCurrentToken() is null.

Returns
TypeDescription
String
Exceptions
TypeDescription
IOException

nextToken()

public abstract JsonToken nextToken()

Returns the next token from the stream or null to indicate end of input.

Returns
TypeDescription
JsonToken
Exceptions
TypeDescription
IOException

parse(Object destination)

public final void parse(Object destination)

Parse a JSON object from the given JSON parser into the given destination object.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name. After this method ends, the current token will be the JsonToken#END_OBJECT of the current object.

Parameter
NameDescription
destinationObject

destination object

Exceptions
TypeDescription
IOException

parse(Object destination, CustomizeJsonParser customizeParser)

public final void parse(Object destination, CustomizeJsonParser customizeParser)

Beta
Parse a JSON object from the given JSON parser into the given destination object, optionally using the given parser customizer.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name. After this method ends, the current token will be the JsonToken#END_OBJECT of the current object.

Parameters
NameDescription
destinationObject

destination object

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Exceptions
TypeDescription
IOException

parse(Type dataType, boolean close)

public Object parse(Type dataType, boolean close)

Parse a JSON object, array, or value into a new instance of the given destination class.

If it parses an object, after this method ends, the current token will be the object's ending JsonToken#END_OBJECT. If it parses an array, after this method ends, the current token will be the array's ending JsonToken#END_ARRAY.

Parameters
NameDescription
dataTypeType

Type into which the JSON should be parsed

closeboolean

true if #close() should be called after parsing

Returns
TypeDescription
Object

new instance of the parsed dataType

Exceptions
TypeDescription
IOException

parse(Type dataType, boolean close, CustomizeJsonParser customizeParser)

public Object parse(Type dataType, boolean close, CustomizeJsonParser customizeParser)

Beta
Parse a JSON object, array, or value into a new instance of the given destination class, optionally using the given parser customizer.

If it parses an object, after this method ends, the current token will be the object's ending JsonToken#END_OBJECT. If it parses an array, after this method ends, the current token will be the array's ending JsonToken#END_ARRAY.

Parameters
NameDescription
dataTypeType

Type into which the JSON should be parsed

closeboolean

true if #close() should be called after parsing

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Returns
TypeDescription
Object

new instance of the parsed dataType

Exceptions
TypeDescription
IOException

parseAndClose(Object destination)

public final void parseAndClose(Object destination)

Parse a JSON Object from the given JSON parser -- which is closed after parsing completes -- into the given destination object.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name.

Parameter
NameDescription
destinationObject

destination object

Exceptions
TypeDescription
IOException

parseAndClose(Object destination, CustomizeJsonParser customizeParser)

public final void parseAndClose(Object destination, CustomizeJsonParser customizeParser)

Beta
Parse a JSON Object from the given JSON parser -- which is closed after parsing completes -- into the given destination object, optionally using the given parser customizer.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name.

Parameters
NameDescription
destinationObject

destination object

customizeParserCustomizeJsonParser

optional parser customizer or null for none

Exceptions
TypeDescription
IOException

skipChildren()

public abstract JsonParser skipChildren()

Skips to the matching JsonToken#END_ARRAY if current token is JsonToken#START_ARRAY, the matching JsonToken#END_OBJECT if the current token is JsonToken#START_OBJECT, else does nothing.

Returns
TypeDescription
JsonParser
Exceptions
TypeDescription
IOException

skipToKey(String keyToFind)

public final void skipToKey(String keyToFind)

Skips the values of all keys in the current object until it finds the given key.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name. After this method ends, the current token will either be the JsonToken#END_OBJECT of the current object if the key is not found, or the value of the key that was found.

Parameter
NameDescription
keyToFindString

key to find

Exceptions
TypeDescription
IOException

skipToKey(Set<String> keysToFind)

public final String skipToKey(Set<String> keysToFind)

Skips the values of all keys in the current object until it finds one of the given keys.

Before this method is called, the parser must either point to the start or end of a JSON object or to a field name. After this method ends, the current token will either be the JsonToken#END_OBJECT of the current object if no matching key is found, or the value of the key that was found.

Parameter
NameDescription
keysToFindSet<String>

set of keys to look for

Returns
TypeDescription
String

name of the first matching key found or null if no match was found

Exceptions
TypeDescription
IOException