module JSON
JSON: parsing into JSON::Value and generation from any value.
require "json"
doc = JSON.parse('{"name": "quarz", "tags": ["lang", "vm"]}')
doc["name"].as_s # => "quarz"
doc["tags"][1].as_s # => "vm"
doc["missing"] # raises KeyError
JSON.generate({ a: 1, b: [true, nil] }) # => "{\"a\":1,\"b\":[true,null]}"
JSON.generate accepts nil, booleans, numbers, strings, symbols, arrays,
hashes, sets, data values and JSON::Value. Other objects are
generated from their fields, unless they define to_json -> String.
<std:json>:15
Classes and modules
JSON::GeneratorErrorRaised by JSON.generate on a value JSON cannot represent, such as a Float infinity or NaN.JSON::ParseErrorRaised by JSON.parse on malformed input; the message gives the line and column.JSON::ValueA parsed JSON document, its variants named as Rust's serde_json names them. A Number holds any number: an Int, an Integer when too large for an Int (kept exact), or a Float when it has a fraction or an exponent. Inside the enum the names are the variants: the types are ::Bool, ::String and so on.
Constants
VERSION: String
The version of the library.
<std:json>:17
Class methods
def self.parse(text: String) -> JSON::Value
The JSON document text as a JSON::Value; whitespace around the
value is allowed.
Raises
JSON::ParseError on malformed input (anything but one value
included), with the line and column in its message.
JSON.parse(" null ") # => JSON::Value::Null
JSON.parse("[1,") # raises JSON::ParseError
JSON.parse("1 2") # raises JSON::ParseError
<std:json>:328
def self.generate(value: Object) -> String
The compact JSON text of a value.
<std:json>:331
def self.pretty_generate(value: Object) -> String
Indented JSON text (two spaces per level).
<std:json>:334
def self.dump(value: Object) -> String
The compact JSON text of a value: JSON.generate under Ruby's other
name.
<std:json>:338
def self.load(text: String) -> JSON::Value
Parses a document: JSON.parse under Ruby's other name.
<std:json>:341