commit | cbfe8be4b9315420c5f60eacc7bf47c05185acbe | [log] [tgz] |
---|---|---|
author | Bob Badour <[email protected]> | Tue Mar 23 13:49:43 2021 +0000 |
committer | Automerger Merge Worker <[email protected]> | Tue Mar 23 13:49:43 2021 +0000 |
tree | edc98c3f5b73937786ca2d28d5642b38ac420ae8 | |
parent | 71ef2e806cddf2ffc24237d754060346f49b305b [diff] | |
parent | 7618641a0bec0df55ae6e2585c6eb5471109e043 [diff] |
[LSC] Add LOCAL_LICENSE_KINDS to external/rust/crates/serde_cbor am: 7618641a0b Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/serde_cbor/+/1649596 Change-Id: Ic39d4e97e35e75eefa7d58bac1f1c4a6120f391a
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.
Serde CBOR supports Rust 1.40 and up. Add this to your Cargo.toml
:
[dependencies] serde_cbor = "0.11.1"
Storing and loading Rust types is easy and requires only minimal modifications to the program code.
use serde_derive::{Deserialize, Serialize}; use std::error::Error; use std::fs::File; // 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::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::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.
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.