Function json_with_comments::ser::to_file_pretty
source · pub fn to_file_pretty(value: impl Serialize, file: &mut File) -> Result<()>
Expand description
Serialize struct S
as a pretty formatted JSON with comments text of the given file.
If you want to serialize as minified JSONC text, use to_file
instead.
§Examples
use serde::Serialize;
#[derive(Serialize)]
struct Product {
name: String,
price: u32,
}
let path = std::path::Path::new("tests/data/product_pretty.jsonc");
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut file = std::fs::File::create(path).unwrap();
let product = Product { name: "candy".to_string(), price: 100 };
json_with_comments::to_file_pretty(product, &mut file).unwrap();
let pretty = r#"{
"name": "candy",
"price": 100,
}"#;
assert_eq!(std::fs::read_to_string(path).unwrap(), pretty);