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