[
]
(https://travis-ci.org/tomjakubowski/json_macros)
let properties = json! {
"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}
};
let menu_value = properties.find_path(["menu", "value"])
.map(|x| x.as_string());
assert_eq!(menu_value, Some("File"));Use JSON-like literals in Rust to construct serde_json Values
or rustc-serialize Json values.
Because json_macros is a compiler plugin, it's only compatible with
the Rust nightly channel.
Depending on your project's needs, you may ask json_macros to
generate code that constructs serde_json values or code that
constructs rustc-serialize values.
By default, json_macros generates code for rustc-serialize. In a
future release, the default may switch to serde_json, but
json_macros should be at least optionally compatible with
rustc-serialize for as long as that crate is supported.
To use json_macros with rustc-serialize, add both packages as
dependencies to your Cargo.toml.
[dependencies]
json_macros = "^0.3"
rustc-serialize = "^0.3"Your crate will also need to link with rustc_serialize and use it
in any submodule that uses the json!() macro.
extern crate rustc_serialize;
// ...
mod foo {
use rustc_serialize;
// ...
}#![feature(plugin)]
#![plugin(json_macros)]
extern crate rustc_serialize;
pub fn main() {
let x = 123i32;
println!("{}", json!({ // object literal
"foo": "foooooo", // string literal keys and values
"bar": [true, null, 123, 123.4], // array, boolean, null, numeric literals
"quux": { // nest as deeply as you like
"a": [1, 2, 3, 4],
"b": { "a": null },
"c": false
},
"waldo": (192 - x) // wrap in parens to splice ToJson expressions directly
}).pretty().to_string());
}To use json_macros with serde_json, add both packages as
dependencies to your Cargo.toml. Enable the with-serde_json
feature for json_macros and disable the default features so as to
not depend on rustc-serialize.
[dependencies]
rustc-serialize = "^0.3"
[dependencies.json_macros]
version = "^0.3"
default-features = false
features = ["with-serde"]Your crate will also need to link with serde_json and use it in
any submodule that uses the json!() macro.
extern crate serde_json;
// ...
mod foo {
use serde_json;
// ...
}#![feature(plugin)]
#![plugin(json_macros)]
extern crate serde_json;
pub fn main() {
let x = 123i32;
println!("{}", serde_json::to_string_pretty(&json!({ // object literal
"foo": "foooooo", // string literal keys and values
"bar": [true, null, 123, 123.4], // array, boolean, null, numeric literals
"quux": { // nest as deeply as you like
"a": [1, 2, 3, 4],
"b": { "a": null },
"c": false
},
"waldo": (192 - x) // wrap in parens to splice ToJson expressions directly
})).unwrap());
}