Skip to main content

atomr_core/serialization/
mod.rs

1//! Serialization framework.
2//!
3//! A `Serializer` is a pluggable codec identified by a numeric id. The
4//! registry maps both rust `TypeId` and serializer id to concrete codecs.
5
6mod json;
7mod registry;
8mod traits;
9
10pub use json::JsonSerializer;
11pub use registry::SerializationRegistry;
12pub use traits::{Serializer, SerializerError};
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use serde::{Deserialize, Serialize};
18
19    #[derive(Debug, Serialize, Deserialize, PartialEq)]
20    struct Greeting {
21        who: String,
22    }
23
24    #[test]
25    fn json_roundtrip() {
26        let reg = SerializationRegistry::default();
27        reg.register(JsonSerializer::<Greeting>::new(1));
28        let out = reg.to_bytes(&Greeting { who: "world".into() }).expect("serialize");
29        let back: Greeting = reg.from_bytes(&out).expect("deserialize");
30        assert_eq!(back.who, "world");
31    }
32}