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.
Author:
Jesse Wilson
  • 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
    • 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
    • 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:
      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
    • close

      public void close() throws IOException
      Description copied from class: JsonReader
      Closes this JSON reader and the underlying Reader.

      Using the JSON reader after it has been closed will throw an IllegalStateException in most cases.

      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
    • 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. 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:
      getPath in class JsonReader
    • 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. 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:
      getPreviousPath in class JsonReader