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

Inheritors

Types

Link copied to clipboard

A structure, name, or value type in a JSON-encoded string.

Functions

Link copied to clipboard
abstract fun beginArray(): JsonReader

Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.

Link copied to clipboard
abstract fun beginObject(): JsonReader

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Link copied to clipboard

buffers the next Object. Has to be called in BEGIN_OBJECT position. The returned MapJsonReader can use MapJsonReader.rewind to read fields multiple times

Link copied to clipboard
abstract fun close()
Link copied to clipboard
abstract fun endArray(): JsonReader

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Link copied to clipboard
abstract fun endObject(): JsonReader

Consumes the next token from the JSON stream and asserts that it is the end of the current object.

Link copied to clipboard
abstract fun getPath(): List<Any>

Returns the current path of the JSON being read, as a List. The elements can either be Strings (names) or Integers (array indices).

Link copied to clipboard
abstract fun hasNext(): Boolean

Returns true if the current array or object has another element.

Link copied to clipboard
abstract fun nextBoolean(): Boolean

Returns the Token.BOOLEAN value of the next token, consuming it.

Link copied to clipboard
abstract fun nextDouble(): Double

Returns the Token.NUMBER value of the next token, consuming it.

Link copied to clipboard
abstract fun nextInt(): Int

Returns the Token.NUMBER value of the next token, consuming it.

Link copied to clipboard
abstract fun nextLong(): Long

Returns the Token.NUMBER value of the next token, consuming it.

Link copied to clipboard
abstract fun nextName(): String

Returns the next token Token.NAME, and consumes it.

Link copied to clipboard
abstract fun nextNull(): Nothing?

Consumes the next token from the JSON stream and asserts that it is a literal null. Returns null.

Link copied to clipboard
abstract fun nextNumber(): JsonNumber

Returns the Token.NUMBER value of the next token, consuming it.

Link copied to clipboard
abstract fun nextString(): String?

Returns the Token.STRING value of the next token, consuming it.

Link copied to clipboard
fun <D : Operation.Data> JsonReader.parseResponse(operation: Operation<D>, requestUuid: Uuid? = null, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, deferredFragmentIdentifiers: Set<DeferredFragmentIdentifier>? = null): ApolloResponse<D>

Reads a ApolloResponse from this. The caller is responsible for closing this.

Link copied to clipboard
abstract fun peek(): JsonReader.Token

Returns the type of the next token without consuming it.

Link copied to clipboard

Returns the Kotlin representation of the given JsonReader

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
abstract fun rewind()

Reset the current object so that selectName start returning names again

Link copied to clipboard
abstract fun selectName(names: List<String>): Int

An optimized way to retrieve the nextName when the candidates and their order is known. selectName maintains the current index in the list and saves having to string compare nextName to every candidate.

Link copied to clipboard
abstract fun skipValue()

Skips the next value recursively. If it is an object or array, all nested elements are skipped.

Link copied to clipboard
fun <D : Operation.Data> JsonReader.toApolloResponse(operation: Operation<D>, requestUuid: Uuid? = null, customScalarAdapters: CustomScalarAdapters = CustomScalarAdapters.Empty, deferredFragmentIdentifiers: Set<DeferredFragmentIdentifier>? = null): ApolloResponse<D>

Reads a single ApolloResponse from this. Returns an error response if this contains more than one JSON response or trailing tokens. toApolloResponse takes ownership and closes this.