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

Functions

beginArray
Link copied to clipboard
common
abstract fun beginArray(): JsonWriter
Begins encoding a new array.
beginObject
Link copied to clipboard
common
abstract fun beginObject(): JsonWriter
Begins encoding a new object.
close
Link copied to clipboard
common
abstract fun close()
endArray
Link copied to clipboard
common
abstract fun endArray(): JsonWriter
Ends encoding the current array.
endObject
Link copied to clipboard
common
abstract fun endObject(): JsonWriter
Ends encoding the current object.
flush
Link copied to clipboard
common
abstract fun flush()
Flushes the writer
name
Link copied to clipboard
common
abstract fun name(name: String): JsonWriter
Encodes the property name.
nullValue
Link copied to clipboard
common
abstract fun nullValue(): JsonWriter
Encodes null.
value
Link copied to clipboard
common
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.

Properties

path
Link copied to clipboard
common
abstract val path: String

Inheritors

BufferedSinkJsonWriter
Link copied to clipboard
MapJsonWriter
Link copied to clipboard

Extensions

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