cbor-diag: Diagnostic notation for CBOR

See the project README file for installation, maintenance and license information.

cbor-diag

This module provides conversion functions between CBOR’s diagnostic notation and its binary representation.

See RFC8949 for the definition of CBOR and its diagnostic notation.

For producing binary representations of CBOR, and for processing them, the cbor2 package is recommended.

cbor_diag.cbor2diag(encoded, *, pretty=Ellipsis)

Given a byte string containing encoded CBOR, produce some diagnostic notation.

>>> from cbor_diag import *
>>> encoded = bytes.fromhex('a1016568656c6c6f')
>>> cbor2diag(encoded)
'{1: "hello"}'

Key word arguments influence additional details:

  • With pretty=False, no space is left after colons, commas etc.

cbor_diag.diag2cbor(diagnostic)

Given a string in CBOR diagnostic notation, produce its CBOR binary encoding.

>>> from cbor_diag import *
>>> diag = '{1: "hello"}'
>>> encoded = diag2cbor(diag)
>>> encoded.hex()
'a1016568656c6c6f'
>>> import cbor2                
>>> cbor2.loads(encoded)        
{1: 'hello'}