pub fn to_path<S>(value: S, path: &Path) -> Result<()>
where S: Serialize,
Expand description

Serialize struct S as a minified JSON with comments text of the given path. If you want to serialize as pretty formatted JSONC text, use to_path_pretty 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_minify.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(product, path).unwrap();
let after = std::fs::read_to_string(path).unwrap();
assert_eq!(before, after);