JsonGenerator

grapple.json.JsonGenerator
See theJsonGenerator companion object
trait JsonGenerator extends AutoCloseable

Defines JSON generator.

import java.io.StringWriter

import scala.language.implicitConversions

import grapple.json.{ *, given }

val buf = StringWriter()
val out = JsonGenerator(buf)

try
 out.writeStartObject()          // start root object
 out.write("id", 1000)
 out.write("name", "lupita")
 out.writeStartArray("groups")   // start nested array
 out.write("lupita")
 out.write("admin")
 out.write("sudoer")
 out.writeEnd()                  // end nested array
 out.writeStartObject("info")    // start nested object
 out.write("home", "/home/lupita")
 out.write("storage", "8 GiB")
 out.writeEnd()                  // end nested object
 out.writeEnd()                  // end root object
 out.flush()

 val json = Json.parse(buf.toString)
 assert { json("id") == JsonNumber(1000) }
 assert { json("name") == JsonString("lupita") }
 assert { json("groups") == Json.arr("lupita", "admin", "sudoer") }
 assert { json("info") == Json.obj("home" -> "/home/lupita", "storage" -> "8 GiB") }
finally
 out.close()

Attributes

See also
Companion
object
Graph
Supertypes
trait AutoCloseable
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def close(): Unit

Closes writer.

Closes writer.

Attributes

def flush(): Unit

Flushes writer.

Flushes writer.

Attributes

def write(key: String, value: JsonValue): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(value: JsonValue): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def writeEnd(): JsonGenerator.this.type

Writes closing brace or bracket based on current context.

Writes closing brace or bracket based on current context.

Attributes

def writeStartArray(): JsonGenerator.this.type

Writes opening bracket to start array context.

Writes opening bracket to start array context.

Attributes

def writeStartArray(key: String): JsonGenerator.this.type

Writes field declaration and opening bracket to start array context.

Writes field declaration and opening bracket to start array context.

Value parameters

key

object key

Attributes

def writeStartObject(): JsonGenerator.this.type

Writes opening brace to start object context.

Writes opening brace to start object context.

Attributes

def writeStartObject(key: String): JsonGenerator.this.type

Writes field declaration and opening brace to start object context.

Writes field declaration and opening brace to start object context.

Value parameters

key

object key

Attributes

Concrete methods

def write(key: String, value: String): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: Boolean): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: Int): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: Long): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: Float): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: Double): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: BigInt): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(key: String, value: BigDecimal): JsonGenerator.this.type

Writes field to object context.

Writes field to object context.

Attributes

def write(value: String): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: Boolean): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: Int): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: Long): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: Float): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: Double): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: BigInt): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def write(value: BigDecimal): JsonGenerator.this.type

Writes value to array context.

Writes value to array context.

Attributes

def writeNull(key: String): JsonGenerator.this.type

Writes field with null value to object context.

Writes field with null value to object context.

Attributes

def writeNull(): JsonGenerator.this.type

Writes null value to array context.

Writes null value to array context.

Attributes