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