| 1 | # Serde CBOR |
| 2 | |
| 3 | [](https://github.com/kanidm/cbor/actions/workflows/ci.yml) |
| 4 | [](https://crates.io/crates/serde_cbor_2) |
| 5 | [](https://docs.rs/serde_cbor_2) |
| 6 | |
| 7 | This crate implements the Concise Binary Object Representation from [RFC 7049][rfc7049]. |
| 8 | It builds on [Serde][serde], the generic serialization framework for Rust. |
| 9 | CBOR provides a binary encoding for a superset of the JSON data model that is small and very fast to parse. |
| 10 | |
| 11 | ## Usage |
| 12 | |
| 13 | Serde CBOR supports Rust 1.81 and up. Add this to your `Cargo.toml`: |
| 14 | |
| 15 | ```toml |
| 16 | [dependencies] |
| 17 | serde_cbor_2 = "0.11.2" |
| 18 | ``` |
| 19 | |
| 20 | Storing and loading Rust types is easy and requires only minimal modifications to the program code. |
| 21 | |
| 22 | ```rust |
| 23 | use std::{error::Error, fs::File}; |
| 24 | use serde::{Deserialize, Serialize}; |
| 25 | |
| 26 | // Types annotated with `Serialize` can be stored as CBOR. |
| 27 | // To be able to load them again add `Deserialize`. |
| 28 | #[derive(Debug, Serialize, Deserialize)] |
| 29 | struct Mascot { |
| 30 | name: String, |
| 31 | species: String, |
| 32 | year_of_birth: u32, |
| 33 | } |
| 34 | |
| 35 | fn main() -> Result<(), Box<dyn Error>> { |
| 36 | let ferris = Mascot { |
| 37 | name: "Ferris".to_owned(), |
| 38 | species: "crab".to_owned(), |
| 39 | year_of_birth: 2015, |
| 40 | }; |
| 41 | |
| 42 | let ferris_file = File::create("examples/ferris.cbor")?; |
| 43 | // Write Ferris to the given file. |
| 44 | // Instead of a file you can use any type that implements `io::Write` |
| 45 | // like a HTTP body, database connection etc. |
| 46 | serde_cbor_2::to_writer(ferris_file, &ferris)?; |
| 47 | |
| 48 | let tux_file = File::open("examples/tux.cbor")?; |
| 49 | // Load Tux from a file. |
| 50 | // Serde CBOR performs roundtrip serialization meaning that |
| 51 | // the data will not change in any way. |
| 52 | let tux: Mascot = serde_cbor_2::from_reader(tux_file)?; |
| 53 | |
| 54 | println!("{tux:?}"); |
| 55 | // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 } |
| 56 | |
| 57 | Ok(()) |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | There are a lot of options available to customize the format. |
| 62 | To operate on untyped CBOR values have a look at the `Value` type. |
| 63 | |
| 64 | ## License |
| 65 | |
| 66 | Licensed under either of |
| 67 | |
| 68 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>) |
| 69 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>) |
| 70 | |
| 71 | at your option. |
| 72 | |
| 73 | ### Contribution |
| 74 | |
| 75 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 76 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any |
| 77 | additional terms or conditions. |
| 78 | |
| 79 | [rfc7049]: https://tools.ietf.org/html/rfc7049 |
| 80 | [serde]: https://github.com/serde-rs/serde |