Defines JSON generator.
import java.io.StringWriter
import little.json.*
import little.json.Implicits.given
import scala.language.implicitConversions
val buf = StringWriter()
val out = JsonGenerator(buf)
try
out.writeStartObject() // start root object
out.write("id", 1000)
out.write("name", "jza")
out.writeStartArray("groups") // start nested array
out.write("jza")
out.write("adm")
out.write("sudo")
out.writeEnd() // end nested array
out.writeStartObject("info") // start nested object
out.write("home", "/home/jza")
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("jza") }
assert { json("groups") == Json.arr("jza", "adm", "sudo") }
assert { json("info") == Json.obj("home" -> "/home/jza", "storage" -> "8 GiB") }
finally
out.close()
- See also
- Companion
- object
trait AutoCloseable
class Object
trait Matchable
class Any
Value members
Abstract methods
Writes closing brace or bracket based on current context.
Writes closing brace or bracket based on current context.
Writes field declaration and opening bracket to start array context.
Writes field declaration and opening bracket to start array context.
- Value Params
- name
field name
Writes field declaration and opening brace to start object context.
Writes field declaration and opening brace to start object context.
- Value Params
- name
field name