JSONWriter

open class JSONWriter(writer: Writer = StringWriter(), prettyPrint: Boolean = false) : AutoCloseable

A stateful JSON writer that produces ASCII-only documents that adhere strictly to ECMA 404: "The JSON Data Interchange Format".

Author

Todd L Smith

Richard Arriaga

See also

Parameters

writer

The target for the raw JSON document.

prettyPrint

true indicates the JSON should be pretty-printed; false indicates the JSON should be minified.

Constructors

Link copied to clipboard
fun JSONWriter(writer: Writer = StringWriter(), prettyPrint: Boolean = false)

Construct a new JSONWriter.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
inline fun <R> at(key: String, action: JSONWriter.() -> R): R

Write the given non-null String as the name of an entity, then evaluate the action, generally to write the associated value.

Link copied to clipboard
open override fun close()
Link copied to clipboard
fun contents(): String

Answer the accumulated String contents of the JSONWriter.

Link copied to clipboard
fun endArray()

Write an array ending to the underlying document writer.

Link copied to clipboard
fun endObject()

Write an object ending to the underlying document writer.

Link copied to clipboard
fun flush()

Flush any buffered data to the underlying document writer.

Link copied to clipboard
fun format(pattern: String?, vararg args: Any)

Write the specified String to the underlying document writer as a JSON string. All non-ASCII characters are encoded as Unicode escape sequences, so only ASCII characters will be written.

Link copied to clipboard
fun startArray()

Write an array beginning to the underlying document writer.

Link copied to clipboard
fun startObject()

Write an object beginning to the underlying document writer.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
open fun write(value: BigDecimal)

Write the specified BigDecimal to the underlying document writer as a JSON number.

fun write(value: BigInteger)

Write the specified BigInteger to the underlying document writer as a JSON number.

open fun write(value: Boolean)

Write the specified Boolean to the underlying document writer as a JSON boolean.

fun write(value: Double)

Write the specified Double to the underlying document writer as a JSON number. Use JSON 5 extensions (and an additional NaN extension).

fun write(value: Float)

Write the specified Float to the underlying document writer as a JSON number.

fun write(value: Int)

Write the specified Int to the underlying document writer as a JSON number.

fun write(value: Long)

Write the specified Long to the underlying document writer as a JSON number.

fun write(value: String?)

Write the specified String to the underlying document writer as a JSON string. All non-ASCII characters are encoded as Unicode escape sequences, so only ASCII characters will be written.

fun write(friendly: JSONFriendly)

Write the specified JSON-friendly value to the underlying document writer.

fun write(other: JSONWriter)

Write the contents of the specified JSONWriter to the underlying document as a JSON value. This is only permitted whenever an arbitrary JSON value would be permitted.

Link copied to clipboard
inline fun <R> writeArray(action: JSONWriter.() -> R): R

Write an array, using an action to supply the contents.

fun writeArray(values: Iterable<JSONFriendly>)
fun writeArray(values: Iterator<JSONFriendly>)
fun writeArray(values: Sequence<JSONFriendly>)

Write an array of JSON-friendly values.

Link copied to clipboard
fun writeBooleans(values: Iterable<Boolean>)
fun writeBooleans(values: Iterator<Boolean>)
fun writeBooleans(values: Sequence<Boolean>)

Write an array of boolean values.

Link copied to clipboard
fun writeDoubles(values: Iterable<Double>)
fun writeDoubles(values: Iterator<Double>)
fun writeDoubles(values: Sequence<Double>)

Write an array of double values.

Link copied to clipboard
fun writeFloats(values: Iterable<Float>)
fun writeFloats(values: Iterator<Float>)
fun writeFloats(values: Sequence<Float>)

Write an array of float values.

Link copied to clipboard
fun writeInts(values: Iterable<Int>)
fun writeInts(values: Iterator<Int>)
fun writeInts(values: Sequence<Int>)

Write an array of integer values.

Link copied to clipboard
fun writeLongs(values: Iterable<Long>)
fun writeLongs(values: Iterator<Long>)
fun writeLongs(values: Sequence<Long>)

Write an array of long values.

Link copied to clipboard
fun <Key, Value> writeMap(map: Map<Key, Value>, pairCreator: (Key, Value) -> Pair<String, JSONFriendly>)

Write a map keyed by an arbitrary type to another arbitrary type as a JSONObject with the map keys transformed into JSONObject keys using the key creator lambda and the corresponding map values transformed into JSONFriendly using the value creator lambda as the JSONObject field values.

fun <Key> writeMap(map: Map<Key, JSONFriendly>, keyCreator: (Key) -> String)

Write a map keyed by an arbitrary type to JSONFriendly as a JSONObject with the map keys transformed into JSONObject keys using the key creator lambda and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun <Key, Value> writeMapAsArrayOfPairs(map: Map<Key, Value>, pairTransformer: (Key, Value) -> Pair<JSONFriendly, JSONFriendly>)

Write a map keyed by an arbitrary type to another arbitrary type as a JSONArray of JSONArray pairs with the map keys as the first element in the JSONArray pair and the value as the second element in the JSONArray pair transformed into JSONFriendly - JSONFriendly pairs by the provided lambda.

Link copied to clipboard
fun writeMapBoolean(map: Map<String, Boolean>)

Write a String to Boolean as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapBooleans(map: Map<String, Iterable<Boolean>>)

Write a String to Iterable of Boolean as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapDouble(map: Map<String, Double>)

Write a String to Double as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapDoubles(map: Map<String, Iterable<Double>>)

Write a String to Iterable of Double as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapFloat(map: Map<String, Float>)

Write a String to Float as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapFloats(map: Map<String, Iterable<Float>>)

Write a String to Iterable of Float as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapInt(map: Map<String, Int>)

Write a String to Int as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapInts(map: Map<String, Iterable<Int>>)

Write a String to Iterable of Int as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapJsonFriendlies(map: Map<String, Iterable<JSONFriendly>>)

Write a String to Iterable of JSONFriendly as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapJsonFriendly(map: Map<String, JSONFriendly>)

Write a String to JSONFriendly as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapLong(map: Map<String, Long>)

Write a String to Long as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapLongs(map: Map<String, Iterable<Long>>)

Write a String to Iterable of Long as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapString(map: Map<String, String>)

Write a String to String as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
fun writeMapStrings(map: Map<String, Iterable<String>>)

Write a String to Iterable of String as a JSONObject with the map keys as the JSONObject keys and the corresponding map values as the JSONObject field values.

Link copied to clipboard
open fun writeNull()

Write a JSON null to the underlying document writer.

Link copied to clipboard
inline fun <R> writeObject(action: JSONWriter.() -> R): R

Write an object, using an action to supply the contents.

Link copied to clipboard
fun <First, Second> writePairsAsArrayOfPairs(pairs: Iterator<Pair<First, Second>>, pairTransformer: (First, Second) -> Pair<JSONFriendly, JSONFriendly>)

Write an Iterator of Pairs keyed by an arbitrary type to another arbitrary type as a JSONArray of JSONArray pairs with the map keys as the first element in the JSONArray pair and the value as the second element in the JSONArray pair transformed into JSONFriendly - JSONFriendly pairs by the provided lambda.

Link copied to clipboard
fun writeStrings(values: Iterable<String>)
fun writeStrings(values: Iterator<String>)
fun writeStrings(values: Sequence<String>)

Write an array of string values.