- 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[]
pathIndices
private java.lang.String[]
pathNames
private static java.lang.Object
SENTINEL_CLOSED
private java.lang.Object[]
stack
private int
stackSize
private static java.io.Reader
UNREADABLE_READER
-
Constructor Summary
Constructors Constructor Description JsonTreeReader(JsonElement element)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginArray()
Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.void
beginObject()
Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.void
close()
Closes this JSON reader and the underlyingReader
.void
endArray()
Consumes the next token from the JSON stream and asserts that it is the end of the current array.void
endObject()
Consumes the next token from the JSON stream and asserts that it is the end of the current object.private void
expect(JsonToken expected)
java.lang.String
getPath()
Returns a JsonPath in dot-notation to the next (or current) location in the JSON document: 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 value has not been consumed yet.private java.lang.String
getPath(boolean usePreviousPath)
java.lang.String
getPreviousPath()
Returns a JsonPath in dot-notation to the previous (or current) location in the JSON document: 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 value has not been consumed yet.boolean
hasNext()
Returns true if the current array or object has another element.private java.lang.String
locationString()
boolean
nextBoolean()
Returns theboolean
value of the next token, consuming it.double
nextDouble()
Returns thedouble
value of the next token, consuming it.int
nextInt()
Returns theint
value of the next token, consuming it.(package private) JsonElement
nextJsonElement()
long
nextLong()
Returns thelong
value of the next token, consuming it.java.lang.String
nextName()
Returns the next token, aproperty name
, and consumes it.void
nextNull()
Consumes the next token from the JSON stream and asserts that it is a literal null.java.lang.String
nextString()
Returns thestring
value of the next token, consuming it.JsonToken
peek()
Returns the type of the next token without consuming it.private java.lang.Object
peekStack()
private java.lang.Object
popStack()
void
promoteNameToValue()
private void
push(java.lang.Object newTop)
void
skipValue()
Skips the next value recursively.java.lang.String
toString()
-
Methods inherited from class com.google.gson.stream.JsonReader
isLenient, setLenient
-
-
-
-
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
-
stackSize
private int stackSize
-
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.IOException
Description copied from class:JsonReader
Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.- Overrides:
beginArray
in classJsonReader
- Throws:
java.io.IOException
-
endArray
public void endArray() throws java.io.IOException
Description copied from class:JsonReader
Consumes the next token from the JSON stream and asserts that it is the end of the current array.- Overrides:
endArray
in classJsonReader
- Throws:
java.io.IOException
-
beginObject
public void beginObject() throws java.io.IOException
Description copied from class:JsonReader
Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.- Overrides:
beginObject
in classJsonReader
- Throws:
java.io.IOException
-
endObject
public void endObject() throws java.io.IOException
Description copied from class:JsonReader
Consumes the next token from the JSON stream and asserts that it is the end of the current object.- Overrides:
endObject
in classJsonReader
- Throws:
java.io.IOException
-
hasNext
public boolean hasNext() throws java.io.IOException
Description copied from class:JsonReader
Returns true if the current array or object has another element.- Overrides:
hasNext
in classJsonReader
- Throws:
java.io.IOException
-
peek
public JsonToken peek() throws java.io.IOException
Description copied from class:JsonReader
Returns the type of the next token without consuming it.- Overrides:
peek
in 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
public java.lang.String nextName() throws java.io.IOException
Description copied from class:JsonReader
Returns the next token, aproperty name
, and consumes it.- Overrides:
nextName
in classJsonReader
- Throws:
java.io.IOException
- if the next token in the stream is not a property name.
-
nextString
public java.lang.String nextString() throws java.io.IOException
Description copied from class:JsonReader
Returns thestring
value of the next token, consuming it. If the next token is a number, this method will return its string form.- Overrides:
nextString
in classJsonReader
- Throws:
java.io.IOException
-
nextBoolean
public boolean nextBoolean() throws java.io.IOException
Description copied from class:JsonReader
Returns theboolean
value of the next token, consuming it.- Overrides:
nextBoolean
in classJsonReader
- Throws:
java.io.IOException
-
nextNull
public void nextNull() throws java.io.IOException
Description copied from class:JsonReader
Consumes the next token from the JSON stream and asserts that it is a literal null.- Overrides:
nextNull
in classJsonReader
- Throws:
java.io.IOException
-
nextDouble
public double nextDouble() throws java.io.IOException
Description copied from class:JsonReader
Returns thedouble
value 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:
nextDouble
in classJsonReader
- Throws:
java.io.IOException
-
nextLong
public long nextLong() throws java.io.IOException
Description copied from class:JsonReader
Returns thelong
value 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:
nextLong
in classJsonReader
- Throws:
java.io.IOException
-
nextInt
public int nextInt() throws java.io.IOException
Description copied from class:JsonReader
Returns theint
value 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:
nextInt
in classJsonReader
- Throws:
java.io.IOException
-
nextJsonElement
JsonElement nextJsonElement() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Description copied from class:JsonReader
Closes this JSON reader and the underlyingReader
.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classJsonReader
- Throws:
java.io.IOException
-
skipValue
public void skipValue() throws java.io.IOException
Description copied from class:JsonReader
Skips the next value recursively. If it is an object or array, all nested elements are skipped. This method is intended for use when the JSON token stream contains unrecognized or unhandled values.- Overrides:
skipValue
in classJsonReader
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in 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)
-
getPreviousPath
public java.lang.String getPreviousPath()
Description copied from class:JsonReader
Returns a JsonPath in dot-notation to the previous (or current) location in the JSON document:- 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 value has not been consumed yet.
This method can be useful to add additional context to exception messages after a value has been consumed.
- Overrides:
getPreviousPath
in classJsonReader
- For JSON arrays the path points to the index of the previous element.
-
getPath
public java.lang.String getPath()
Description copied from class:JsonReader
Returns a JsonPath in dot-notation to the next (or current) location in the JSON document:- 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 value has not been consumed yet.
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:
getPath
in classJsonReader
-
locationString
private java.lang.String locationString()
-
-