Serde CBOR
  • Rust 99.8%
  • Python 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
James Hodgkinson b06c72ffc3
Merge pull request #31 from kanidm/dependabot/github_actions/actions-rust-lang/setup-rust-toolchain-2.0.0
Bump actions-rust-lang/setup-rust-toolchain from 1.17.0 to 2.0.0
2026-09-14 15:51:21 +10:00
.github Bump actions-rust-lang/setup-rust-toolchain from 1.17.0 to 2.0.0 2026-09-14 05:21:49 +00:00
examples Remove extern crate declarations 2023-01-02 19:39:16 +00:00
fuzz Add a script to create fuzz curpuses 2017-09-25 22:13:52 +02:00
src fix: clippy lint about const refcell 2025-06-14 10:01:18 +10:00
tests maint: adding some coverage 2025-06-14 09:57:23 +10:00
.editorconfig Fix rustfmt issues in CI jobs (#192) 2020-06-04 16:58:07 +02:00
.gitignore Run build and tests for no_std in Travis. 2019-03-12 10:44:41 +01:00
Cargo.toml maint: update version 2025-06-14 11:59:34 +10:00
CHANGELOG.md maint: updating MSRV 2025-04-28 23:46:50 +10:00
CONTRIBUTING.md Improve README, add CONTRIBUTING 2018-12-28 14:52:30 +01:00
LICENSE-APACHE Fix LICENSE-APACHE file. 2016-03-30 13:34:00 +02:00
LICENSE-MIT Relicense under MIT/Apache-2.0 2016-03-30 13:06:11 +02:00
README.md maint: updating MSRV 2025-04-28 23:46:50 +10:00

Serde CBOR

CI crates.io Documentation

This crate implements the Concise Binary Object Representation from RFC 7049. It builds on Serde, the generic serialization framework for Rust. CBOR provides a binary encoding for a superset of the JSON data model that is small and very fast to parse.

Usage

Serde CBOR supports Rust 1.81 and up. Add this to your Cargo.toml:

[dependencies]
serde_cbor_2 = "0.11.2"

Storing and loading Rust types is easy and requires only minimal modifications to the program code.

use std::{error::Error, fs::File};
use serde::{Deserialize, Serialize};

// Types annotated with `Serialize` can be stored as CBOR.
// To be able to load them again add `Deserialize`.
#[derive(Debug, Serialize, Deserialize)]
struct Mascot {
    name: String,
    species: String,
    year_of_birth: u32,
}

fn main() -> Result<(), Box<dyn Error>> {
    let ferris = Mascot {
        name: "Ferris".to_owned(),
        species: "crab".to_owned(),
        year_of_birth: 2015,
    };

    let ferris_file = File::create("examples/ferris.cbor")?;
    // Write Ferris to the given file.
    // Instead of a file you can use any type that implements `io::Write`
    // like a HTTP body, database connection etc.
    serde_cbor_2::to_writer(ferris_file, &ferris)?;

    let tux_file = File::open("examples/tux.cbor")?;
    // Load Tux from a file.
    // Serde CBOR performs roundtrip serialization meaning that
    // the data will not change in any way.
    let tux: Mascot = serde_cbor_2::from_reader(tux_file)?;

    println!("{tux:?}");
    // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 }

    Ok(())
}

There are a lot of options available to customize the format. To operate on untyped CBOR values have a look at the Value type.

License

Licensed under either of

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.