JsonWriter

interface JsonWriter : Closeable

Writes a JSON RFC 7159 encoded value to a stream, one token at a time.

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

Encoding JSON

To encode your data as JSON, create a new JsonWriter. Each JSON document must contain one top-level array or object. Call methods on the writer as you walk the structure's contents, nesting arrays and objects as necessary:

  • To write arrays, first call beginArray. Write each of the array's elements with the appropriate value. Finally close the array using endArray.

  • To write objects, first call beginObject. Write each of the object's properties by alternating calls to name with the property's value. Write property values with the appropriate value method or by nesting other objects or arrays. Finally close the object using endObject.

Each JsonWriter may be used to write a single JSON stream. Instances of this class are not thread safe. Calls that would result in a malformed JSON string will fail with an IllegalStateException.

Note: This interface was originally from Moshi and has been tweaked to better match the GraphQL use cases such as the absence of arbitrary precision and the presence of the Upload

Inheritors

Properties

Link copied to clipboard
abstract val path: String

Functions

Link copied to clipboard
abstract fun beginArray(): JsonWriter

Begins encoding a new array. Each call to this method must be paired with a call to endArray.

Link copied to clipboard
abstract fun beginObject(): JsonWriter

Begins encoding a new object. Each call to this method must be paired with a call to endObject.

Link copied to clipboard
expect abstract fun close()
Link copied to clipboard
abstract fun endArray(): JsonWriter

Ends encoding the current array.

Link copied to clipboard
abstract fun endObject(): JsonWriter

Ends encoding the current object.

Link copied to clipboard
abstract fun flush()

Flushes the writer

Link copied to clipboard
abstract fun name(name: String): JsonWriter

Encodes the property name.

Link copied to clipboard
abstract fun nullValue(): JsonWriter

Encodes null.

Link copied to clipboard
abstract fun value(value: Upload): JsonWriter

Encodes a Upload.

abstract fun value(value: JsonNumber): JsonWriter

Encodes number value.

abstract fun value(value: Boolean): JsonWriter

Encodes boolean value.

abstract fun value(value: Double): JsonWriter

Encodes a finite double value.

abstract fun value(value: Int): JsonWriter

Encodes int value.

abstract fun value(value: Long): JsonWriter

Encodes long value.

abstract fun value(value: String): JsonWriter

Encodes the literal string value, or null to encode a null literal.

Link copied to clipboard
fun JsonWriter.writeAny(value: Any?)
Link copied to clipboard
inline fun JsonWriter.writeArray(crossinline block: JsonWriter.() -> Unit)
Link copied to clipboard
inline fun JsonWriter.writeObject(crossinline block: JsonWriter.() -> Unit)