macro_rules! json { (true) => { ... }; (false) => { ... }; (null) => { ... }; ([]) => { ... }; ({}) => { ... }; ($($json:tt)+) => { ... }; }
Expand description
Construct a sonic_rs::Value
from a JSON literal.
let value = json!({
"code": 200,
"success": true,
"payload": {
"features": [
"serde",
"json"
],
"homepage": null
}
});
Variables or expressions can be interpolated into the JSON literal. Any type
interpolated into an array element or object value must implement Serde’s
Serialize
trait, while any type interpolated into a object key must
implement AsRef<str>
. If the Serialize
implementation of the
interpolated type decides to fail, or if the interpolated type contains a
map with non-string keys, the json!
macro will panic.
let code = 200;
let features = vec!["sonic_rs", "json"];
let value = json!({
"code": code,
"success": code == 200,
"payload": {
"features": features,
features[0]: features[1]
}
});
assert_eq!(value["code"], 200);
assert_eq!(value["payload"]["features"][0], "sonic_rs");
Trailing commas are allowed inside both arrays and objects.
let value = json!(["notice", "the", "trailing", "comma -->",]);