Class JsonParser
- All Implemented Interfaces:
Closeable, AutoCloseable
- Direct Known Subclasses:
GsonParser, JacksonParser, MockJsonParser
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
invalid reference
java.util.ArrayMap
- Since:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static WeakHashMap<Class<?>, Field> Maps a polymorphicClassto itsFieldwith theJsonPolymorphicTypeMapannotation, ornullif there is no field with that annotation.private static final LockLock on thecachedTypemapFields. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidclose()Closes the parser and the underlying input stream or reader, and releases any memory associated with it.abstract BigIntegerReturns theBigIntegervalue of the current token.abstract byteReturns the byte value of the current token.private static FieldgetCachedTypemapFieldFor(Class<?> key) Finds theFieldon the givenClassthat has theJsonPolymorphicTypeMapannotation, ornullif there is none.abstract StringReturns the most recent field name ornullfor array values or for root-level values.abstract JsonTokenReturns the token the parser currently points to ornullfor none (at start of input or after end of input).abstract BigDecimalReturns theBigDecimalvalue of the current token.abstract doubleReturns the double value of the current token.abstract JsonFactoryReturns the JSON factory from which this generator was created.abstract floatReturns the float value of the current token.abstract intReturns the int value of the current token.abstract longReturns the long value of the current token.abstract shortReturns the short value of the current token.abstract StringgetText()abstract JsonTokenReturns the next token from the stream ornullto indicate end of input.final <T> TParse a JSON object, array, or value into a new instance of the given destination class.final <T> Tparse(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.final voidParse a JSON object from the given JSON parser into the given destination object.final voidparse(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.Parse a JSON object, array, or value into a new instance of the given destination class.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.private voidparse(ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser) Parses the next field from the given JSON parser into the given destination object.final <T> TparseAndClose(Class<T> destinationClass) Parse a JSON object, array, or value into a new instance of the given destination class, and then closes the parser.final <T> TparseAndClose(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.final voidparseAndClose(Object destination) Parse a JSON Object from the given JSON parser -- which is closed after parsing completes -- into the given destination object.final voidparseAndClose(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.final <T> Collection<T> parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass) Parse a JSON Array from the given JSON parser into the given destination collection.final <T> Collection<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.private <T> voidparseArray(Field fieldContext, Collection<T> destinationCollection, Type destinationItemType, ArrayList<Type> context, CustomizeJsonParser customizeParser) Parse a JSON Array from the given JSON parser into the given destination collection, optionally using the given parser customizer.final <T> voidparseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass) Parse a JSON Array from the given JSON parser into the given destination collection.final <T> voidparseArray(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.final <T> Collection<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.final <T> Collection<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.final <T> voidparseArrayAndClose(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.final <T> voidparseArrayAndClose(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.private voidparseMap(Field fieldContext, Map<String, Object> destinationMap, Type valueType, ArrayList<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 final ObjectparseValue(Field fieldContext, Type valueType, ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser, boolean handlePolymorphic) Parse a value.abstract JsonParserSkips to the matchingJsonToken.END_ARRAYif current token isJsonToken.START_ARRAY, the matchingJsonToken.END_OBJECTif the current token isJsonToken.START_OBJECT, else does nothing.final voidSkips the values of all keys in the current object until it finds the given key.final StringSkips the values of all keys in the current object until it finds one of the given keys.private JsonTokenStarts parsing that handles start of input by callingnextToken().private JsonTokenStarts 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 Details
-
cachedTypemapFields
Maps a polymorphicClassto itsFieldwith theJsonPolymorphicTypeMapannotation, ornullif there is no field with that annotation. -
lock
Lock on thecachedTypemapFields.
-
-
Constructor Details
-
JsonParser
public JsonParser()
-
-
Method Details
-
getFactory
Returns the JSON factory from which this generator was created. -
close
Closes the parser and the underlying input stream or reader, and releases any memory associated with it.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
nextToken
Returns the next token from the stream ornullto indicate end of input.- Throws:
IOException
-
getCurrentToken
Returns the token the parser currently points to ornullfor none (at start of input or after end of input). -
getCurrentName
Returns the most recent field name ornullfor array values or for root-level values.- Throws:
IOException
-
skipChildren
Skips to the matchingJsonToken.END_ARRAYif current token isJsonToken.START_ARRAY, the matchingJsonToken.END_OBJECTif the current token isJsonToken.START_OBJECT, else does nothing.- Throws:
IOException
-
getText
- Throws:
IOException
-
getByteValue
Returns the byte value of the current token.- Throws:
IOException
-
getShortValue
Returns the short value of the current token.- Throws:
IOException
-
getIntValue
Returns the int value of the current token.- Throws:
IOException
-
getFloatValue
Returns the float value of the current token.- Throws:
IOException
-
getLongValue
Returns the long value of the current token.- Throws:
IOException
-
getDoubleValue
Returns the double value of the current token.- Throws:
IOException
-
getBigIntegerValue
Returns theBigIntegervalue of the current token.- Throws:
IOException
-
getDecimalValue
Returns theBigDecimalvalue of the current token.- Throws:
IOException
-
parseAndClose
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:
IOException- Since:
- 1.15
-
parseAndClose
@Beta public final <T> T parseAndClose(Class<T> destinationClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Returns:
- new instance of the parsed destination class
- Throws:
IOException
-
skipToKey
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_OBJECTof the current object if the key is not found, or the value of the key that was found.- Parameters:
keyToFind- key to find- Throws:
IOException
-
skipToKey
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_OBJECTof 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
nullif no match was found - Throws:
IOException- Since:
- 1.10
-
startParsing
Starts parsing that handles start of input by callingnextToken().- Throws:
IOException
-
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.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_NAMEorJsonToken.END_OBJECT.- Throws:
IOException
-
parseAndClose
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:
IOException- Since:
- 1.15
-
parseAndClose
@Beta public final void parseAndClose(Object destination, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Throws:
IOException
-
parse
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:
IOException- Since:
- 1.15
-
parse
@Beta public final <T> T parse(Class<T> destinationClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Returns:
- new instance of the parsed destination class
- Throws:
IOException
-
parse
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-trueifclose()should be called after parsing- Returns:
- new instance of the parsed dataType
- Throws:
IOException- Since:
- 1.15
-
parse
@Beta public Object parse(Type dataType, boolean close, CustomizeJsonParser customizeParser) throws 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-trueifclose()should be called after parsingcustomizeParser- optional parser customizer ornullfor none- Returns:
- new instance of the parsed dataType
- Throws:
IOException- Since:
- 1.10
-
parse
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_OBJECTof the current object.- Parameters:
destination- destination object- Throws:
IOException- Since:
- 1.15
-
parse
@Beta public final void parse(Object destination, CustomizeJsonParser customizeParser) throws 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_OBJECTof the current object.- Parameters:
destination- destination objectcustomizeParser- optional parser customizer ornullfor none- Throws:
IOException
-
parse
private void parse(ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser) throws 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 ornullfor none (for example empty context stack)customizeParser- optional parser customizer ornullfor none- Throws:
IOException
-
parseArrayAndClose
public final <T> Collection<T> parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass) throws 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:
IOException- Since:
- 1.15
-
parseArrayAndClose
@Beta public final <T> Collection<T> parseArrayAndClose(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Throws:
IOException
-
parseArrayAndClose
public final <T> void parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass) throws 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:
IOException- Since:
- 1.15
-
parseArrayAndClose
@Beta public final <T> void parseArrayAndClose(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Throws:
IOException
-
parseArray
public final <T> Collection<T> parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass) throws 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:
IOException- Since:
- 1.15
-
parseArray
@Beta public final <T> Collection<T> parseArray(Class<?> destinationCollectionClass, Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Throws:
IOException
-
parseArray
public final <T> void parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass) throws 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:
IOException- Since:
- 1.15
-
parseArray
@Beta public final <T> void parseArray(Collection<? super T> destinationCollection, Class<T> destinationItemClass, CustomizeJsonParser customizeParser) throws 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 ornullfor none- Throws:
IOException
-
parseArray
private <T> void parseArray(Field fieldContext, Collection<T> destinationCollection, Type destinationItemType, ArrayList<Type> context, CustomizeJsonParser customizeParser) throws 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 ornullfor nonedestinationCollection- destination collectiondestinationItemType- type of destination collection itemcontext- destination context stack (possibly empty)customizeParser- optional parser customizer ornullfor none- Throws:
IOException
-
parseMap
private void parseMap(Field fieldContext, Map<String, Object> destinationMap, Type valueType, ArrayList<Type> context, CustomizeJsonParser customizeParser) throws IOExceptionParse a JSON Object from the given JSON parser into the given destination map, optionally using the given parser customizer.- Parameters:
fieldContext- field context ornullfor nonedestinationMap- destination mapvalueType- valueType of the map value type parametercontext- destination context stack (possibly empty)customizeParser- optional parser customizer ornullfor none- Throws:
IOException
-
parseValue
private final Object parseValue(Field fieldContext, Type valueType, ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser, boolean handlePolymorphic) throws IOException Parse a value.- Parameters:
fieldContext- field context ornullfor none (for example into a map)valueType- value type ornullif not known (for example into a map)context- destination context stack (possibly empty)destination- destination object instance ornullfor none (for example empty context stack)customizeParser- customize parser ornullfor nonehandlePolymorphic- whether or not to check for polymorphic schema- Returns:
- parsed value
- Throws:
IOException
-
getCachedTypemapFieldFor
Finds theFieldon the givenClassthat has theJsonPolymorphicTypeMapannotation, ornullif there is none.The class must contain exactly zero or one
JsonPolymorphicTypeMapannotation.- Parameters:
key- TheClassto search in, ornull- Returns:
- The
Fieldwith theJsonPolymorphicTypeMapannotation, ornulleither if there is none or if the key isnull
-