Provides the infrastructure for doing serialization and deserialization of rust datastructures. In the general case it strives to give fairly high effeciency.
High level overview
Data Type
The rust data type that is being used.
- Serialize
- Deserialize
Serde Data Model
Mapping between the data type and the data format. Creates a separation of concerns between the type and format as each side only needs to "know about" the data model.
Ultimately, the data model is a set of types, a simplified form of Rust's own type system.
Data Format
- Serializer
- Deserializer
Example
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Foo {
a: u64,
b: String,
}
Or if we implemented Serialize ourselves:
use serde::{Deserialize, Serialize, ser::SerializeStruct};
#[derive(Deserialize)]
struct Foo {
a: u64,
b: String,
}
impl Serialize for Foo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut s = serializer.serialize_struct("Foo", 2)?;
s.serialize_field("a", &self.a)?;
s.serialize_field("b", &self.b)?;
s.end()
}
}
https://www.youtube.com/watch?v=BI_bHCGRgMY
Great video about how serde works.