JsonReader Class
- java.
lang. Object - com.
azure. json. JsonReader
- com.
Implements
public abstract class JsonReader
implements Closeable
Reads a JSON value as a stream of tokens.
Instances of JsonReader are created using an instance of JsonProvider or using the utility methods in JsonProviders.
Constructor Summary
Constructor | Description |
---|---|
JsonReader() |
Creates an instance of JsonReader. |
Method Summary
Modifier and Type | Method and Description |
---|---|
final T |
getNullable(ReadValueCallback<JsonReader,T> nonNullGetter)
Convenience method to read a nullable type. |
final List<T> |
readArray(ReadValueCallback<JsonReader,T> elementReaderFunc)
Reads a JSON array. |
final Map<String,T> |
readMap(ReadValueCallback<JsonReader,T> valueReaderFunc)
Reads a JSON map. |
final T |
readObject(ReadValueCallback<JsonReader,T> objectReaderFunc)
Reads a JSON object. |
abstract
Json |
bufferObject()
Reads and returns the current JSON object the JsonReader is pointing to. |
abstract void |
close()
Closes the JSON stream. |
abstract
Json |
currentToken()
Gets the JsonToken that the reader currently points. |
abstract byte[] |
getBinary()
Gets the binary value if the reader is currently pointing to a STRING token. |
abstract boolean |
getBoolean()
Gets the boolean value if the reader is currently pointing to a BOOLEAN token. |
abstract double |
getDouble()
Gets the double value if the reader is currently pointing to a NUMBER or STRING. |
abstract String |
getFieldName()
Gets the field name if the reader is currently pointing to a FIELD_NAME. |
abstract float |
getFloat()
Gets the float value if the reader is currently pointing to a NUMBER or STRING. |
abstract int |
getInt()
Gets the int value if the reader is currently pointing to a NUMBER or STRING. |
abstract long |
getLong()
Gets the long value if the reader is currently pointing to a NUMBER or STRING. |
String |
getRawText()
Gets the raw text value for the currentToken(). |
abstract String |
getString()
Gets the String value if the reader is currently pointing to a BOOLEAN, NULL, NUMBER, or STRING. |
final String |
getText()
Gets the text value for the currentToken(). |
final boolean |
isEndArrayOrObject()
Whether the currentToken() is END_ARRAY or END_OBJECT. |
abstract boolean |
isResetSupported()
Indicates whether the JsonReader supports reset(). |
final boolean |
isStartArrayOrObject()
Whether the currentToken() is START_ARRAY or START_OBJECT. |
abstract
Json |
nextToken()
Iterates to and returns the next JsonToken in the JSON encoded value. |
final String |
readChildren()
Recursively reads the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT. |
final void |
readChildren(StringBuilder buffer)
Recursively reads the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT into the passed StringBuilder. |
final String |
readRemainingFieldsAsJsonObject()
Reads the remaining fields in the current JSON object as a JSON object. |
final void |
readRemainingFieldsAsJsonObject(StringBuilder buffer)
Reads the remaining fields in the current JSON object as a JSON object. |
final Object |
readUntyped()
Reads an untyped object. |
abstract
Json |
reset()
Creates a new JsonReader reset to the beginning of the JSON stream. |
abstract void |
skipChildren()
Recursively skips the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT. |
Methods inherited from java.lang.Object
Constructor Details
JsonReader
public JsonReader()
Creates an instance of JsonReader.
Method Details
getNullable
public final T
Convenience method to read a nullable type.
If the currentToken() is NULL null will be returned, otherwise this JsonReader will be passed into the nonNullGetter
function to get the value. Effectively, this is the generic form of the get*NullableValue methods.
Parameters:
Returns:
Throws:
readArray
public final List
Reads a JSON array.
If the currentToken() is null this will nextToken(). If the starting token is still null or NULL null will be returned. If the token is anything other than START_ARRAY an IllegalStateException will be thrown.
Once the JSON stream is prepared for element reading this will get the element token and pass this JsonReader into the elementReaderFunc
to 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(ReadValueCallback<JsonReader,T> objectReaderFunc) or if a JSON map should be read use readMap(ReadValueCallback<JsonReader,T> valueReaderFunc).
Parameters:
Returns:
Throws:
readMap
public final Map
Reads a JSON map.
If the currentToken() is null this will nextToken(). If the starting token is still null or NULL null will be returned. If the token is anything other than START_OBJECT an IllegalStateException will 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 JsonReader into the valueReaderFunc
to 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(ReadValueCallback<JsonReader,T> objectReaderFunc) or if a JSON array should be read use readArray(ReadValueCallback<JsonReader,T> elementReaderFunc).
Parameters:
Returns:
Throws:
readObject
public final T
Reads a JSON object.
If the currentToken() is null this will nextToken(). If the starting token is still null or NULL null will be returned. If the token is anything other than START_OBJECT an IllegalStateException will be thrown.
Once the JSON stream is prepared for object reading this will get the next token and pass this JsonReader into the objectReaderFunc
to handle reading the object.
If a JSON array should be read use readArray(ReadValueCallback<JsonReader,T> elementReaderFunc) or if a JSON map should be read use readMap(ReadValueCallback<JsonReader,T> valueReaderFunc).
Parameters:
Returns:
Throws:
bufferObject
public abstract JsonReader bufferObject()
Reads and returns the current JSON object the JsonReader is pointing to. This will mutate the current location of this JsonReader.
If the currentToken() isn't START_OBJECT or FIELD_NAME an IllegalStateException will be thrown.
If the currentToken() is FIELD_NAME this will create a JSON object where the first JSON field is the currentToken() field, meaning this can be called from the middle of a JSON object to create a new JSON object with only a subset of fields (those remaining from when the method is called).
The returned JsonReader is able to be reset() to replay the underlying JSON stream.
Returns:
Throws:
close
public abstract void close()
Closes the JSON stream.
Throws:
currentToken
public abstract JsonToken currentToken()
Gets the JsonToken that 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:
getBinary
public abstract byte[] getBinary()
Gets the binary value if the reader is currently pointing to a STRING token.
This returns the equivalent of Base64#getDecoder() Base64.Decoder#decode(String).
If the reader is pointing to a NULL null will be returned. If the reader is pointing to any other token type an IllegalStateException will be thrown.
Returns:
Throws:
getBoolean
public abstract boolean getBoolean()
Gets the boolean value if the reader is currently pointing to a BOOLEAN token.
If the reader is pointing to any other token type an IllegalStateException will be thrown.
If Boolean should be read use getNullable(ReadValueCallback<JsonReader,T> nonNullGetter).
Returns:
Throws:
getDouble
public abstract double getDouble()
Gets the double value if the reader is currently pointing to a NUMBER or STRING.
STRING will throw a NumberFormatException if the underlying string value cannot be converted to a double.
All other JsonToken types will throw an IllegalStateException.
If Double should be read use getNullable(ReadValueCallback<JsonReader,T> nonNullGetter).
Returns:
Throws:
getFieldName
public abstract String getFieldName()
Gets the field name if the reader is currently pointing to a FIELD_NAME.
All other JsonToken types will throw an IllegalStateException.
Returns:
Throws:
getFloat
public abstract float getFloat()
Gets the float value if the reader is currently pointing to a NUMBER or STRING.
STRING will throw a NumberFormatException if the underlying string value cannot be converted to a float.
All other JsonToken types will throw an IllegalStateException.
If Float should be read use getNullable(ReadValueCallback<JsonReader,T> nonNullGetter).
Returns:
Throws:
getInt
public abstract int getInt()
Gets the int value if the reader is currently pointing to a NUMBER or STRING.
STRING will throw a NumberFormatException if the underlying string value cannot be converted to an int.
All other JsonToken types will throw an IllegalStateException.
If Integer should be read use getNullable(ReadValueCallback<JsonReader,T> nonNullGetter).
Returns:
Throws:
getLong
public abstract long getLong()
Gets the long value if the reader is currently pointing to a NUMBER or STRING.
STRING will throw a NumberFormatException if the underlying string value cannot be converted to a long.
All other JsonToken types will throw an IllegalStateException.
If Long should be read use getNullable(ReadValueCallback<JsonReader,T> nonNullGetter).
Returns:
Throws:
getRawText
public String getRawText()
Gets the raw text value for the currentToken().
The following is how each JsonToken type is handled:
- START_OBJECT -> {
- END_OBJECT -> }
- START_ARRAY -> [
- END_ARRAY -> ]
- FIELD_NAME -> getFieldName() retaining JSON encoded and quoted characters
- BOOLEAN -> String.valueOf getBoolean()
- NULL -> "null"
- STRING -> getString() retaining JSON encoded and quoted characters
- NUMBER -> String.valueOf getString()
If the current token is null an IllegalStateException will be thrown.
Returns:
Throws:
getString
public abstract String getString()
Gets the String value if the reader is currently pointing to a BOOLEAN, NULL, NUMBER, or STRING.
If the current token is a BOOLEAN, or NUMBER the String representation of the value will be returned. If the current token is NULL null will be returned.
All other JsonToken types will throw an IllegalStateException.
Returns:
Throws:
getText
public final String getText()
Gets the text value for the currentToken().
The following is how each JsonToken type is handled:
- START_OBJECT -> {
- END_OBJECT -> }
- START_ARRAY -> [
- END_ARRAY -> ]
- FIELD_NAME -> getFieldName()
- BOOLEAN -> String.valueOf getBoolean()
- NULL -> "null"
- STRING -> getString()
- NUMBER -> String.valueOf getString()
If the current token is null an IllegalStateException will be thrown.
Returns:
Throws:
isEndArrayOrObject
public final boolean isEndArrayOrObject()
Whether the currentToken() is END_ARRAY or END_OBJECT.
Returns:
isResetSupported
public abstract boolean isResetSupported()
Indicates whether the JsonReader supports reset().
Returns:
isStartArrayOrObject
public final boolean isStartArrayOrObject()
Whether the currentToken() is START_ARRAY or START_OBJECT.
Returns:
nextToken
public abstract JsonToken nextToken()
Iterates to and returns the next JsonToken in the JSON encoded value.
Returns null if iterating to the next token completes reading of the JSON encoded value.
Returns:
Throws:
readChildren
public final String readChildren()
Recursively reads the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT.
If the currentToken() isn't START_OBJECT or START_ARRAY nothing will be read.
Returns:
Throws:
readChildren
public final void readChildren(StringBuilder buffer)
Recursively reads the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT into the passed StringBuilder.
If the currentToken() isn't START_OBJECT or START_ARRAY nothing will be read.
Parameters:
Throws:
buffer
is null.
readRemainingFieldsAsJsonObject
public final String readRemainingFieldsAsJsonObject()
Reads the remaining fields in the current JSON object as a JSON object.
If the currentToken() is START_OBJECT this functions the same as readChildren(). If the currentToken() is FIELD_NAME this creates a JSON object where the first field is the current field and reads the remaining fields in the JSON object.
If the currentToken() isn't START_OBJECT or FIELD_NAME nothing will be read.
Returns:
Throws:
readRemainingFieldsAsJsonObject
public final void readRemainingFieldsAsJsonObject(StringBuilder buffer)
Reads the remaining fields in the current JSON object as a JSON object.
If the currentToken() is START_OBJECT this functions the same as readChildren(StringBuilder buffer). If the currentToken() is FIELD_NAME this creates a JSON object where the first field is the current field and reads the remaining fields in the JSON object.
If the currentToken() isn't START_OBJECT or FIELD_NAME nothing will be read.
Parameters:
Throws:
buffer
is null.
readUntyped
public final Object readUntyped()
Reads an untyped object.
If the currentToken() is null this will nextToken().
If the starting token is END_ARRAY, END_OBJECT, or FIELD_NAME an IllegalStateException will be thrown as these are invalid starting points for reading an unknown type. If the untyped object is deeply nested an IllegalStateException will 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 NULL
- true or false if the starting token is BOOLEAN
- One of int, long, float, or double is the starting token is NUMBER, the smallest containing value will be used if the number is an integer
- An array of untyped elements if the starting point is START_ARRAY
- A map of String-untyped value if the starting point is START_OBJECT
Returns:
Throws:
reset
public abstract JsonReader reset()
Creates a new JsonReader reset to the beginning of the JSON stream.
Use isResetSupported() to determine whether the JsonReader can be reset. If resetting is called and it isn't supported an IllegalStateException will be thrown.
Returns:
Throws:
skipChildren
public abstract void skipChildren()
Recursively skips the JSON token sub-stream if the current token is either START_ARRAY or START_OBJECT.
If the current token isn't the beginning of an array or object this method is a no-op.
Throws:
Applies to
Azure SDK for Java