Postcard is a #![no_std]
focused serializer and deserializer for Serde.
Postcard aims to be convenient for developers in constrained environments, while allowing for flexibility to customize behavior as needed.
#![no_std]
usage, in embedded or other constrained contextsserde
features, so postcard
can be used as a drop in replacementAs of v1.0.0, postcard
has a documented and stable wire format. More information about this
wire format can be found in the spec/
folder of the Postcard repository, or viewed online
at https://postcard.jamesmunns.com.
Work towards the Postcard Specification and portions of the Postcard 1.0 Release were sponsored by Mozilla Corporation.
All signed and unsigned integers larger than eight bits are encoded using a [Varint].
This includes the length of array slices, as well as the discriminant of enums
.
For more information, see the [Varint] chapter of the wire specification.
Postcard can serialize and deserialize messages similar to other serde
formats.
Using the default heapless
feature to serialize to a heapless::Vec<u8>
:
```rust use core::ops::Deref; use serde::{Serialize, Deserialize}; use postcard::{frombytes, tovec}; use heapless::Vec;
struct RefStruct<'a> {
bytes: &'a [u8],
strs: &'a str,
}
let message = "hElLo";
let bytes = [0x01, 0x10, 0x02, 0x20];
let output: Vec
assert_eq!( &[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',], output.deref() );
let out: RefStruct = frombytes(output.deref()).unwrap(); asserteq!( out, RefStruct { bytes: &bytes, str_s: message, } ); ```
Or the optional alloc
feature to serialize to an alloc::vec::Vec<u8>
:
```rust use core::ops::Deref; use serde::{Serialize, Deserialize}; use postcard::{frombytes, toallocvec}; extern crate alloc; use alloc::vec::Vec;
struct RefStruct<'a> {
bytes: &'a [u8],
strs: &'a str,
}
let message = "hElLo";
let bytes = [0x01, 0x10, 0x02, 0x20];
let output: Vec
assert_eq!( &[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',], output.deref() );
let out: RefStruct = frombytes(output.deref()).unwrap(); asserteq!( out, RefStruct { bytes: &bytes, str_s: message, } ); ```
postcard
supports a system called Flavors
, which are used to modify the way
postcard serializes or processes serialized data. These flavors act as "plugins" or "middlewares"
during the serialization or deserialization process, and can be combined to obtain complex protocol formats.
See the documentation of the ser_flavors
or de_flavors
modules for more information on usage.
Cargo.toml
Don't forget to add the no-std
subset of serde
along with postcard
to the [dependencies]
section of your Cargo.toml
!
```toml [dependencies] postcard = "1.0.0"
serde
has the std
feature enabled, which makes it unsuitable for embedded targetsserde = { version = "1.0.*", default-features = false } ```
Licensed under either of
at your option.
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.