ser-write
Writer-style serializers and deserializers for convenience designed with embedded (no_std) targets in mind.
- Writer-style serializers use the common writer trait found in this crate.
- Designed for
no_std. - Fully supports
stdorallocwhen enabled for code portability and testablilty. - For each serializer a deserializer is provided for convenience.
- Embedded projects can implement
SerWritetrait for custom containers, frame builders and more.
This crate provides:
- the trait -
SerWritewhich should be used by serializers to write the serialized output, SerError- a convenient error type,SliceWriter- a convenient slice writer object implementingSerWrite,SerWriteimplementations for foreign types.
Depending on the enabled crate features, SerWrite is implemented for:
SliceWriter- example slice writer implementation,arrayvec::ArrayVec<u8,CAP>-arrayvecfeature,heapless::Vec<u8,CAP>-heaplessfeature,smallvec::SmallVec<[u8; CAP]>-smallvecfeature,tinyvec::ArrayVec<[u8; CAP]>-tinyvecfeature,tinyvec::SliceVec<'_, u8>-tinyvecfeature,tinyvec::TinyVec<[u8; CAP]>-tinyvecwithallocorstdfeature,Vec<u8>-allocorstdfeature,VecDeque<u8>-allocorstdfeature,io::Cursor<T: io::Write>-stdfeature,
smallvec also enables alloc.
Usage
Start by adding a dependency to the serializer:
For example:
[dependencies]
ser-write-json = { version = "0.3", default-features = false }
If you want to also pull implementations of SerWrite for the foreign types add:
ser-write = { version = "0.3", default-features = false, features = ["arrayvec", "heapless"] }
In the above example implementations for: arrayvec::ArrayVec<u8;_> and heapless::Vec<u8> are selected.
Serializers
Currently available serializers and deserializers are:
- JSON (compact) - ser-write-json
- MessagePack - ser-write-msgpack
Example
An example SliceWriter implementation:
use ;
Alternatives
For alloc only:
Alternatively there's a Rust Embedded Community crate for serializeing JSONs without std:
- serde-json-core
- serde-json-core-fmt (a writer-style attempt abusing
fmt::Displaytrait)
serde-json-core is a true no_std, no alloc alternative but rather inconvenient. One has to serialize data into intermediate slices instead just pushing data to outgoing buffers or frame builder implementations.
Motivation
- This crate would not be needed once something like
io::Writelands in the Rust core. - Unique features of the deserializers adds some functionality missing in the other crates.
Rust Version Requirements
ser-write requires Rustc version 1.87 or greater.