JSON
Unlike some other languages, Ruby does not have first class support for JSON so we use Array and Hash to make handling JSON in your Vert.x applications a bit easier.
JSON objects
Ruby hashes represents JSON objects.
A JSON object is basically just a map which has string keys and values can be of one of the JSON supported types (string, number, boolean).
JSON objects also support nil values.
Creating JSON objects
Empty JSON objects can be created with the default constructor.
You can create a JSON object from a string JSON representation as follows:
require 'json'
object = JSON.parse('"foo":"bar"')
Encoding the JSON object to a String
You use JSON.generate
to encode the object to a String form.
require 'json'
json = JSON.generate(object)
JSON arrays
Ruby arrays represents JSON arrays.
A JSON array is a sequence of values (string, number, boolean).
JSON arrays can also contain null values.
Creating JSON arrays
Empty JSON arrays can be created with the default constructor.
You can create a JSON array from a string JSON representation as follows:
require 'json'
object = JSON.parse('[1,2,3]')
Encoding the JSON array to a String
You use JSON.generate
to encode the array to a String form.
require 'json'
json = JSON.generate(array)