class

JSON::Builder

Inherits Reference / Object

A JSON builder generates valid JSON.

A JSON::Error is raised if attempting to generate an invalid JSON (for example, if invoking end_array without a matching start_array, or trying to use a non-string value as an object's field name).

Constructors

new(io : IO)

Creates a JSON::Builder that will write to the given IO.

Source

Instance methods

array

Writes the start of an array, invokes the block, and the writes the end of it.

Source
bool(value : Bool) : Nil

Writes a boolean value.

Source
document
Source
end_array

Writes the end of an array.

Source
end_document

Signals the end of a JSON document.

Source
end_object

Writes the end of an object.

Source
field(name : _, value : _) : Nil

Writes an object's field and value. The field's name is first converted to a String by invoking to_s on it.

Source
field(name, &)

Writes an object's field and then invokes the block. This is equivalent of invoking string(value) and then invoking the block.

Source
flush

Flushes the underlying IO.

Source
indent=(string : String) : String | Nil

Sets the indent string.

Source
indent=(level : Int) : String | Nil

Sets the indent level (number of spaces).

Source
max_nesting

By default the maximum nesting of arrays/objects is 99. Nesting more than this will result in a JSON::Error. Changing the value of this property allows more/less nesting.

Source
max_nesting=(max_nesting : Int32)

By default the maximum nesting of arrays/objects is 99. Nesting more than this will result in a JSON::Error. Changing the value of this property allows more/less nesting.

Source
next_is_object_key?

Returns true if the next thing that must pushed into this builder is an object key (so a string) or the end of an object.

Source
null

Writes a null value.

Source
number(number : Int) : Nil

Writes an integer.

Source
number(number : Float) : Nil

Writes a float.

Source
number(number : BigDecimal) : Nil

Writes a big decimal.

Source
object

Writes the start of an object, invokes the block, and the writes the end of it.

Source
raw(string : String) : Nil

Writes a raw value, considered a scalar, directly into the IO without processing. This is the only method that might lead to invalid JSON being generated, so you must be sure that string contains a valid JSON string.

Source
scalar(value : Nil)

Writes a scalar value.

Source
scalar(value : Bool)

Writes a scalar value.

Source
scalar(value : Int | Float) : Nil

Writes a scalar value.

Source
scalar(value : String) : Nil

Writes a scalar value.

Source
start_array

Writes the start of an array.

Source
start_document

Starts a document.

Source
start_object

Writes the start of an object.

Source
string(value : _) : Nil

Writes a string with the content of value. The payload is stringified via to_s(IO) and escaped for JSON representation.

JSON.build do |builder|
  builder.string("foo")
end # => %("foo")
JSON.build do |builder|
  builder.string([1, 2])
end # => %("[1, 2]")

This method can also be used to write the name of an object field.

Source
string

Writes a string with the contents written to the yielded IO. The payload gets escaped for JSON representation.

JSON.build do |builder|
  builder.string do |io|
    io << "foo"
    io << [1, 2]
  end # => %("foo[1, 2]")
end

This method can also be used to write the name of an object field.

Source

Nested types