Estimated time: 1 day
Rust ecosystem has the well-known serde crate, which provides common approach and toolset for serialization and deserialization.
The sweet part is that serde does not rely on a runtime reflection mechanism and uses trait implementation for each type, so eliminates most runtime costs and in most cases makes serialization as performant as handwritten serializer for a particular case, yet remains ergonomic due to automatic code deriving.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Point { x: 1, y: 2 };
let serialized = serde_json::to_string(&point).unwrap();
println!("serialized = {}", serialized);
let deserialized: Point = serde_json::from_str(&serialized).unwrap();
println!("deserialized = {:?}", deserialized);
}
serde by itself represents only an universal serialization frontend, which can be backed by actual implementation for any format. There are already implemented backends for most used formats, and you're free to implement backend for your own format if it's not implemented yet.
For better understanding and familiarity with serde's design, concepts, usage, and features (like zero-copy deserialization), read through the following articles:
Write a program which deserializes the following JSON into a static Request
type and prints out its serialization in a YAML and TOML formats. Consider to choose correct types for data representation.
Prove your implementation correctness with tests.