I have defined a struct to store data generated by the code. Since it takes a lot of time to generate it each time, I wanted it to be written out - used serde (1.0, derive feature enabled).
pub struct TrgWorld {
pub atlas: Vec<(String, TrgLink)>
}
type TrgLink = Arc<Mutex<TrgNode>>;
#[derive(Debug, Serialize, Deserialize)]
pub struct TrgNode {
pub seq: String,
pub id_score: Vec<f32>,
pub sim_score: Vec<Vec<f32>>,
#[serde(skip)]
pub sim_seq: Vec<Vec<TrgLink>>,
pub valid_trg: bool
}
I've enabled #[serde(skip)]
on one of the fields which is by default non-SerDe, and unnecessary once I've generated the data once.
Once the necessary operations are complete, I collect the TrgNode struct into vector for export.
let out = trgmap.atlas.into_iter().map(|(_, node)|
Arc::try_unwrap(node).expect("Strong counts not released.")
.into_inner().expect("Could not lock")
).collect::<Vec<TrgNode>>();
I then use serde_json (v1.0.73) to Deserialize the struct into json, and write the string to a file.
let outdump = serde_json::to_string(&out).unwrap();
let output_printer = io::BufWriter::new(File::create(format!("{}/{}_exported_files.json", outdir, outname))
.expect("Could not prepare output summary file"));
serde_json::to_writer(output_printer, &outdump).unwrap();
I also need another piece of information - lineages: Vec<String>
to be exported along with this.
let outdump = serde_json::to_string(&lineages).unwrap();
let output_printer = io::BufWriter::new(File::create(format!("{}/{}_exported_lineages.json", outdir, outname))
.expect("Could not prepare output summary file"));
serde_json::to_writer(output_printer, &outdump).unwrap();
Both of these are exported without any issue, but it fails to Serialized when I try the following
// Serialize a Vec<String>
let mut temp_str = String::new();
let mut file = std::fs::File::open(&path_to_lineages).unwrap();
file.read_to_string(&mut temp_str).unwrap();
let lineages: Vec<String> = serde_json::from_str(&temp_str).unwrap();
temp_str.clear();
// Serialize Vec<TrgNode>
let mut file = std::fs::File::open(&path_to_import).unwrap();
file.read_to_string(&mut tmp_str).unwrap();
let out: Vec<TrgNode> = serde_json::from_str(&temp_str).unwrap();
Where I get the following error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value:
Error("invalid type: string \"[\\\"denv_0\\\",\\\"denv_1\\\",\\\"denv_2\\\",\\\"denv_3\\\",\\\"denv_4\\\"]\",
expected a sequence", line: 1, column: 58)', src/main.rs:59:67
Even when I uncomment the Vec<String>
serialization part, I get a similar error with the Vec<TrgNode>
part. I'm not sure where I am going wrong here, please help me out.
Thanks in advance! Let me know if you need any more information from my side.