Files
kaczmarczyck 8a53986961 CBOR API changes (#639)
* adds extract_* functions to CBOR library

* hides Value implementation details

* CBOR API fixes

* README adapted to API changes
2023-08-11 17:28:59 +02:00
..
2023-08-11 17:28:59 +02:00
2023-03-07 15:56:46 +01:00
2023-08-11 17:28:59 +02:00
2021-06-29 10:01:29 +02:00
2021-06-29 10:01:29 +02:00
2023-08-11 17:28:59 +02:00

CBOR Parsing Library

crates.io crates.io docs.rs License Maintenance

This crate implements Concise Binary Object Representation (CBOR) from RFC 8949.

Usage

fn main() {
    // Build a CBOR object with the crate's convenience macros. Note that this
    // object is not built in canonical order.
    let map_object = cbor_map! {
        1 => cbor_array![2, 3],
        "tstr" => cbor_bytes!(vec![1, 2, 3]),
        -2 => cbor_null!(),
        3 => cbor_true!(),
    };

    println!("Object {:?}", map_object);

    // Serialize to bytes.
    let mut map_data = vec![];
    sk_cbor::writer::write(map_object, &mut map_data).unwrap();
    let hex_map_data = hex::encode(&map_data);

    // Serialized version is in canonical order.
    println!("Serializes to {}", hex_map_data);
    assert_eq!(
        hex_map_data,
        concat!(
            "a4",         // 4-map
            "01",         // int(1) =>
            "820203",     // 2-array [2, 3],
            "03",         // int(3) =>
            "f5",         // true,
            "21",         // nint(-2) =>
            "f6",         // null,
            "6474737472", // 4-tstr "tstr" =>
            "43010203"    // 3-bstr
        )
    );

    // Convert back to an object.  This is different than the original object,
    // because the map is now in canonical order.
    let recovered_object = sk_cbor::reader::read(&map_data).unwrap();
    println!("Deserializes to {:?}", recovered_object);
}