Open dharanad opened 10 months ago
Exercise: Serialize and deserialize a data structure to a buffer with serde (RON).
use std::{fs::File, io::Error, rc::Rc};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
#[derive(Serialize, Deserialize, Debug)]
struct Move {
direction: Direction,
step_count: u16
}
fn main() -> Result<(), Error> {
// create move object
let a = Move {
direction: Direction::UP,
step_count: 10
};
println!("serializing {:?}", a);
// serialize a to vec
let buf = serde_json::to_vec(&a).unwrap();
println!("serialized bytes: {:?}", buf);
// convert vec to string
let str_b = std::str::from_utf8(&buf).unwrap();
println!("serialized json string: {}", str_b);
// deserialize vec to b
let b: Move = serde_json::from_slice(&buf).unwrap();
println!("deserialized: {:?}", b);
Ok(())
}
Output
serializing Move { direction: UP, step_count: 10 }
serialized bytes: [123, 34, 100, 105, 114, 101, 99, 116, 105, 111, 110, 34, 58, 34, 85, 80, 34, 44, 34, 115, 116, 101, 112, 95, 99, 111, 117, 110, 116, 34, 58, 49, 48, 125]
serialized json string: {"direction":"UP","step_count":10}
deserialized: Move { direction: UP, step_count: 10 }
Continuation of above exercise
use std::{fs::File, io::Error, rc::Rc};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
#[derive(Serialize, Deserialize, Debug)]
struct Move {
direction: Direction,
step_count: u16
}
fn main() -> Result<(), Error> {
// create move object
let a = Move {
direction: Direction::UP,
step_count: 10
};
println!("serializing {:?}", a);
// serialize a to ron string
let serialized_ron_str = ron::to_string(&a).unwrap();
println!("serialized ron string: {}", serialized_ron_str);
// deserializing ron string to b
let b: Move = ron::from_str(&serialized_ron_str).unwrap();
println!("deserialized: {:?}", b);
Ok(())
}
Output
serializing Move { direction: UP, step_count: 10 }
serialized ron string: (direction:UP,step_count:10)
deserialized: Move { direction: UP, step_count: 10 }
Exercise: Serialize and deserialize 1000 data structures with serde (BSON).
use std::{fs::File, io::Error, rc::Rc};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
#[derive(Serialize, Deserialize, Debug)]
struct Move {
direction: Direction,
step_count: u16
}
fn main() -> Result<(), Error> {
// Create a file for writing
let file = File::create("move.bson")?;
// Serialize the object to BSON and write it to the file
println!("writing to file");
for i in 0..=1000 {
// create move object
let m = Move {
direction: match i % 4 {
0 => Direction::UP,
1 => Direction::DOWN,
2 => Direction::LEFT,
3 => Direction::RIGHT,
_ => Direction::UP
},
step_count: i,
};
// serialize to BSON
let bson_value = bson::to_bson(&m).unwrap();
match bson_value.as_document() {
Some(doc) => {
doc.to_writer(&file).unwrap();
}
None => {
println!("not a doc type. stopping");
break;
}
}
}
println!("done");
// Open a file for reading
let file = File::open("move.bson")?;
println!("reading from file");
loop {
match bson::Document::from_reader(&file) {
Ok(doc) => {
let m: Move = bson::from_bson(bson::Bson::Document(doc)).unwrap();
println!("{:?}", m);
}
Err(e) => {
println!("error: {:?}", e);
break;
}
}
}
println!("done");
Ok(())
}
TDB
Ref: https://www.perplexity.ai/search/Bitcask-9XxybXumQZiQIzGjQH1YsQ?s=c
Exercise: Serialize and deserialize a data structure with serde (JSON).