module JSON::Builder

Overview

The JSON::Builder module adds two methods, #json_object and #json_array to all IOs so generating JSON by streaming to an IO is easy and convenient.

Example

require "json"

result = String.build do |io|
  io.json_object do |object|
    object.field "address", "Crystal Road 1234"
    object.field "location" do
      io.json_array do |array|
        array << 12.3
        array << 34.5
      end
    end
  end
end
result # => %({"address":"Crystal Road 1234","location":[12.3,34.5]})

Direct including types

Defined in:

json/to_json.cr

Instance Method Summary

Instance Method Detail

def json_array(&block) #

Writes a JSON array to the given IO. Yields a JSON::ArrayBuilder.


[View source]
def json_object(&block) #

Writes a JSON object to the given IO. Yields a JSON::ObjectBuilder.


[View source]