Class JsonParser
- java.lang.Object
-
- com.google.api.client.json.JsonParser
-
- Direct Known Subclasses:
GsonParser
,JacksonParser
,JacksonParser
,MockJsonParser
public abstract class JsonParser extends java.lang.Object
Abstract low-level JSON parser. See https://code.google.com/p/google-http-java-client/wiki/JSONImplementation 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
ArrayMap
is used by default for the parsed values.- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.WeakHashMap<java.lang.Class<?>,java.lang.reflect.Field>
cachedTypemapFields
Maps a polymorphicClass
to itsField
with theJsonPolymorphicTypeMap
annotation, ornull
if there is no field with that annotation.private static java.util.concurrent.locks.Lock
lock
Lock on thecachedTypemapFields
.
-
Constructor Summary
Constructors Constructor Description JsonParser()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
close()
Closes the parser and the underlying input stream or reader, and releases any memory associated with it.abstract java.math.BigInteger
getBigIntegerValue()
Returns theBigInteger
value of the current token.abstract byte
getByteValue()
Returns the byte value of the current token.private static java.lang.reflect.Field
getCachedTypemapFieldFor(java.lang.Class<?> key)
Finds theField
on the givenClass
that has theJsonPolymorphicTypeMap
annotation, ornull
if there is none.abstract java.lang.String
getCurrentName()
Returns the most recent field name ornull
for array values or for root-level values.abstract JsonToken
getCurrentToken()
Returns the token the parser currently points to ornull
for none (at start of input or after end of input).abstract java.math.BigDecimal
getDecimalValue()
Returns theBigDecimal
value of the current token.abstract double
getDoubleValue()
Returns the double value of the current token.abstract JsonFactory
getFactory()
Returns the JSON factory from which this generator was created.abstract float
getFloatValue()
Returns the float value of the current token.abstract int
getIntValue()
Returns the int value of the current token.abstract long
getLongValue()
Returns the long value of the current token.abstract short
getShortValue()
Returns the short value of the current token.abstract java.lang.String
getText()
abstract JsonToken
nextToken()
Returns the next token from the stream ornull
to indicate end of input.<T> T
parse(java.lang.Class<T> destinationClass)
Parse a JSON object, array, or value into a new instance of the given destination class.<T> T
parse(java.lang.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.void
parse(java.lang.Object destination)
Parse a JSON object from the given JSON parser into the given destination object.void
parse(java.lang.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.java.lang.Object
parse(java.lang.reflect.Type dataType, boolean close)
Parse a JSON object, array, or value into a new instance of the given destination class.java.lang.Object
parse(java.lang.reflect.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.private void
parse(java.util.ArrayList<java.lang.reflect.Type> context, java.lang.Object destination, CustomizeJsonParser customizeParser)
Parses the next field from the given JSON parser into the given destination object.<T> T
parseAndClose(java.lang.Class<T> destinationClass)
Parse a JSON object, array, or value into a new instance of the given destination class, and then closes the parser.<T> T
parseAndClose(java.lang.Class<T> destinationClass, CustomizeJsonParser customizeParser)
Beta
Parse a JSON object, array, or value into a new instance of the given destination class usingparse(Class, CustomizeJsonParser)
, and then closes the parser.void
parseAndClose(java.lang.Object destination)
Parse a JSON Object from the given JSON parser -- which is closed after parsing completes -- into the given destination object.void
parseAndClose(java.lang.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.<T> java.util.Collection<T>
parseArray(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass)
Parse a JSON Array from the given JSON parser into the given destination collection.<T> java.util.Collection<T>
parseArray(java.lang.Class<?> destinationCollectionClass, java.lang.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.private <T> void
parseArray(java.lang.reflect.Field fieldContext, java.util.Collection<T> destinationCollection, java.lang.reflect.Type destinationItemType, java.util.ArrayList<java.lang.reflect.Type> context, CustomizeJsonParser customizeParser)
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.<T> void
parseArray(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass)
Parse a JSON Array from the given JSON parser into the given destination collection.<T> void
parseArray(java.util.Collection<? super T> destinationCollection, java.lang.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.<T> java.util.Collection<T>
parseArrayAndClose(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass)
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.<T> java.util.Collection<T>
parseArrayAndClose(java.lang.Class<?> destinationCollectionClass, java.lang.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.<T> void
parseArrayAndClose(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass)
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.<T> void
parseArrayAndClose(java.util.Collection<? super T> destinationCollection, java.lang.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.private void
parseMap(java.lang.reflect.Field fieldContext, java.util.Map<java.lang.String,java.lang.Object> destinationMap, java.lang.reflect.Type valueType, java.util.ArrayList<java.lang.reflect.Type> context, CustomizeJsonParser customizeParser)
Parse a JSON Object from the given JSON parser into the given destination map, optionally using the given parser customizer.private java.lang.Object
parseValue(java.lang.reflect.Field fieldContext, java.lang.reflect.Type valueType, java.util.ArrayList<java.lang.reflect.Type> context, java.lang.Object destination, CustomizeJsonParser customizeParser, boolean handlePolymorphic)
Parse a value.abstract JsonParser
skipChildren()
Skips to the matchingJsonToken.END_ARRAY
if current token isJsonToken.START_ARRAY
, the matchingJsonToken.END_OBJECT
if the current token isJsonToken.START_OBJECT
, else does nothing.void
skipToKey(java.lang.String keyToFind)
Skips the values of all keys in the current object until it finds the given key.java.lang.String
skipToKey(java.util.Set<java.lang.String> keysToFind)
Skips the values of all keys in the current object until it finds one of the given keys.private JsonToken
startParsing()
Starts parsing that handles start of input by callingnextToken()
.private JsonToken
startParsingObjectOrArray()
Starts parsing an object or array by making sure the parser points to an object field name, first array value or end of object or array.
-
-
-
Field Detail
-
cachedTypemapFields
private static java.util.WeakHashMap<java.lang.Class<?>,java.lang.reflect.Field> cachedTypemapFields
Maps a polymorphicClass
to itsField
with theJsonPolymorphicTypeMap
annotation, ornull
if there is no field with that annotation.
-
lock
private static final java.util.concurrent.locks.Lock lock
Lock on thecachedTypemapFields
.
-
-
Method Detail
-
getFactory
public abstract JsonFactory getFactory()
Returns the JSON factory from which this generator was created.
-
close
public abstract void close() throws java.io.IOException
Closes the parser and the underlying input stream or reader, and releases any memory associated with it.- Throws:
java.io.IOException
-
nextToken
public abstract JsonToken nextToken() throws java.io.IOException
Returns the next token from the stream ornull
to indicate end of input.- Throws:
java.io.IOException
-
getCurrentToken
public abstract JsonToken getCurrentToken()
Returns the token the parser currently points to ornull
for none (at start of input or after end of input).
-
getCurrentName
public abstract java.lang.String getCurrentName() throws java.io.IOException
Returns the most recent field name ornull
for array values or for root-level values.- Throws:
java.io.IOException
-
skipChildren
public abstract JsonParser skipChildren() throws java.io.IOException
Skips to the matchingJsonToken.END_ARRAY
if current token isJsonToken.START_ARRAY
, the matchingJsonToken.END_OBJECT
if the current token isJsonToken.START_OBJECT
, else does nothing.- Throws:
java.io.IOException
-
getText
public abstract java.lang.String getText() throws java.io.IOException
- Throws:
java.io.IOException
-
getByteValue
public abstract byte getByteValue() throws java.io.IOException
Returns the byte value of the current token.- Throws:
java.io.IOException
-
getShortValue
public abstract short getShortValue() throws java.io.IOException
Returns the short value of the current token.- Throws:
java.io.IOException
-
getIntValue
public abstract int getIntValue() throws java.io.IOException
Returns the int value of the current token.- Throws:
java.io.IOException
-
getFloatValue
public abstract float getFloatValue() throws java.io.IOException
Returns the float value of the current token.- Throws:
java.io.IOException
-
getLongValue
public abstract long getLongValue() throws java.io.IOException
Returns the long value of the current token.- Throws:
java.io.IOException
-
getDoubleValue
public abstract double getDoubleValue() throws java.io.IOException
Returns the double value of the current token.- Throws:
java.io.IOException
-
getBigIntegerValue
public abstract java.math.BigInteger getBigIntegerValue() throws java.io.IOException
Returns theBigInteger
value of the current token.- Throws:
java.io.IOException
-
getDecimalValue
public abstract java.math.BigDecimal getDecimalValue() throws java.io.IOException
Returns theBigDecimal
value of the current token.- Throws:
java.io.IOException
-
parseAndClose
public final <T> T parseAndClose(java.lang.Class<T> destinationClass) throws java.io.IOException
Parse a JSON object, array, or value into a new instance of the given destination class, and then closes the parser.- Type Parameters:
T
- destination class- Parameters:
destinationClass
- destination class that has a public default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
java.io.IOException
- Since:
- 1.15
-
parseAndClose
@Beta public final <T> T parseAndClose(java.lang.Class<T> destinationClass, CustomizeJsonParser customizeParser) throws java.io.IOException
Beta
Parse a JSON object, array, or value into a new instance of the given destination class usingparse(Class, CustomizeJsonParser)
, and then closes the parser.- Type Parameters:
T
- destination class- Parameters:
destinationClass
- destination class that has a public default constructor to use to create a new instancecustomizeParser
- optional parser customizer ornull
for none- Returns:
- new instance of the parsed destination class
- Throws:
java.io.IOException
-
skipToKey
public final void skipToKey(java.lang.String keyToFind) throws java.io.IOException
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.- Parameters:
keyToFind
- key to find- Throws:
java.io.IOException
-
skipToKey
public final java.lang.String skipToKey(java.util.Set<java.lang.String> keysToFind) throws java.io.IOException
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.- Parameters:
keysToFind
- set of keys to look for- Returns:
- name of the first matching key found or
null
if no match was found - Throws:
java.io.IOException
- Since:
- 1.10
-
startParsing
private JsonToken startParsing() throws java.io.IOException
Starts parsing that handles start of input by callingnextToken()
.- Throws:
java.io.IOException
-
startParsingObjectOrArray
private JsonToken startParsingObjectOrArray() throws java.io.IOException
Starts parsing an object or array by making sure the parser points to an object field name, first array value or end of object or array.If the parser is at the start of input,
nextToken()
is called. The current token must then beJsonToken.START_OBJECT
,JsonToken.END_OBJECT
,JsonToken.START_ARRAY
,JsonToken.END_ARRAY
, orJsonToken.FIELD_NAME
. For an object only, after the method is called, the current token must be eitherJsonToken.FIELD_NAME
orJsonToken.END_OBJECT
.- Throws:
java.io.IOException
-
parseAndClose
public final void parseAndClose(java.lang.Object destination) throws java.io.IOException
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.
- Parameters:
destination
- destination object- Throws:
java.io.IOException
- Since:
- 1.15
-
parseAndClose
@Beta public final void parseAndClose(java.lang.Object destination, CustomizeJsonParser customizeParser) throws java.io.IOException
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:
destination
- destination objectcustomizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parse
public final <T> T parse(java.lang.Class<T> destinationClass) throws java.io.IOException
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 endingJsonToken.END_ARRAY
.- Type Parameters:
T
- destination class- Parameters:
destinationClass
- destination class that has a public default constructor to use to create a new instance- Returns:
- new instance of the parsed destination class
- Throws:
java.io.IOException
- Since:
- 1.15
-
parse
@Beta public final <T> T parse(java.lang.Class<T> destinationClass, CustomizeJsonParser customizeParser) throws java.io.IOException
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 endingJsonToken.END_ARRAY
.- Type Parameters:
T
- destination class- Parameters:
destinationClass
- destination class that has a public default constructor to use to create a new instancecustomizeParser
- optional parser customizer ornull
for none- Returns:
- new instance of the parsed destination class
- Throws:
java.io.IOException
-
parse
public java.lang.Object parse(java.lang.reflect.Type dataType, boolean close) throws java.io.IOException
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 endingJsonToken.END_ARRAY
.- Parameters:
dataType
- Type into which the JSON should be parsedclose
-true
ifclose()
should be called after parsing- Returns:
- new instance of the parsed dataType
- Throws:
java.io.IOException
- Since:
- 1.15
-
parse
@Beta public java.lang.Object parse(java.lang.reflect.Type dataType, boolean close, CustomizeJsonParser customizeParser) throws java.io.IOException
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 endingJsonToken.END_ARRAY
.- Parameters:
dataType
- Type into which the JSON should be parsedclose
-true
ifclose()
should be called after parsingcustomizeParser
- optional parser customizer ornull
for none- Returns:
- new instance of the parsed dataType
- Throws:
java.io.IOException
- Since:
- 1.10
-
parse
public final void parse(java.lang.Object destination) throws java.io.IOException
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.- Parameters:
destination
- destination object- Throws:
java.io.IOException
- Since:
- 1.15
-
parse
@Beta public final void parse(java.lang.Object destination, CustomizeJsonParser customizeParser) throws java.io.IOException
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:
destination
- destination objectcustomizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parse
private void parse(java.util.ArrayList<java.lang.reflect.Type> context, java.lang.Object destination, CustomizeJsonParser customizeParser) throws java.io.IOException
Parses the next field from the given JSON parser into the given destination object.- Parameters:
context
- destination context stack (possibly empty)destination
- destination object instance ornull
for none (for example empty context stack)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseArrayAndClose
public final <T> java.util.Collection<T> parseArrayAndClose(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass) throws java.io.IOException
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.- Parameters:
destinationCollectionClass
- class of destination collection (must have a public default constructor)destinationItemClass
- class of destination collection item (must have a public default constructor)- Throws:
java.io.IOException
- Since:
- 1.15
-
parseArrayAndClose
@Beta public final <T> java.util.Collection<T> parseArrayAndClose(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws java.io.IOException
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:
destinationCollectionClass
- class of destination collection (must have a public default constructor)destinationItemClass
- class of destination collection item (must have a public default constructor)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseArrayAndClose
public final <T> void parseArrayAndClose(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass) throws java.io.IOException
Parse a JSON Array from the given JSON parser (which is closed after parsing completes) into the given destination collection.- Parameters:
destinationCollection
- destination collectiondestinationItemClass
- class of destination collection item (must have a public default constructor)- Throws:
java.io.IOException
- Since:
- 1.15
-
parseArrayAndClose
@Beta public final <T> void parseArrayAndClose(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws java.io.IOException
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:
destinationCollection
- destination collectiondestinationItemClass
- class of destination collection item (must have a public default constructor)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseArray
public final <T> java.util.Collection<T> parseArray(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass) throws java.io.IOException
Parse a JSON Array from the given JSON parser into the given destination collection.- Parameters:
destinationCollectionClass
- class of destination collection (must have a public default constructor)destinationItemClass
- class of destination collection item (must have a public default constructor)- Throws:
java.io.IOException
- Since:
- 1.15
-
parseArray
@Beta public final <T> java.util.Collection<T> parseArray(java.lang.Class<?> destinationCollectionClass, java.lang.Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws java.io.IOException
Beta
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.- Parameters:
destinationCollectionClass
- class of destination collection (must have a public default constructor)destinationItemClass
- class of destination collection item (must have a public default constructor)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseArray
public final <T> void parseArray(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass) throws java.io.IOException
Parse a JSON Array from the given JSON parser into the given destination collection.- Parameters:
destinationCollection
- destination collectiondestinationItemClass
- class of destination collection item (must have a public default constructor)- Throws:
java.io.IOException
- Since:
- 1.15
-
parseArray
@Beta public final <T> void parseArray(java.util.Collection<? super T> destinationCollection, java.lang.Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws java.io.IOException
Beta
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.- Parameters:
destinationCollection
- destination collectiondestinationItemClass
- class of destination collection item (must have a public default constructor)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseArray
private <T> void parseArray(java.lang.reflect.Field fieldContext, java.util.Collection<T> destinationCollection, java.lang.reflect.Type destinationItemType, java.util.ArrayList<java.lang.reflect.Type> context, CustomizeJsonParser customizeParser) throws java.io.IOException
Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.- Parameters:
fieldContext
- field context ornull
for nonedestinationCollection
- destination collectiondestinationItemType
- type of destination collection itemcontext
- destination context stack (possibly empty)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseMap
private void parseMap(java.lang.reflect.Field fieldContext, java.util.Map<java.lang.String,java.lang.Object> destinationMap, java.lang.reflect.Type valueType, java.util.ArrayList<java.lang.reflect.Type> context, CustomizeJsonParser customizeParser) throws java.io.IOException
Parse a JSON Object from the given JSON parser into the given destination map, optionally using the given parser customizer.- Parameters:
fieldContext
- field context ornull
for nonedestinationMap
- destination mapvalueType
- valueType of the map value type parametercontext
- destination context stack (possibly empty)customizeParser
- optional parser customizer ornull
for none- Throws:
java.io.IOException
-
parseValue
private final java.lang.Object parseValue(java.lang.reflect.Field fieldContext, java.lang.reflect.Type valueType, java.util.ArrayList<java.lang.reflect.Type> context, java.lang.Object destination, CustomizeJsonParser customizeParser, boolean handlePolymorphic) throws java.io.IOException
Parse a value.- Parameters:
fieldContext
- field context ornull
for none (for example into a map)valueType
- value type ornull
if not known (for example into a map)context
- destination context stack (possibly empty)destination
- destination object instance ornull
for none (for example empty context stack)customizeParser
- customize parser ornull
for nonehandlePolymorphic
- whether or not to check for polymorphic schema- Returns:
- parsed value
- Throws:
java.io.IOException
-
getCachedTypemapFieldFor
private static java.lang.reflect.Field getCachedTypemapFieldFor(java.lang.Class<?> key)
Finds theField
on the givenClass
that has theJsonPolymorphicTypeMap
annotation, ornull
if there is none.The class must contain exactly zero or one
JsonPolymorphicTypeMap
annotation.- Parameters:
key
- TheClass
to search in, ornull
- Returns:
- The
Field
with theJsonPolymorphicTypeMap
annotation, ornull
either if there is none or if the key isnull
-
-