I am serializing some data to a file in json format with serde:
let json = serde_json::to_string_pretty(&data).unwrap();
std::fs::write(&path, &json).expect("Unable to write json file");
What would be the best way to add a trailing linefeed to the file?
std::fs::write
is just a convenience API. You can write to the file yourself, in which case you can trivially add a newline after json_serde
is done writing, e.g.:
pub fn save(path: impl AsRef<Path>, data: &impl Serialize) -> std::io::Result<()> {
let mut w = BufWriter::new(File::create(path)?);
serde_json::to_writer_pretty(&mut w, data)?;
w.write(b"\n")?;
w.flush()?;
Ok(())
}