json!() { /* proc-macro */ }Expand description
Lints and properly formats a JSON object, array, or value.
This proc-macro supports:
- all literals (integers, floats,
&str) String- any expression that evaluates to a
impl Display
If you are looking for custom serialization traits, macros,
and functions, use serde_json and serde instead.
Examples:
Serializing an object:
// You have to have the `ToJson` trait restriction since
// the json! macro uses ToJson. Should a struct not
// implement ToJson, you can use the derive macro.
fn obj<J: json_proc::ToJson>(input: J) -> String {
json!({
"hello": "world!",
thisDidntNeedQuotes: "wow!",
// this will essentially become `format!("{}", input.to_json_string())`
anExpression: input
})
}Serializing an array:
fn arr<J: json_proc::ToJson>(input: J) -> String {
json!([
input,
(2 + 11) as f32 / 2.0,
"literal"
])
}