JsonValueWriter

Writes JSON by building a Java object comprising maps, lists, and JSON primitives.

class JsonValueWriter : JsonWriter

Constructors

JsonValueWriter
Link copied to clipboard
open fun JsonValueWriter()

Functions

beginArray
Link copied to clipboard

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

open fun beginArray(): JsonWriter
beginFlatten
Link copied to clipboard

Cancels immediately-nested calls to beginArray or beginObject and their matching calls to endArray or endObject . Use this to compose JSON adapters without nesting.

For example, the following creates JSON with nested arrays: {@code [1,[2,3,4],5]} .

{@code * JsonAdapter

With flattening we can create JSON with a single array {@code [1,2,3,4,5]} :

{@code * JsonAdapter

This method flattens arrays within arrays:

{@code * Emit: [1, [2, 3, 4], 5] * To produce: [1, 2, 3, 4, 5] * }
It also flattens objects within objects. Do not call name before writing a flattened object.
{@code * Emit: {"a": 1, {"b": 2}, "c": 3} * To Produce: {"a": 1, "b": 2, "c": 3} * }
Other combinations are permitted but do not perform flattening. For example, objects inside of arrays are not flattened:
{@code * Emit: [1, {"b": 2}, 3, [4, 5], 6] * To Produce: [1, {"b": 2}, 3, 4, 5, 6] * }

This method returns an opaque token. Callers must match all calls to this method with a call to endFlatten with the matching token.

fun beginFlatten(): Int
beginObject
Link copied to clipboard

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

open fun beginObject(): JsonWriter
checkStack
Link copied to clipboard

Before pushing a value on the stack this confirms that the stack has capacity.

fun checkStack(): Boolean
close
Link copied to clipboard
open fun close()
abstract fun close()
endArray
Link copied to clipboard

Ends encoding the current array.

open fun endArray(): JsonWriter
endFlatten
Link copied to clipboard

Ends nested call flattening created by beginFlatten .

fun endFlatten(token: Int)
endObject
Link copied to clipboard

Ends encoding the current object.

open fun endObject(): JsonWriter
flush
Link copied to clipboard
open fun flush()
abstract fun flush()
getIndent
Link copied to clipboard

Returns a string containing only whitespace, used for each level of indentation. If empty, the encoded document will be compact.

fun getIndent(): String
getPath
Link copied to clipboard

Returns a JsonPath to the current location in the JSON value.

fun getPath(): String
getSerializeNulls
Link copied to clipboard

Returns true if object members are serialized when their value is null. This has no impact on array elements. The default is false.

fun getSerializeNulls(): Boolean
isLenient
Link copied to clipboard

Returns true if this writer has relaxed syntax rules.

fun isLenient(): Boolean
jsonValue
Link copied to clipboard

Encodes the value which may be a string, number, boolean, null, map, or list.

fun jsonValue(value: Any): JsonWriter
name
Link copied to clipboard

Encodes the property name.

open fun name(name: String): JsonWriter
nullValue
Link copied to clipboard

Encodes {@code null} .

open fun nullValue(): JsonWriter
of
Link copied to clipboard

Returns a new instance that writes UTF-8 encoded JSON to {@code sink} .

open fun of(sink: BufferedSink): JsonWriter
peekScope
Link copied to clipboard

Returns the scope on the top of the stack.

fun peekScope(): Int
promoteValueToName
Link copied to clipboard

Changes the writer to treat the next value as a string name. This is useful for map adapters so that arbitrary type adapters can use to write a name value.

In this example, calling this method allows two sequential calls to value to produce the object, {@code {"a": "b"}} .

{@code * JsonWriter writer = JsonWriter.of(...); * writer.beginObject(); * writer.promoteValueToName(); * writer.value("a"); * writer.value("b"); * writer.endObject(); * }

fun promoteValueToName()
pushScope
Link copied to clipboard
fun pushScope(newTop: Int)
replaceTop
Link copied to clipboard

Replace the value on the top of the stack with the given value.

fun replaceTop(topOfStack: Int)
root
Link copied to clipboard
open fun root(): Any
setIndent
Link copied to clipboard

Sets the indentation string to be repeated for each level of indentation in the encoded document. If {@code indent.isEmpty()} the encoded document will be compact. Otherwise the encoded document will be more human-readable.

open fun setIndent(indent: String)
setLenient
Link copied to clipboard

Configure this writer to relax its syntax rules. By default, this writer only emits well-formed JSON as specified by RFC 7159. Setting the writer to lenient permits the following:

  • Top-level values of any type. With strict writing, the top-level value must be an object or an array.
  • Numbers may be NaNs or infinities .

fun setLenient(lenient: Boolean)
setSerializeNulls
Link copied to clipboard

Sets whether object members are serialized when their value is null. This has no impact on array elements. The default is false.

fun setSerializeNulls(serializeNulls: Boolean)
setTag
Link copied to clipboard

Assigns the tag value using the given class key and value.

fun <T> setTag(clazz: Class<T>, value: T)
tag
Link copied to clipboard

Returns the tag value for the given class key.

fun <T> tag(clazz: Class<T>): T
value
Link copied to clipboard

Encodes {@code value} .

open fun value(value: Boolean): JsonWriter
open fun value(value: Double): JsonWriter
open fun value(value: Boolean): JsonWriter
open fun value(value: Number): JsonWriter
open fun value(value: String): JsonWriter
open fun value(value: Long): JsonWriter

Writes {@code source} directly without encoding its contents. Equivalent to {@code try * (BufferedSink sink = writer.valueSink()) { source.readAll(sink): }}

fun value(source: BufferedSource): JsonWriter
valueSink
Link copied to clipboard

Returns a BufferedSink into which arbitrary data can be written without any additional encoding. You must call before interacting with this {@code * JsonWriter} instance again.

Since no validation is performed, options like setSerializeNulls and other writer configurations are not respected.

open fun valueSink(): BufferedSink

Properties

stack
Link copied to clipboard
open val stack: Array<Any>