W3cubDocs

/Crystal 0.31

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

Constructors

Instance Method Summary

Constructor 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 bool(value : Bool)Source

Writes a boolean value.

def document(&block)Source

def end_arraySource

Writes the end of an array.

def end_document : NilSource

Signals the end of a JSON document.

def end_objectSource

Writes the end of an object.

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 field(name, value)Source

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

def flushSource

Flushes the underlying IO.

def indent=(level : Int)Source

Sets the indent level (number of spaces).

def indent=(string : String)Source

Sets the indent string.

def max_nesting : Int32Source

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

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? : BoolSource

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 nullSource

Writes a #null value.

def number(number : Float)Source

Writes a float.

def number(number : Int)Source

Writes an integer.

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 scalar(value : Nil)Source

Writes a scalar value.

def scalar(value : Bool)Source

Writes a scalar value.

def scalar(value : Int | Float)Source

Writes a scalar value.

def scalar(value : String)Source

Writes a scalar value.

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–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/JSON/Builder.html