Package com.google.gson.internal.bind
Class JsonTreeReader
- java.lang.Object
-
- com.google.gson.stream.JsonReader
-
- com.google.gson.internal.bind.JsonTreeReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class JsonTreeReader extends JsonReader
This reader walks the elements of a JsonElement as if it was coming from a character stream.
-
-
Field Summary
Fields Modifier and Type Field Description private int[]pathIndicesprivate java.lang.String[]pathNamesprivate static java.lang.ObjectSENTINEL_CLOSEDprivate java.lang.Object[]stackThe nesting stack.private intstackSizeThe used size ofstack; the value atstackSize - 1is the value last placed on the stack.private static java.io.ReaderUNREADABLE_READER
-
Constructor Summary
Constructors Constructor Description JsonTreeReader(JsonElement element)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginArray()Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.voidbeginObject()Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.voidclose()Closes this JSON reader and the underlyingReader.voidendArray()Consumes the next token from the JSON stream and asserts that it is the end of the current array.voidendObject()Consumes the next token from the JSON stream and asserts that it is the end of the current object.private voidexpect(JsonToken expected)java.lang.StringgetPath()Returns a JSONPath in dot-notation to the next (or current) location in the JSON document.private java.lang.StringgetPath(boolean usePreviousPath)java.lang.StringgetPreviousPath()Returns a JSONPath in dot-notation to the previous (or current) location in the JSON document.booleanhasNext()Returns true if the current array or object has another element.private java.lang.StringlocationString()booleannextBoolean()Returns thebooleanvalue of the next token, consuming it.doublenextDouble()Returns thedoublevalue of the next token, consuming it.intnextInt()Returns theintvalue of the next token, consuming it.(package private) JsonElementnextJsonElement()longnextLong()Returns thelongvalue of the next token, consuming it.java.lang.StringnextName()Returns the next token, aproperty name, and consumes it.private java.lang.StringnextName(boolean skipName)voidnextNull()Consumes the next token from the JSON stream and asserts that it is a literal null.java.lang.StringnextString()Returns thestringvalue of the next token, consuming it.JsonTokenpeek()Returns the type of the next token without consuming it.private java.lang.ObjectpeekStack()private java.lang.ObjectpopStack()voidpromoteNameToValue()private voidpush(java.lang.Object newTop)voidskipValue()Skips the next value recursively.java.lang.StringtoString()-
Methods inherited from class com.google.gson.stream.JsonReader
getNestingLimit, getStrictness, isLenient, setLenient, setNestingLimit, setStrictness
-
-
-
-
Field Detail
-
UNREADABLE_READER
private static final java.io.Reader UNREADABLE_READER
-
SENTINEL_CLOSED
private static final java.lang.Object SENTINEL_CLOSED
-
stack
private java.lang.Object[] stack
The nesting stack. Using a manual array rather than an ArrayList saves 20%.
-
stackSize
private int stackSize
The used size ofstack; the value atstackSize - 1is the value last placed on the stack.stackSizemight differ from the nesting depth, because the stack also contains temporary additional objects, for example for a JsonArray it contains the JsonArray object as well as the corresponding iterator.
-
pathNames
private java.lang.String[] pathNames
-
pathIndices
private int[] pathIndices
-
-
Constructor Detail
-
JsonTreeReader
public JsonTreeReader(JsonElement element)
-
-
Method Detail
-
beginArray
public void beginArray() throws java.io.IOExceptionDescription copied from class:JsonReaderConsumes the next token from the JSON stream and asserts that it is the beginning of a new array.- Overrides:
beginArrayin classJsonReader- Throws:
java.io.IOException
-
endArray
public void endArray() throws java.io.IOExceptionDescription copied from class:JsonReaderConsumes the next token from the JSON stream and asserts that it is the end of the current array.- Overrides:
endArrayin classJsonReader- Throws:
java.io.IOException
-
beginObject
public void beginObject() throws java.io.IOExceptionDescription copied from class:JsonReaderConsumes the next token from the JSON stream and asserts that it is the beginning of a new object.- Overrides:
beginObjectin classJsonReader- Throws:
java.io.IOException
-
endObject
public void endObject() throws java.io.IOExceptionDescription copied from class:JsonReaderConsumes the next token from the JSON stream and asserts that it is the end of the current object.- Overrides:
endObjectin classJsonReader- Throws:
java.io.IOException
-
hasNext
public boolean hasNext() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns true if the current array or object has another element.- Overrides:
hasNextin classJsonReader- Throws:
java.io.IOException
-
peek
public JsonToken peek() throws java.io.IOException
Description copied from class:JsonReaderReturns the type of the next token without consuming it.- Overrides:
peekin classJsonReader- Throws:
java.io.IOException
-
peekStack
private java.lang.Object peekStack()
-
popStack
private java.lang.Object popStack()
-
expect
private void expect(JsonToken expected) throws java.io.IOException
- Throws:
java.io.IOException
-
nextName
private java.lang.String nextName(boolean skipName) throws java.io.IOException- Throws:
java.io.IOException
-
nextName
public java.lang.String nextName() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns the next token, aproperty name, and consumes it.- Overrides:
nextNamein classJsonReader- Throws:
java.io.IOException
-
nextString
public java.lang.String nextString() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns thestringvalue of the next token, consuming it. If the next token is a number, this method will return its string form.- Overrides:
nextStringin classJsonReader- Throws:
java.io.IOException
-
nextBoolean
public boolean nextBoolean() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns thebooleanvalue of the next token, consuming it.- Overrides:
nextBooleanin classJsonReader- Throws:
java.io.IOException
-
nextNull
public void nextNull() throws java.io.IOExceptionDescription copied from class:JsonReaderConsumes the next token from the JSON stream and asserts that it is a literal null.- Overrides:
nextNullin classJsonReader- Throws:
java.io.IOException
-
nextDouble
public double nextDouble() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns thedoublevalue of the next token, consuming it. If the next token is a string, this method will attempt to parse it as a double usingDouble.parseDouble(String).- Overrides:
nextDoublein classJsonReader- Throws:
MalformedJsonException- if the next literal value is NaN or Infinity and this reader is notlenient.java.io.IOException
-
nextLong
public long nextLong() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns thelongvalue of the next token, consuming it. If the next token is a string, this method will attempt to parse it as a long. If the next token's numeric value cannot be exactly represented by a Javalong, this method throws.- Overrides:
nextLongin classJsonReader- Throws:
java.io.IOException
-
nextInt
public int nextInt() throws java.io.IOExceptionDescription copied from class:JsonReaderReturns theintvalue of the next token, consuming it. If the next token is a string, this method will attempt to parse it as an int. If the next token's numeric value cannot be exactly represented by a Javaint, this method throws.- Overrides:
nextIntin classJsonReader- Throws:
java.io.IOException
-
nextJsonElement
JsonElement nextJsonElement() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionDescription copied from class:JsonReaderCloses this JSON reader and the underlyingReader.Using the JSON reader after it has been closed will throw an
IllegalStateExceptionin most cases.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classJsonReader- Throws:
java.io.IOException
-
skipValue
public void skipValue() throws java.io.IOExceptionDescription copied from class:JsonReaderSkips the next value recursively. This method is intended for use when the JSON token stream contains unrecognized or unhandled values.The behavior depends on the type of the next JSON token:
- Start of a JSON array or object: It and all of its nested values are skipped.
- Primitive value (for example a JSON number): The primitive value is skipped.
- Property name: Only the name but not the value of the property is skipped.
skipValue()has to be called again to skip the property value as well. - End of a JSON array or object: Only this end token is skipped.
- End of JSON document: Skipping has no effect, the next token continues to be the end of the document.
- Overrides:
skipValuein classJsonReader- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classJsonReader
-
promoteNameToValue
public void promoteNameToValue() throws java.io.IOException- Throws:
java.io.IOException
-
push
private void push(java.lang.Object newTop)
-
getPath
private java.lang.String getPath(boolean usePreviousPath)
-
getPath
public java.lang.String getPath()
Description copied from class:JsonReaderReturns a JSONPath in dot-notation to the next (or current) location in the JSON document. That means:- For JSON arrays the path points to the index of the next element (even if there are no further elements).
- For JSON objects the path points to the last property, or to the current property if its name has already been consumed.
This method can be useful to add additional context to exception messages before a value is consumed, for example when the peeked token is unexpected.
- Overrides:
getPathin classJsonReader
-
getPreviousPath
public java.lang.String getPreviousPath()
Description copied from class:JsonReaderReturns a JSONPath in dot-notation to the previous (or current) location in the JSON document. That means:- For JSON arrays the path points to the index of the previous element.
If no element has been consumed yet it uses the index 0 (even if there are no elements). - For JSON objects the path points to the last property, or to the current property if its name has already been consumed.
This method can be useful to add additional context to exception messages after a value has been consumed.
- Overrides:
getPreviousPathin classJsonReader
- For JSON arrays the path points to the index of the previous element.
-
locationString
private java.lang.String locationString()
-
-