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

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