The following is the pseudo code when I serialize an object and then deserialize it:
let my_data = 1i32;
let my_data_ser = serde::Serialize(&my_data);
let my_data_de = serde::Deserialize::<i32>(my_data_ser);
When I do deserialize I need to add type annotation <i32> to serde::Deserialize::<i32>. But in the situation I am facing, there are many types that I can not write it down by hand. So is there a way I don't have to add type annotation? I found the mothod deserialize_any, would it be a relevant solution?
The deserializer has to be given the type — otherwise it cannot know what to create from the data — but you may be able to get the type from some (compile time) context rather than writing it out. Can you give a larger code sample showing how this deserialization is going to be used, and more description of your program's goal?
Thanks for your reply.
The following is the larger code sample I am facing:
In some day, I save some data need to save in local files:
use std::io::{BufWriter, BufReader};
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use bincode;
pub fn sof<T: Serialize>(data: &T, name: &str, path: &str) {
let full_path = path.to_owned() + "/" + name;
let w = std::fs::File::create(full_path).unwrap();
let mut f = BufWriter::new(w);
bincode::serialize_into(&mut f, &data).unwrap();
}
pub fn rof<T: DeserializeOwned>(name: &str, path: &str) -> T {
let full_path = path.to_owned() + "/" + name;
let r = std::fs::File::open(full_path).unwrap();
let mut f = BufReader::new(r);
let res: T = bincode::deserialize_from::<&mut BufReader<std::fs::File>, T>(&mut f).unwrap();
res
}
pub trait Sof: Serialize
where
for<'a> Self: Deserialize<'a>,
{
fn sof(&self, name: &str, path: &str) { sof(self, name, path); }
fn rof(name: &str, path: &str) -> Self { rof::<Self>(name, path) }
}
impl<T> Sof for T
where
for<'a> T: Serialize + Deserialize<'a>,
{}
#[derive(Serialize, Deserialize)]
struct MyType1;
#[derive(Serialize, Deserialize)]
struct MyType2;
fn main() {
let my_data_i32 = 1i32;
let my_data_f32 = 1f32;
let my_data_type1 = MyType1;
let my_data_type2 = MyType2;
my_data_i32.sof("data1", "D:/data_save");
my_data_f32.sof("data2", "D:/data_save");
my_data_type1.sof("data3", "D:/data_save");
my_data_type2.sof("data4", "D:/data_save");
}
And in another day, I need to read the saved files out:
fn main {
let data1 = rof::<i32>("data1", "D:/data_save");
let data2 = rof::<f32>("data2", "D:/data_save");
let data3 = rof::<MyType1>("data3", "D:/data_save");
let data4 = rof::<MyType2>("data4", "D:/data_save");
}
Then you can serialize and deserialize them all together only using the MyData struct type.
let data = rof::<MyData>("all_data", "D:/data_save");
Or, if it is important that they be written to separate files, then you can still use the struct to help you serialize and deserialize the parts with the correct types.