Struct cbor::Decoder
[−]
[src]
pub struct Decoder<R> { /* fields omitted */ }
Read CBOR data items into Rust values from the underlying reader R
.
Methods
impl<R: Read> Decoder<R>
[src]
fn from_reader(rdr: R) -> Decoder<BufReader<R>>
Create a new CBOR decoder from the underlying reader.
impl<R: Read> Decoder<R>
[src]
fn decode<D: Decodable>(&mut self) -> DecodedItems<R, D>
Decode a sequence of top-level CBOR data items into Rust values.
Example
This shows how to encode and decode a sequence of data items:
use cbor::{Decoder, Encoder}; let data = vec![("a".to_string(), 1), ("b".to_string(), 2), ("c".to_string(), 3)]; let mut enc = Encoder::from_memory(); enc.encode(&data).unwrap(); let mut dec = Decoder::from_bytes(enc.as_bytes()); let items: Vec<(String, i32)> = dec.decode() .collect::<Result<_, _>>() .unwrap(); assert_eq!(items, data);
fn items(&mut self) -> Items<R>
Read a sequence of top-level CBOR data items.
This yields data items represented by the Cbor
type, which is its
abstract syntax. (Using the decode
iterator is probably much more
convenient, but this is useful when you need to do more sophisticated
analysis on the CBOR data.)
Example
This shows how to encode and decode a sequence of data items:
use cbor::{Cbor, CborUnsigned, Decoder, Encoder}; let mut enc = Encoder::from_memory(); enc.encode(vec![("a", 1), ("b", 2), ("c", 3)]).unwrap(); let mut dec = Decoder::from_bytes(enc.as_bytes()); let items = dec.items().collect::<Result<Vec<_>, _>>().unwrap(); assert_eq!(items, vec![ Cbor::Array(vec![ Cbor::Unicode("a".to_string()), Cbor::Unsigned(CborUnsigned::UInt8(1)), ]), Cbor::Array(vec![ Cbor::Unicode("b".to_string()), Cbor::Unsigned(CborUnsigned::UInt8(2)), ]), Cbor::Array(vec![ Cbor::Unicode("c".to_string()), Cbor::Unsigned(CborUnsigned::UInt8(3)), ]), ]);