Value
enum Value {
/// JSON's `null`.
Null,
/// `true` or `false`.
Bool(bool),
/// A number. JSON doesn't tell integers and decimals apart, and both are stored as a `float`.
Number(float),
/// A string.
String(str),
/// An array of values.
Array([Value]),
/// An object, keyed by string. The keys of an object from `from_json` are in no particular
/// order.
Object(~{Value}),
}
A JSON value, as from_json returns it and to_json takes it. Match on it to handle each kind
of value, or use the as_ methods to get the kind you expect.
use std::parse;
use std::parse::Value;
let data = parse::from_json("{\"alive\": true}")!;
let alive = match data.as_dict()!["alive"]! {
Value::Bool(b) => b,
_ => false,
};
Implementations
as_dict
fn as_dict(self) -> ~{Value}?
Returns the entries of an Object, or null for any other kind of value. The keys are in no
particular order.
use std::parse;
let data = parse::from_json("{\"hp\": 30}")!;
let hp = data.as_dict()!["hp"]!.as_float()!; // 30.0
as_array
fn as_array(self) -> [Value]?
Returns the elements of an Array, or null for any other kind of value.
use std::parse;
let list = parse::from_json("[1, 2, 3]")!;
let n = list.as_array()!.len(); // 3
as_str
fn as_str(self) -> str?
Returns the text of a String, or null for any other kind of value.
use std::parse;
let a = parse::from_json("\"hi\"")!.as_str(); // "hi"
let b = parse::from_json("12")!.as_str(); // null
as_float
fn as_float(self) -> float?
Returns the number in a Number, or null for any other kind of value. Every JSON number
comes back as a float. Call to_int on the result when you need an int.
use std::parse;
let a = parse::from_json("12")!.as_float(); // 12.0
let b = parse::from_json("12")!.as_float()!.to_int(); // 12