Class JsonReader
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
DefaultJsonReader,GsonJsonReader
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract JsonReaderReads and returns the current JSON structure theJsonReaderis pointing to.abstract JsonTokenGets theJsonTokenthat the reader currently points.abstract byte[]Gets the binary value if the reader is currently pointing to aJsonToken.STRINGtoken.final BooleanGets theBooleanvalue if the reader is currently pointing to aJsonToken.BOOLEANtoken or null if the reader is currently pointing to aJsonToken.NULL.abstract booleanGets the boolean value if the reader is currently pointing to aJsonToken.BOOLEANtoken.final DoubleGets theDoublevalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.abstract doubleGets the double value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.abstract StringGets the field name if the reader is currently pointing to aJsonToken.FIELD_NAME.final FloatGets theFloatvalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.abstract floatGets the float value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.final IntegerGets theIntegervalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.abstract intGets the int value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.final LongGets theLongvalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.abstract longGets the long value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.final <T> TgetNullableValue(Function<JsonReader, T> nonNullGetter) Convenience method to read a nullable type.abstract StringGets the String value if the reader is currently pointing to aJsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER, orJsonToken.STRING.final StringGets the text value for thecurrentToken().final booleanfinal booleanabstract JsonTokenIterates to and returns the nextJsonTokenin the JSON encoded value.final <T> List<T>readArray(Function<JsonReader, T> elementReaderFunc) Reads a JSON array.final StringRecursively reads the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECT.final voidreadChildren(StringBuilder buffer) Recursively reads the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECTinto the passedStringBuilder.readMap(Function<JsonReader, T> valueReaderFunc) Reads a JSON map.final <T> TreadObject(Function<JsonReader, T> objectReaderFunc) Reads a JSON object.final ObjectReads an untyped object.abstract JsonReaderreset()Creates a newJsonReaderreset to the beginning of the JSON stream.abstract booleanIndicates whether theJsonReadersupportsresetting.abstract voidRecursively skips the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECT.
-
Constructor Details
-
JsonReader
public JsonReader()
-
-
Method Details
-
currentToken
Gets theJsonTokenthat the reader currently points.Returns null if the reader isn't pointing to a token. This happens if the reader hasn't begun to read the JSON value or if reading of the JSON value has completed.
- Returns:
- The
JsonTokenthat the reader currently points, or null if the reader isn't pointing to a token.
-
nextToken
Iterates to and returns the nextJsonTokenin the JSON encoded value.Returns null if iterating to the next token completes reading of the JSON encoded value.
- Returns:
- The next
JsonTokenin the JSON encoded value, or null if reading completes.
-
isStartArrayOrObject
public final boolean isStartArrayOrObject()- Returns:
- Whether the
currentToken()isJsonToken.START_ARRAYorJsonToken.START_OBJECT.
-
isEndArrayOrObject
public final boolean isEndArrayOrObject()- Returns:
- Whether the
currentToken()isJsonToken.END_ARRAYorJsonToken.END_OBJECT.
-
getBinaryValue
public abstract byte[] getBinaryValue()Gets the binary value if the reader is currently pointing to aJsonToken.STRINGtoken.This returns the equivalent of
Base64.getDecoder()Base64.Decoder.decode(String).If the reader is pointing to a
JsonToken.NULLnull will be returned. If the reader is pointing to any other token type anIllegalStateExceptionwill be thrown.- Returns:
- The binary value based on whether the current token is
JsonToken.STRINGorJsonToken.NULL. - Throws:
IllegalStateException- If the reader isn't pointing to eitherJsonToken.STRINGorJsonToken.NULL.
-
getBooleanValue
public abstract boolean getBooleanValue()Gets the boolean value if the reader is currently pointing to aJsonToken.BOOLEANtoken.If the reader is pointing to any other token type an
IllegalStateExceptionwill be thrown.If
Booleanshould be read usegetBooleanNullableValue().- Returns:
- The boolean value based on the
JsonToken.BOOLEAN. - Throws:
IllegalStateException- If the reader isn't pointing toJsonToken.BOOLEAN.
-
getBooleanNullableValue
Gets theBooleanvalue if the reader is currently pointing to aJsonToken.BOOLEANtoken or null if the reader is currently pointing to aJsonToken.NULL.If the reader is pointing to any other token type an
IllegalStateExceptionwill be thrown.If boolean should be read use
getBooleanValue().- Returns:
- The
Booleanvalue based on the current token. - Throws:
IllegalStateException- If the reader isn't pointing toJsonToken.BOOLEANorJsonToken.NULL.
-
getFloatValue
public abstract float getFloatValue()Gets the float value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a float.All other
JsonTokentypes will throw anIllegalStateException.If
Floatshould be read usegetFloatNullableValue().- Returns:
- The float value based on the current token.
- Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a float.IllegalStateException- If the current token isn't aJsonToken.NUMBERorJsonToken.STRING.
-
getFloatNullableValue
Gets theFloatvalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a float.All other
JsonTokentypes will throw anIllegalStateException.If float should be read use
getFloatValue().- Returns:
- The
Floatvalue based on the current token. - Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a float.IllegalStateException- If the current token isn't aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.
-
getDoubleValue
public abstract double getDoubleValue()Gets the double value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a double.All other
JsonTokentypes will throw anIllegalStateException.If
Doubleshould be read usegetDoubleNullableValue().- Returns:
- The double value based on the current token.
- Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a double.IllegalStateException- If the current token isn't aJsonToken.NUMBERorJsonToken.STRING.
-
getDoubleNullableValue
Gets theDoublevalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a double.All other
JsonTokentypes will throw anIllegalStateException.If double should be read use
getDoubleValue().- Returns:
- The
Doublevalue based on the current token. - Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a double.IllegalStateException- If the current token isn't aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.
-
getIntValue
public abstract int getIntValue()Gets the int value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to an int.All other
JsonTokentypes will throw anIllegalStateException.If
Integershould be read usegetIntegerNullableValue()- Returns:
- The int value based on the current token.
- Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to an int.IllegalStateException- If the current token isn't aJsonToken.NUMBERorJsonToken.STRING.
-
getIntegerNullableValue
Gets theIntegervalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to an int.All other
JsonTokentypes will throw anIllegalStateException.If int should be read use
getIntValue()- Returns:
- The
Integervalue based on the current token. - Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to an int.IllegalStateException- If the current token isn't aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.
-
getLongValue
public abstract long getLongValue()Gets the long value if the reader is currently pointing to aJsonToken.NUMBERorJsonToken.STRING.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a long.All other
JsonTokentypes will throw anIllegalStateException.If
Longshould be read usegetLongNullableValue().- Returns:
- The long value based on the current token.
- Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a long.IllegalStateException- If the current token isn't aJsonToken.NUMBERorJsonToken.STRING.
-
getLongNullableValue
Gets theLongvalue if the reader is currently pointing to aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.JsonToken.STRINGwill throw aNumberFormatExceptionif the underlying string value cannot be converted to a long.All other
JsonTokentypes will throw anIllegalStateException.If long should be read use
getLongValue().- Returns:
- The
Longvalue based on the current token. - Throws:
NumberFormatException- If the current token is aJsonToken.STRINGand cannot be converted to a long.IllegalStateException- If the current token isn't aJsonToken.NUMBER,JsonToken.STRING, orJsonToken.NULL.
-
getStringValue
Gets the String value if the reader is currently pointing to aJsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER, orJsonToken.STRING.If the current token is a
JsonToken.BOOLEAN, orJsonToken.NUMBERthe String representation of the value will be returned. If the current token isJsonToken.NULLnull will be returned.All other
JsonTokentypes will throw anIllegalStateException.- Returns:
- The String value based on the current token.
- Throws:
IllegalStateException- If the current token isn't aJsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER, orJsonToken.STRING.
-
getFieldName
Gets the field name if the reader is currently pointing to aJsonToken.FIELD_NAME.All other
JsonTokentypes will throw anIllegalStateException.- Returns:
- The field name based on the current token.
- Throws:
IllegalStateException- If the current token isn't aJsonToken.FIELD_NAME.
-
getNullableValue
Convenience method to read a nullable type.If the
currentToken()isJsonToken.NULLnull will be returned, otherwise thisJsonReaderwill be passed into thenonNullGetterfunction to get the value. Effectively, this is the generic form of the get*NullableValue methods.- Type Parameters:
T- Type returned by the function.- Parameters:
nonNullGetter- Function that reads the non-null JSON value.- Returns:
- null if the
currentToken()isJsonToken.NULLor the value returned bynonNullGetter.
-
skipChildren
public abstract void skipChildren()Recursively skips the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECT.If the current token isn't the beginning of an array or object this method is a no-op.
-
bufferObject
Reads and returns the current JSON structure theJsonReaderis pointing to. This will mutate the current location of thisJsonReader.If the
currentToken()isn'tJsonToken.START_OBJECTorJsonToken.FIELD_NAMEanIllegalStateExceptionwill be thrown.The returned
JsonReaderis able to bereset()to replay the underlying JSON stream.- Returns:
- The buffered JSON object the
JsonReaderwas pointing to. - Throws:
IllegalStateException- If thecurrentToken()isn'tJsonToken.START_OBJECTorJsonToken.FIELD_NAME.
-
resetSupported
public abstract boolean resetSupported()Indicates whether theJsonReadersupportsresetting.- Returns:
- Whether
reset()is supported.
-
reset
Creates a newJsonReaderreset to the beginning of the JSON stream.Use
resetSupported()to determine whether theJsonReadercan be reset. If resetting is called and it isn't supported anIllegalStateExceptionwill be thrown.- Returns:
- A new
JsonReaderreset to the beginning of the JSON stream. - Throws:
IllegalStateException- If resetting isn't supported by the current JsonReader.
-
readChildren
Recursively reads the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECT.If the current token isn't the beginning of an array or object this method is a no-op.
- Returns:
- The raw textual value of the JSON token sub-stream.
-
readChildren
Recursively reads the JSON token sub-stream if the current token is eitherJsonToken.START_ARRAYorJsonToken.START_OBJECTinto the passedStringBuilder.If the current token isn't the beginning of an array or object this method is a no-op.
- Parameters:
buffer- TheStringBuilderwhere the read sub-stream will be written.
-
readObject
Reads a JSON object.If the
currentToken()is null this willread the next token. If the starting token is still null orJsonToken.NULLnull will be returned. If the token is anything other thanJsonToken.START_OBJECTanIllegalStateExceptionwill be thrown.Once the JSON stream is prepared for object reading this will get the next token and pass this
JsonReaderinto theobjectReaderFuncto handle reading the object.If a JSON array should be read use
readArray(Function)or if a JSON map should be read usereadMap(Function).- Type Parameters:
T- The value element type.- Parameters:
objectReaderFunc- Function that reads each value of the key-value pair.- Returns:
- The read JSON map, or null if the
JsonTokenis null orJsonToken.NULL. - Throws:
IllegalStateException- If the token isn'tJsonToken.START_OBJECT,JsonToken.NULL, or null.
-
readArray
Reads a JSON array.If the
currentToken()is null this willread the next token. If the starting token is still null orJsonToken.NULLnull will be returned. If the token is anything other thanJsonToken.START_ARRAYanIllegalStateExceptionwill be thrown.Once the JSON stream is prepared for element reading this will get the element token and pass this
JsonReaderinto theelementReaderFuncto handle reading the element of the array. If the array has no elements an empty list will be returned.If a JSON object should be read use
readObject(Function)or if a JSON map should be read usereadMap(Function).- Type Parameters:
T- The array element type.- Parameters:
elementReaderFunc- Function that reads each element of the array.- Returns:
- The read JSON array, or null if the
JsonTokenis null orJsonToken.NULL. - Throws:
IllegalStateException- If the token isn'tJsonToken.START_ARRAY,JsonToken.NULL, or null.
-
readMap
Reads a JSON map.If the
currentToken()is null this willread the next token. If the starting token is still null orJsonToken.NULLnull will be returned. If the token is anything other thanJsonToken.START_OBJECTanIllegalStateExceptionwill be thrown.Once the JSON stream is prepared for key-value reading this will get the next token and read the field name as the key then get the next token after that and pass this
JsonReaderinto thevalueReaderFuncto handle reading the value of the key-value pair. If the object has no elements an empty map will be returned.If a JSON object should be read use
readObject(Function)or if a JSON array should be read usereadArray(Function).- Type Parameters:
T- The value element type.- Parameters:
valueReaderFunc- Function that reads each value of the key-value pair.- Returns:
- The read JSON map, or null if the
JsonTokenis null orJsonToken.NULL. - Throws:
IllegalStateException- If the token isn'tJsonToken.START_OBJECT,JsonToken.NULL, or null.
-
readUntyped
Reads an untyped object.If the
currentToken()is null this willread the next token.If the starting token is
JsonToken.END_ARRAY,JsonToken.END_OBJECT, orJsonToken.FIELD_NAMEanIllegalStateExceptionwill be thrown as these are invalid starting points for reading an unknown type. If the untyped object is deeply nested anIllegalStateExceptionwill also be thrown to prevent a stack overflow exception.The returned object will be one of the following:
- null if the starting token is null or
JsonToken.NULL - true or false if the starting token is
JsonToken.BOOLEAN - One of int, long, float, or double is the starting token is
JsonToken.NUMBER, the smallest containing value will be used if the number is an integer - An array of untyped elements if the starting point is
JsonToken.START_ARRAY - A map of String-untyped value if the starting point is
JsonToken.START_OBJECT
- Returns:
- The untyped value based on the outlined return types above.
- Throws:
IllegalStateException- If the starting point of the object isJsonToken.END_ARRAY,JsonToken.END_OBJECT, orJsonToken.FIELD_NAMEor if the untyped object is deeply nested.
- null if the starting token is null or
-
getTextValue
Gets the text value for thecurrentToken().The following is how each
JsonTokentype is handled:JsonToken.START_OBJECT-> {JsonToken.END_OBJECT-> }JsonToken.START_ARRAY-> [JsonToken.END_ARRAY-> ]JsonToken.FIELD_NAME->getFieldName()JsonToken.BOOLEAN-> String.valueOfgetBooleanValue()JsonToken.NULL-> "null"JsonToken.STRING->getStringValue()JsonToken.NUMBER-> String.valueOfgetStringValue()
IllegalStateExceptionwill be thrown.- Returns:
- The text value for the
currentToken(). - Throws:
IllegalStateException- If the current token is null.
-