Function json_with_comments::ser::to_string_pretty
source · pub fn to_string_pretty<S>(value: S) -> Result<String>where
S: Serialize,
Expand description
Serialize struct S
as pretty formatted JSON with comments text.
If you want to serialize as minified JSONC text, use to_string
instead.
§Examples
use serde::Serialize;
#[derive(Serialize)]
struct Country<'a> {
name: &'a str,
code: u32,
regions: Vec<&'a str>,
}
let japan = Country {
name: "Japan",
code: 81,
regions: vec!["Hokkaido", "Kanto", "Kyushu-Okinawa"],
};
let jp = json_with_comments::to_string_pretty(japan).unwrap();
let pretty = r#"{
"name": "Japan",
"code": 81,
"regions": [
"Hokkaido",
"Kanto",
"Kyushu-Okinawa",
],
}"#;
assert_eq!(jp, pretty);