JsonWriter

abstract class JsonWriter : Closeable, Flushable

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 JSONTo encode your data as JSON, create a new {@code 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 methods or by nesting other arrays and objects. 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.
ExampleSuppose we'd like to encode a stream of messages such as the following:
{@code * [ * { * "id": 912345678901, * "text": "How do I stream JSON in Java?", * "geo": null, * "user": { * "name": "json_newb", * "followers_count": 41 * } * }, * { * "id": 912345678902, * "text": "@json_newb just use JsonWriter!", * "geo": [50.454722, -104.606667], * "user": { * "name": "jesse", * "followers_count": 2 * } * } * ] * }
This code encodes the above structure:
{@code * public void writeJsonStream(BufferedSink sink, Listmessages) throws IOException {
 *   JsonWriter writer = JsonWriter.of(sink);
 *   writer.setIndent("  ");
 *   writeMessagesArray(writer, messages);
 *   writer.close();
 * }
 *
 * public void writeMessagesArray(JsonWriter writer, List

Each {@code 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 .

Functions

beginArray
Link copied to clipboard
abstract fun beginArray(): JsonWriter
Begins encoding a new array.
beginFlatten
Link copied to clipboard
fun beginFlatten(): Int
Cancels immediately-nested calls to beginArray or beginObject and theirmatching calls to endArray or endObject.
beginObject
Link copied to clipboard
abstract fun beginObject(): JsonWriter
Begins encoding a new object.
close
Link copied to clipboard
abstract fun close()
endArray
Link copied to clipboard
abstract fun endArray(): JsonWriter
Ends encoding the current array.
endFlatten
Link copied to clipboard
fun endFlatten(token: Int)
Ends nested call flattening created by beginFlatten.
endObject
Link copied to clipboard
abstract fun endObject(): JsonWriter
Ends encoding the current object.
flush
Link copied to clipboard
abstract fun flush()
getPath
Link copied to clipboard
fun getPath(): String
Returns a JsonPath to the current locationin the JSON value.
isLenient
Link copied to clipboard
fun isLenient(): Boolean
Returns true if this writer has relaxed syntax rules.
jsonValue
Link copied to clipboard
fun jsonValue(@Nullable() value: Any): JsonWriter
Encodes the value which may be a string, number, boolean, null, map, or list.
name
Link copied to clipboard
abstract fun name(name: String): JsonWriter
Encodes the property name.
nullValue
Link copied to clipboard
abstract fun nullValue(): JsonWriter
Encodes {@code null}.
of
Link copied to clipboard
open fun of(sink: BufferedSink): JsonWriter
Returns a new instance that writes UTF-8 encoded JSON to {@code sink}.
promoteValueToName
Link copied to clipboard
fun promoteValueToName()
Changes the writer to treat the next value as a string name.
setTag
Link copied to clipboard
fun <T> setTag(clazz: Class<T>, value: T)
Assigns the tag value using the given class key and value.
tag
Link copied to clipboard
fun <T> tag(clazz: Class<T>): T
Returns the tag value for the given class key.
value
Link copied to clipboard
abstract fun value(value: Boolean): JsonWriter
Encodes {@code value}.
abstract fun value(value: Double): JsonWriter
Encodes {@code value}.
abstract fun value(@Nullable() value: Boolean): JsonWriter
Encodes {@code value}.
abstract fun value(@Nullable() value: Number): JsonWriter
Encodes {@code value}.
abstract fun value(@Nullable() value: String): JsonWriter
Encodes {@code value}.
abstract fun value(value: Long): JsonWriter
Encodes {@code value}.
fun value(source: BufferedSource): JsonWriter
Writes {@code source} directly without encoding its contents.
valueSink
Link copied to clipboard
abstract fun valueSink(): BufferedSink
Returns a BufferedSink into which arbitrary data can be written without any additionalencoding.

Properties

indent
Link copied to clipboard
open var indent: String
A string containing a full set of spaces for a single level of indentation, or null for nopretty printing.
lenient
Link copied to clipboard
open var lenient: Boolean
serializeNulls
Link copied to clipboard
open var serializeNulls: Boolean