JsonReader

interface JsonReader : Closeable

Reads a JSON RFC 7159 encoded value as a stream of tokens.

This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays.

The tokens are traversed in depth-first order, the same order that they appear in the JSON document. Within JSON objects, name/value pairs are represented by a single token.

Each {@code JsonReader} may be used to read a single JSON stream. Instances of this class are not thread safe.

Json doesn't have int, long, double values. Only string and number. That means nextInt(), nextDouble(), nextLong() and nextNumber() will coerce the Json value into the expected type or throw if the conversion is not possible or loses precision.

BufferedSourceJsonReader can parse longs without having to buffer so when possible, prefer nextLong() over nextInt()

Note: This interface was originally from Moshi and has been tweaked to better match the GraphQL use cases

Types

Token
Link copied to clipboard
common
enum Token : Enum<JsonReader.Token>
A structure, name, or value type in a JSON-encoded string.

Functions

beginArray
Link copied to clipboard
common
abstract fun beginArray(): JsonReader
Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.
beginObject
Link copied to clipboard
common
abstract fun beginObject(): JsonReader
Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.
close
Link copied to clipboard
common
abstract fun close()
endArray
Link copied to clipboard
common
abstract fun endArray(): JsonReader
Consumes the next token from the JSON stream and asserts that it is the end of the current array.
endObject
Link copied to clipboard
common
abstract fun endObject(): JsonReader
Consumes the next token from the JSON stream and asserts that it is the end of the current object.
hasNext
Link copied to clipboard
common
abstract fun hasNext(): Boolean
Returns true if the current array or object has another element.
nextBoolean
Link copied to clipboard
common
abstract fun nextBoolean(): Boolean
Returns the Token.BOOLEAN value of the next token, consuming it.
nextDouble
Link copied to clipboard
common
abstract fun nextDouble(): Double
Returns the Token.NUMBER value of the next token, consuming it.
nextInt
Link copied to clipboard
common
abstract fun nextInt(): Int
Returns the Token.NUMBER value of the next token, consuming it.
nextLong
Link copied to clipboard
common
abstract fun nextLong(): Long
Returns the Token.NUMBER value of the next token, consuming it.
nextName
Link copied to clipboard
common
abstract fun nextName(): String
Returns the next token Token.NAME, and consumes it.
nextNull
Link copied to clipboard
common
abstract fun nextNull(): Nothing?
Consumes the next token from the JSON stream and asserts that it is a literal null.
nextNumber
Link copied to clipboard
common
abstract fun nextNumber(): JsonNumber
Returns the Token.NUMBER value of the next token, consuming it.
nextString
Link copied to clipboard
common
abstract fun nextString(): String?
Returns the Token.STRING value of the next token, consuming it.
peek
Link copied to clipboard
common
abstract fun peek(): JsonReader.Token
Returns the type of the next token without consuming it.
rewind
Link copied to clipboard
common
abstract fun rewind()
Reset the current object so that selectName start returning names again
selectName
Link copied to clipboard
common
abstract fun selectName(names: List<String>): Int
An optimized way to retrieve the nextName when the candidates and their order is known.
skipValue
Link copied to clipboard
common
abstract fun skipValue()
Skips the next value recursively.

Inheritors

BufferedSourceJsonReader
Link copied to clipboard
MapJsonReader
Link copied to clipboard

Extensions

buffer
Link copied to clipboard
common
fun JsonReader.buffer(): MapJsonReader
buffers the next Object.
readAny
Link copied to clipboard
common
fun JsonReader.readAny(): Any?
Reads the reader and maps numbers to the closest representation possible in that order:
  • Int

  • Long

  • Double

  • JsonNumber