Class JsonTreeReader

java.lang.Object
com.google.gson.stream.JsonReader
com.google.gson.internal.bind.JsonTreeReader
All Implemented Interfaces:
Closeable, 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[]
     
    private String[]
     
    private static final Object
     
    private Object[]
     
    private int
     
    private static final Reader
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.
    void
    Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.
    void
    Closes this JSON reader and the underlying Reader.
    void
    Consumes the next token from the JSON stream and asserts that it is the end of the current array.
    void
    Consumes the next token from the JSON stream and asserts that it is the end of the current object.
    private void
    expect(JsonToken expected)
     
    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 name has already been consumed.
    private String
    getPath(boolean usePreviousPath)
     
    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 name has already been consumed.
    boolean
    Returns true if the current array or object has another element.
    private String
     
    boolean
    Returns the boolean value of the next token, consuming it.
    double
    Returns the double value of the next token, consuming it.
    int
    Returns the int value of the next token, consuming it.
    (package private) JsonElement
     
    long
    Returns the long value of the next token, consuming it.
    Returns the next token, a property name, and consumes it.
    private String
    nextName(boolean skipName)
     
    void
    Consumes the next token from the JSON stream and asserts that it is a literal null.
    Returns the string value of the next token, consuming it.
    Returns the type of the next token without consuming it.
    private Object
     
    private Object
     
    void
     
    private void
    push(Object newTop)
     
    void
    Skips the next value recursively.
     

    Methods inherited from class com.google.gson.stream.JsonReader

    isLenient, setLenient

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • UNREADABLE_READER

      private static final Reader UNREADABLE_READER
    • SENTINEL_CLOSED

      private static final Object SENTINEL_CLOSED
    • stack

      private Object[] stack
    • stackSize

      private int stackSize
    • pathNames

      private String[] pathNames
    • pathIndices

      private int[] pathIndices
  • Constructor Details

    • JsonTreeReader

      public JsonTreeReader(JsonElement element)
  • Method Details

    • beginArray

      public void beginArray() throws 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 class JsonReader
      Throws:
      IOException
    • endArray

      public void endArray() throws 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 class JsonReader
      Throws:
      IOException
    • beginObject

      public void beginObject() throws 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 class JsonReader
      Throws:
      IOException
    • endObject

      public void endObject() throws 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 class JsonReader
      Throws:
      IOException
    • hasNext

      public boolean hasNext() throws IOException
      Description copied from class: JsonReader
      Returns true if the current array or object has another element.
      Overrides:
      hasNext in class JsonReader
      Throws:
      IOException
    • peek

      public JsonToken peek() throws IOException
      Description copied from class: JsonReader
      Returns the type of the next token without consuming it.
      Overrides:
      peek in class JsonReader
      Throws:
      IOException
    • peekStack

      private Object peekStack()
    • popStack

      private Object popStack()
    • expect

      private void expect(JsonToken expected) throws IOException
      Throws:
      IOException
    • nextName

      private String nextName(boolean skipName) throws IOException
      Throws:
      IOException
    • nextName

      public String nextName() throws IOException
      Description copied from class: JsonReader
      Returns the next token, a property name, and consumes it.
      Overrides:
      nextName in class JsonReader
      Throws:
      IOException - if the next token in the stream is not a property name.
    • nextString

      public String nextString() throws IOException
      Description copied from class: JsonReader
      Returns the string value of the next token, consuming it. If the next token is a number, this method will return its string form.
      Overrides:
      nextString in class JsonReader
      Throws:
      IOException
    • nextBoolean

      public boolean nextBoolean() throws IOException
      Description copied from class: JsonReader
      Returns the boolean value of the next token, consuming it.
      Overrides:
      nextBoolean in class JsonReader
      Throws:
      IOException
    • nextNull

      public void nextNull() throws 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 class JsonReader
      Throws:
      IOException
    • nextDouble

      public double nextDouble() throws IOException
      Description copied from class: JsonReader
      Returns the double value of the next token, consuming it. If the next token is a string, this method will attempt to parse it as a double using Double.parseDouble(String).
      Overrides:
      nextDouble in class JsonReader
      Throws:
      MalformedJsonException - if the next literal value is NaN or Infinity and this reader is not lenient.
      IOException
    • nextLong

      public long nextLong() throws IOException
      Description copied from class: JsonReader
      Returns the long 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 Java long, this method throws.
      Overrides:
      nextLong in class JsonReader
      Throws:
      IOException
    • nextInt

      public int nextInt() throws IOException
      Description copied from class: JsonReader
      Returns the int 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 Java int, this method throws.
      Overrides:
      nextInt in class JsonReader
      Throws:
      IOException
    • nextJsonElement

      JsonElement nextJsonElement() throws IOException
      Throws:
      IOException
    • close

      public void close() throws IOException
      Description copied from class: JsonReader
      Closes this JSON reader and the underlying Reader.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class JsonReader
      Throws:
      IOException
    • skipValue

      public void skipValue() throws IOException
      Description copied from class: JsonReader
      Skips 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:
      skipValue in class JsonReader
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class JsonReader
    • promoteNameToValue

      public void promoteNameToValue() throws IOException
      Throws:
      IOException
    • push

      private void push(Object newTop)
    • getPath

      private String getPath(boolean usePreviousPath)
    • getPreviousPath

      public 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 name has already been consumed.

      This method can be useful to add additional context to exception messages after a value has been consumed.

      Overrides:
      getPreviousPath in class JsonReader
    • getPath

      public 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 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:
      getPath in class JsonReader
    • locationString

      private String locationString()