enum JSON::Value

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

<std:json>:38

Variants

variant JSON::Value::Null

JSON null.

<std:json>:40

variant JSON::Value::Bool(value: Bool)

JSON true or false.

<std:json>:42

variant JSON::Value::Number(value: Int | Integer | Float)

A JSON number: an Int, an Integer when too large for an Int, or a Float when it has a fraction or an exponent.

<std:json>:45

variant JSON::Value::String(value: String)

A JSON string.

<std:json>:47

variant JSON::Value::Array(items: Array[JSON::Value])

A JSON array.

<std:json>:49

variant JSON::Value::Object(members: Hash[String, JSON::Value])

A JSON object, its members in document order.

<std:json>:51

Methods

def null? -> Bool

Whether the value is Null.

<std:json>:54

def bool? -> Bool

Whether the value is a Bool.

<std:json>:62

def number? -> Bool

Whether the value is a Number.

<std:json>:70

def string? -> Bool

Whether the value is a String.

<std:json>:78

def array? -> Bool

Whether the value is an Array.

<std:json>:86

def object? -> Bool

Whether the value is an Object.

<std:json>:94

def [](key: String) -> JSON::Value

The member named key of an object.

Raises

KeyError when the object has no such member, TypeError when the value is not an Object.

<std:json>:107

def [](index: Int) -> JSON::Value

The element at index of an array; a negative index counts from the end.

Raises

IndexError when index is out of range, TypeError when the value is not an Array.

JSON.parse("[1, 2]")[-1]   # => JSON::Value::Number(value: 2)
JSON.parse("[1, 2]")[5]    # raises IndexError

<std:json>:126

def []?(key: String) -> JSON::Value?

Member of an object, or nil when it is missing or the value is not an Object.

doc = JSON.parse('{"a": 1}')
doc["a"]?          # => JSON::Value::Number(value: 1)
doc["b"]?          # => nil
doc[0]?            # => nil

<std:json>:142

def []?(index: Int) -> JSON::Value?

Element of an array, or nil when out of range or the value is not an Array. A negative index counts from the end.

<std:json>:151

def as_bool -> Bool

The boolean of a Bool; raises TypeError on any other value.

<std:json>:159

def as_i -> Int

The number as an Int (a Float truncated); OverflowError when it does not fit.

<std:json>:168

def as_integer -> Integer

The number as an Integer, exact whatever its size (a Float truncated).

<std:json>:177

def as_f -> Float

The number as a Float; raises TypeError on anything but a Number.

<std:json>:186

def as_s -> String

The string of a String; raises TypeError on any other value.

<std:json>:194

def as_a -> Array[JSON::Value]

The elements of an Array; raises TypeError on any other value.

<std:json>:202

def as_h -> Hash[String, JSON::Value]

The members of an Object; raises TypeError on any other value.

<std:json>:210

def as_bool? -> Bool?

The boolean of a Bool, or nil for any other value.

<std:json>:218

def as_i? -> Int?

The number as an Int (a Float truncated), or nil when the value is not a Number. A number too large for an Int still raises OverflowError.

JSON.parse("2.5").as_i?     # => 2
JSON.parse('"2"').as_i?     # => nil

<std:json>:233

def as_integer? -> Integer?

The number as an Integer (a Float truncated), or nil when the value is not a Number.

<std:json>:242

def as_f? -> Float?

The number as a Float, or nil when the value is not a Number.

<std:json>:250

def as_s? -> String?

The string of a String, or nil for any other value.

<std:json>:258

def as_a? -> Array[JSON::Value]?

The elements of an Array, or nil for any other value.

<std:json>:266

def as_h? -> Hash[String, JSON::Value]?

The members of an Object, or nil for any other value.

<std:json>:274

def size -> Int

The number of elements of an array or members of an object.

Raises

TypeError for any other value.

<std:json>:286

def to_json -> String

The compact JSON text of the value, as JSON.generate gives it.

JSON.parse("[1, true, null]").to_json   # => "[1,true,null]"

<std:json>:299

def to_s -> String

The compact JSON text of the value, as #to_json.

<std:json>:302