Macro serde_json::json [] [src]

macro_rules! json {
    ($($json:tt)+) => { ... };
}

Construct a serde_json::Value from a JSON literal.

let value = json!({
    "code": 200,
    "success": true,
    "payload": {
        "features": [
            "serde",
            "json"
        ]
    }
});

Any variable or expression that implements Serde's Serialize trait can be interpolated into the JSON literal just by referring to it.

let code = 200;
let features = vec!["serde", "json"];

let value = json!({
   "code": code,
   "success": code == 200,
   "payload": {
       features[0]: features[1]
   }
});

Trailing commas are allowed inside both arrays and objects.

let value = json!([
    "notice",
    "the",
    "trailing",
    "comma -->",
]);