Write my first dedeserializer

Hi, having read the serde doc, i am still not able to get it work.
here is my code:

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Record {
    score_value: u32,
    player_name: String, //  does not implement `Copy`
    timestamp_fake: u32,

let yesterdayscore = Record { score_value: 1500, player_name: "yesterdayscore".to_string(), timestamp_fake: 7000 };
let myscore = Record { score_value: 800, player_name: "myscore".to_string(), timestamp_fake: 8000 };
let friendscore = Record { score_value: 800, player_name: "friendscore".to_string(), timestamp_fake: 9000 };

let mut allscores :Vec<&Record> = Vec::new();

let serialized = serde_json::to_string(&allscores).unwrap();
println!("serialized = {}", serialized);
let deserialized: std::vec::Vec<&Record> = serde_json::from_str(&serialized).unwrap();

my goal is to save allscores in a txt file with json , and recover it
in a OOP style after derserialization.

You haven't made it clear what error you are getting. The serialisation portion works fine as far as I can see. The deserialisation part has the problem that you are trying to deserialise to a reference and serde doesn't know how to do that. Since there isn't really anything to borrow from, you probably just want to deserialise to owned data:

let deserialized: std::vec::Vec<Record> = serde_json::from_str(&serialized).unwrap();


I'm not sure what you mean by "OOP style", but it looks like that is what you want.

thx jameseb7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.