Crate rmp_serde [−] [src]
Type-based Serialization and Deserialization
Serde provides a mechanism for low boilerplate serialization & deserialization of values to and
from MessagePack via the serialization API. To be able to serialize a piece of data, it must
implement the serde::Serialize
trait. To be able to deserialize a piece of data, it must
implement the serde::Deserialize
trait. Serde provides provides an annotation to
automatically generate the code for these traits: #[derive(Serialize, Deserialize)]
.
Examples
Let's try to encode and decode some built-in types.
extern crate serde; extern crate rmp_serde as rmps; use serde::{Deserialize, Serialize}; use rmps::{Deserializer, Serializer}; fn main() { let mut buf = Vec::new(); let val = (42u8, "the Answer"); val.serialize(&mut Serializer::new(&mut buf)).unwrap(); assert_eq!(vec![0x92, 0x2a, 0xaa, 0x74, 0x68, 0x65, 0x20, 0x41, 0x6e, 0x73, 0x77, 0x65, 0x72], buf); let mut de = Deserializer::new(&buf[..]); assert_eq!((42, "the Answer".to_owned()), Deserialize::deserialize(&mut de).unwrap()); }
No one gonna hurt if we add some reflection magic.
extern crate serde; #[macro_use] extern crate serde_derive; extern crate rmp_serde as rmps; use std::collections::HashMap; use serde::{Deserialize, Serialize}; use rmps::{Deserializer, Serializer}; #[derive(Debug, PartialEq, Deserialize, Serialize)] struct Human { age: u32, name: String, } fn main() { let mut buf = Vec::new(); let val = Human { age: 42, name: "John".into(), }; val.serialize(&mut Serializer::new(&mut buf)).unwrap(); }
Reexports
pub use decode::Deserializer; |
pub use decode::from_slice; |
pub use decode::from_read; |
pub use encode::Serializer; |
pub use encode::to_vec; |
pub use encode::to_vec_named; |
Modules
decode | |
encode |
Structs
Raw |
Helper that allows both to encode and decode strings no matter whether they contain valid or invalid UTF-8. |
RawRef |
Helper that allows both to encode and decode strings no matter whether they contain valid or invalid UTF-8. |