Decoding
The core decoding functionality is performed by the decode
function, which decodes bytes
into a value:
>>> import dag_cbor
>>> dag_cbor.decode(b'\xa2aa\x0cabfhello!')
{'a': 12, 'b': 'hello!'}
A buffered binary stream (i.e. an instance of BufferedIOBase
) can be passed to the decode
function instead of a bytes
object, in which case the contents of the stream are read in their entirety and decoded:
>>> stream = BytesIO(b'\xa2aa\x0cabfhello!')
>>> dag_cbor.decode(stream)
{'a': 12, 'b': 'hello!'}
The decision to read the entirety of the stream stems from the DAG-CBOR codec specification, stating that encoding and decoding is only allowed on a single top-level item.
However, the optional keyword argument allow_concat
(default False
) can be set to True
to disable this behaviour and allow only part of the stream to be decoded.