std::parse
| Item | Summary |
|---|---|
from_json | Parses text as JSON and returns it as a Value |
to_json | Returns value written as compact JSON, with no spaces or line breaks |
Functions
from_json
fn from_json(text: str) -> Value!
Parses text as JSON and returns it as a Value. Raises if text isn’t valid JSON, with a
message giving the line and column of the problem.
use std::parse;
let data = parse::from_json("{\"name\": \"ada\", \"hp\": 30}")!;
let name = data.as_dict()!["name"]!.as_str()!; // "ada"
to_json
fn to_json(value: Value) -> str!
Returns value written as compact JSON, with no spaces or line breaks. Object keys are written
in alphabetical order, and a whole number has no decimal point (3.0 is written as 3). A
NaN or infinite number, which JSON can’t represent, is written as null.
use std::parse;
use std::parse::Value;
let player = Value::Object(~{
name = Value::String("ada"),
level = Value::Number(3.0),
});
let text = parse::to_json(player)!; // "{\"level\":3,\"name\":\"ada\"}"
Types
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.