# class JSON::Builder

- JSON::Builder
- Reference
- Object

##
Overview

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).

##
Defined in:

big/json.cr
json/builder.cr

##
Constructors

- .new(io : IO)

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

##
Instance Method Summary

- #array(&)

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

- #bool(value : Bool) : Nil

  Writes a boolean value.

- #document(&)
- #end_array : Nil

  Writes the end of an array.

- #end_document : Nil

  Signals the end of a JSON document.

- #end_object : Nil

  Writes the end of an object.

- #field(name : _, value : _) : Nil

  Writes an object's field and value.

- #field(name, &)

  Writes an object's field and then invokes the block.

- #flush : Nil

  Flushes the underlying `IO`.

- #indent=(string : String) : String | Nil

  Sets the indent string.

- #indent=(level : Int) : String | Nil

  Sets the indent level (number of spaces).

- #max_nesting : Int32

  By default the maximum nesting of arrays/objects is 99.

- #max_nesting=(max_nesting : Int32)

  By default the maximum nesting of arrays/objects is 99.

- #next_is_object_key? : Bool

  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.

- #null : Nil

  Writes a `#null` value.

- #number(number : Int) : Nil

  Writes an integer.

- #number(number : Float) : Nil

  Writes a float.

- #number(number : BigDecimal) : Nil

  Writes a big decimal.

- #object(&)

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

- #raw(string : String) : Nil

  Writes a raw value, considered a scalar, directly into the IO without processing.

- #scalar(value : Nil)

  Writes a scalar value.

- #scalar(value : Bool)

  Writes a scalar value.

- #scalar(value : Int | Float) : Nil

  Writes a scalar value.

- #scalar(value : String) : Nil

  Writes a scalar value.

- #start_array : Nil

  Writes the start of an array.

- #start_document : Nil

  Starts a document.

- #start_object : Nil

  Writes the start of an object.

- #string(value : _) : Nil

  Writes a string with the content of `value`.

- #string(& : IO -> ) : Nil

  Writes a string with the contents written to the yielded `IO`.

### Instance methods inherited from class `Reference`

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==, dup dup, hash(hasher) hash, initialize initialize, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference) : Bool
same?(other : Nil) same?, to_s(io : IO) : Nil to_s

### Constructor methods inherited from class `Reference`

new new, unsafe_construct(address : Pointer, *args, **opts) : self unsafe_construct

### Class methods inherited from class `Reference`

pre_initialize(address : Pointer) pre_initialize

### Instance methods inherited from class `Object`

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(collection : Object) : Bool
in?(*values : Object) : Bool in?, inspect(io : IO) : Nil
inspect : String inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String to_json, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json, to_s(io : IO) : Nil
to_s : String to_s, to_yaml(io : IO) : Nil
to_yaml : String to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

### Class methods inherited from class `Object`

from_json(string_or_io : String | IO, root : String)
from_json(string_or_io : String | IO) from_json, from_yaml(string_or_io : String | IO) from_yaml

### Macros inherited from class `Object`

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

##
Constructor Detail

def self.new(io : IO) #

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

##
Instance Method Detail

def array(&) #

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

def bool(value : Bool) : Nil #

Writes a boolean value.

def document(&) #
def end_array : Nil #

Writes the end of an array.

def end_document : Nil #

Signals the end of a JSON document.

def end_object : Nil #

Writes the end of an object.

def 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.

def field(name, &) #

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

def flush : Nil #

Flushes the underlying `IO`.

def indent=(string : String) : String | Nil #

Sets the indent string.

def indent=(level : Int) : String | Nil #

Sets the indent level (number of spaces).

def 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.

def 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.

def next_is_object_key? : Bool #

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.

def null : Nil #

Writes a `#null` value.

def number(number : Int) : Nil #

Writes an integer.

def number(number : Float) : Nil #

Writes a float.

def number(number : BigDecimal) : Nil #

Writes a big decimal.

def object(&) #

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

def 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.

def scalar(value : Nil) #

Writes a scalar value.

def scalar(value : Bool) #

Writes a scalar value.

def scalar(value : Int | Float) : Nil #

Writes a scalar value.

def scalar(value : String) : Nil #

Writes a scalar value.

def start_array : Nil #

Writes the start of an array.

def start_document : Nil #

Starts a document.

def start_object : Nil #

Writes the start of an object.

def 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.

def string(& : IO -> ) : Nil #

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.