multi-cbor
CBOR serialization and deserialization for serde, forked from the archived
upstream serde_cbor crate and extended with a tags feature for
DAG-CBOR tag round-tripping.
multi-cbor is a maintained drop-in replacement for serde_cbor. Upstream
serde_cbor has been unmaintained and archived since 2021. The crate name on
crates.io is still owned by the archived upstream, so this fork publishes under
the multi-cbor name. The license stays MIT OR Apache-2.0, the same as
upstream.
The tags feature is the reason this fork exists. Upstream ignored CBOR tags
during deserialization and refused to emit them during serialization. The
tags feature adds the thread-local tag plumbing that DAG-CBOR needs.
Table of Contents
- Features
- Install
- Usage
- The
tagsFeature - Feature Flags
no_stdSupport- Fork Notes
- Testing
- Maintainers
- Contribute
- License
Features
- CBOR serialization and deserialization for any
serde-compatible type. - Packed encoding. Struct keys and unit enum variants encode as integers.
- Self-describing documents. The CBOR magic number can be prepended.
- The
Valueenum for untyped CBOR data, likeserde_json::Value. - DAG-CBOR tag round-tripping under the
tagsfeature. no_stdsupport with anallocmode.- Zero unsafe code.
Install
Add this to your Cargo.toml:
[]
= "0.1"
For no_std builds:
[]
= { = "0.1", = false }
# Enable alloc to get from_slice and to_vec without std:
# multi-cbor = { version = "0.1", default-features = false, features = ["alloc"] }
MSRV: Rust 1.85 (Edition 2021).
Usage
use ;
use ;
let ferris = Mascot ;
let bytes = to_vec.unwrap;
let back: Mascot = from_slice.unwrap;
assert_eq!;
For streaming I/O, use to_writer and from_reader (these require the std
feature, which is on by default):
use File;
use ;
# use ;
#
#
#
The tags Feature
CBOR tags mark a data item with a tag number that tells the consumer how to
interpret the enclosed value. The DAG-CBOR subset of CBOR uses tags for the
Cid type (tag 42) and the Link wrapper (tag 42 on a byte string).
Upstream serde_cbor ignored tags on read and refused to write them. This fork
adds the tags cargo feature. When the feature is on:
Tagged<T>serializes a(Option<u64>, T)pair, writing the tag before the value.current_cbor_tag()returns the tag that is in scope during avisit_newtype_structcall. This lets aDeserializeimplementation read the tag and dispatch on it.- A thread-local
TagGuardrecords and restores the active tag during nested serialization.
When the feature is off, the same API compiles, but current_cbor_tag() always
returns None and tags are not written. This keeps the no-tags build
byte-compatible with upstream serde_cbor.
[]
= { = "0.1", = ["tags"] }
use Tagged;
use ;
let value = new;
let bytes = to_vec.unwrap;
let back: = from_slice.unwrap;
assert_eq!;
assert_eq!;
Feature Flags
| Feature | Default | Effect |
|---|---|---|
std |
yes | Enables from_reader, to_writer, and the Value module. Requires serde/std. |
alloc |
no | Enables from_slice and to_vec for no_std + alloc builds. Requires serde/alloc. |
tags |
no | Enables CBOR tag round-tripping for DAG-CBOR. Requires the std feature (uses thread_local!). See The tags Feature. |
unsealed_read_write |
no | Exposes the read::Read and write::Write traits so external callers can build custom sources and sinks. |
no_std Support
With default-features = false the crate builds without std. Only the
Serializer and Deserializer types and the SliceWrite and SliceRead
helpers are available. Enable alloc to get from_slice and to_vec. The
Value module and from_reader / to_writer require std.
[]
= { = "0.1", = false, = ["alloc"] }
= { = "1.0", = false, = ["alloc", "derive"] }
Fork Notes
This crate is a fork of the archived upstream serde_cbor 0.7.0. The
differences from upstream are:
- The crate is renamed from
serde_cbortomulti-cbor. - The
tagscargo feature is added for DAG-CBOR tag round-tripping. - The
repositoryfield points at thecryptidtech/multi-cborGitHub repo. - The MSRV is declared as 1.85.
- The dev-dependencies that referenced the
bs-*BetterSign workspace crates are removed. Thecid_linked_listexample is removed for the same reason. A later release will re-point it at the standalonemulti-cidcrate.
The license stays MIT OR Apache-2.0, the same as upstream.
Testing
Maintainers
- Dave Grantham dwg@linuxprogrammer.org
Contribute
Pull requests go to the cryptidtech/multi-cbor
repository. Sign commits with GPG. Use Conventional Commits messages.
License
Dual-licensed as MIT OR Apache-2.0, the same as upstream serde_cbor.
See LICENSE-MIT and LICENSE-APACHE for the
full text.