To check how to use serde_json::to_writer() and serde_json::from_reader() I use a very simple main(), no problem when I just write but a strange error when I add the reader Error: Error("EOF while parsing a value", line: 1, column: 0)
I just start to learn rust ...
use std::collections::HashMap;
use std::error::Error;
use std::fs::OpenOptions;
use std::io::BufReader;
fn main() -> Result<(), Box<dyn Error>> {
let mut map: HashMap<String, String> = HashMap::new();
map.insert("foo".to_string(), "bar".to_string());
map.insert("hello".to_string(), "you".to_string());
map.insert("to".to_string(), "you".to_string());
println!("{:?}", map);
let file = OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.open("foo_serde.json")?;
// why does this work?
serde_json::to_writer(&file, &map)?;
let reader = BufReader::new(file);
let output: HashMap<String, String> = serde_json::from_reader(reader)?;
println!("{:?}", output);
Ok(())
}