Enum Value
A value of a valid JSON datatype.
This is what is returned from the parser, and what can be serialized back into JSON data. You may want to extract your parsed data from this enum to a more specialized data structure, to make working with it more ergonomic.
scoped enum Value { Object(Hash[String, Value]), Array(List[Value]), String(String), Number(Double), Boolean(Boolean), Null }
define serialize(format: *Boolean, non_finite: *Boolean): String
Converts self
to a String
containing JSON data.
If format
is true
, the output JSON will be auto-formatted with consistent spacing and indentation. If you need your JSON to be human-readable, this may be desirable - however, if working with a large amount of data, you may want to keep it disabled to keep the size of the output JSON small.
If non_finite
is true
(the default), non-finite values will be serialized.
define serialize_file(path: String, format: *Boolean, non_finite: *Boolean)
Utility function to write serialized JSON data to a file.
format
and non_finite
function identically to how they do in Value.encode
.
define as_hash: Hash[String, Value]
Returns the internal Hash
contained within self
.
ValueError
ifself
is not of typeValue.Object
.
define as_list: List[Value]
Returns the internal List
contained within self
.
ValueError
ifself
is not of typeValue.Array
.
define as_string: String
Returns the internal String
contained within self
.
ValueError
ifself
is not of typeValue.String
.
define as_double: Double
define as_integer: Integer
Returns the internal Double
contained within self
, casted to an Integer
.
ValueError
ifself
is not of typeValue.Number
.ValueError
ifself
is not an integer / has a fractional part.
define as_boolean: Boolean
Returns the internal Boolean
contained within self
.
ValueError
ifself
is not of typeValue.Boolean
.
define get(key: String): Value
Returns the Value
corresponding to key
in self
.
ValueError
ifself
is not of typeValue.Object
.IndexError
ifkey
is not inself
.
define at(index: Integer): Value
Returns the Value
at index
in self
.
ValueError
ifself
is not of typeValue.Array
.IndexError
ifindex
is out of range.
define each(fn: Function(Value))
Calls fn
for each Value
in self
.
ValueError
ifself
is not of typeValue.Array
orValue.Object
.