JSON::Builder
class JSON::Builder
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:
json/builder.crClass Method Summary
- .new(io : IO)
Creates a
JSON::Builder
that will write to the givenIO
.
Instance Method Summary
- #array(&block)
Writes the start of an array, invokes the block, and the writes the end of it.
- #bool(value : Bool)
Writes a boolean value.
- #document(&block)
- #end_array
Writes the end of an array.
- #end_document : Nil
Signals the end of a JSON document.
- #end_object
Writes the end of an object.
- #field(name, value)
Writes an object's field and value.
- #field(name, &block)
Writes an object's field and then invokes the block.
- #flush
Flushes the underlying
IO
. - #indent=(string : String)
Sets the indent string.
- #indent=(level : Int)
Sets the indent level (number of spaces).
- #null
Writes a
#null
value. - #number(number : Float)
Writes a float.
- #number(number : Int)
Writes an integer.
- #object(&block)
Writes the start of an object, invokes the block, and the writes the end of it.
- #raw(string : String)
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)
Writes a scalar value.
- #scalar(value : String)
Writes a scalar value.
- #start_array
Writes the start of an array.
- #start_document
Starts a document.
- #start_object
Writes the start of an object.
- #string(value)
Writes a string.
Instance methods inherited from class Reference
==(other : self)==(other) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Class methods inherited from class Reference
new new Instance methods inherited from class Object
!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml
Class Method Detail
def self.new(io : IO)Source
Creates a JSON::Builder
that will write to the given IO
.
Instance Method Detail
def array(&block)Source
Writes the start of an array, invokes the block, and the writes the end of it.
def document(&block)Source
def end_arraySource
Writes the end of an array.
def end_objectSource
Writes the end of an object.
def field(name, value)Source
def field(name, &block)Source
Writes an object's field and then invokes the block. This is equivalent of invoking #string(value)
and then invoking the block.
def object(&block)Source
Writes the start of an object, invokes the block, and the writes the end of it.
def raw(string : String)Source
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 start_arraySource
Writes the start of an array.
def start_documentSource
Starts a document.
def start_objectSource
Writes the start of an object.
def string(value)Source
Writes a string. The given value is first converted to a String
by invoking to_s
on it.
This method can also be used to write the name of an object field.
© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/JSON/Builder.html