messagepack-serde
MessagePack for no_std with serde.
Examples
use ;
let buf: & = &;
let data = .unwrap;
let expected = Data ;
assert_eq!;
let mut serialized = ;
let len = to_slice.unwrap;
assert_eq!;
Installation
Add this crate to Cargo.toml. no_std is supported by default.
= { = "0.1" }
Features
-
no_stdsupport
If you want to usestd::io::Readorstd::io::Write, enable thestdfeature and usemessagepack_serde::from_readerormessagepack_serde::to_writer. -
Flexible numeric serialization
- Provides multiple serialization strategies:
Exact: Serializes numeric types exactly as provided.LosslessMinimize: Minimizes size without losing information (default).AggressiveMinimize: Aggressively minimizes values, including serializing floats with integral values as integers.
- To deserialize arbitrary numeric values, use
messagepack_serde::value::Number.
- Provides multiple serialization strategies:
-
extformat support
Design Decisions
Struct serialization format
This crate serializes Rust structs as MessagePack maps by default to preserve field names and allow flexible field ordering. Some other implementations (e.g., rmp-serde and MessagePack for C#) serialize structs as arrays by default.
To maximize interoperability, the deserializer accepts both map- and array-serialized structs. When an array is encountered, fields are read in the declaration order of the Rust struct.
// Example: deserializing a struct from an array and a map
use Deserialize;
// [true, 0] serialized as a MessagePack array of length 2
let buf: & = &;
let s = .unwrap;
assert_eq!;
// {"compact": true, "schema": 0} serialized as a MessagePack map
let buf: & = &;
let s = .unwrap;
assert_eq!;
A major advantage of serializing structs as arrays is reduced output size. Smaller output sizes positively impact processing speed. Additionally, since you no longer need to search for properties within strings during deserialization, you can expect faster deserialization times as well. On the downside, this eliminates the self-describing nature of maps, making binary compatibility more fragile.
// Example: field was added and cannot be deserialized
use Deserialize;
// A future version of the struct
// Older payload: [true, 0]
let buf: & = &;
let s = ;
assert!; // cannot deserialize the array
// Older payload: {"compact": true, "schema": 0}
let buf: & = &;
let s = .unwrap;
assert_eq!;
This crate prioritizes robustness and binary compatibility, so structures are serialized in map format. For array-based serialization, consider representing the data as tuples or manually implementing Serialize implementations for each type.
// Example: serialize a struct as an array
use Deserialize;
let val = S ;
let mut buf = ;
let len = to_slice.unwrap;
// [true, 0] serialized as a MessagePack array of length 2
let expected: & = &;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.