Encoding

The core encoding functionality is performed by the encode function, which encods a value into a bytes object:

>>> import dag_cbor
>>> dag_cbor.encode({'a': 12, 'b': 'hello!'})
b'\xa2aa\x0cabfhello!'

A buffered binary stream (i.e. an instance of BufferedIOBase) can be passed to the encode function using the optional keyword argument stream, in which case the encoded bytes are written to the stream and the number of bytes written is returned:

>>> from io import BytesIO
>>> stream = BytesIO()
>>> dag_cbor.encode({'a': 12, 'b': 'hello!'}, stream=stream)
13
>>> stream.getvalue()
b'\xa2aa\x0cabfhello!'