The `cbor` library provides a Python implementation of RFC 7049, the Concise Binary Object Representation (CBOR). It allows encoding and decoding Python objects into a compact, binary format, offering an API similar to Python's built-in `json` module. The current version is 1.0.0, released in March 2024, indicating a stable and mature API, with a relatively slow release cadence focused on stability.
pip install cborVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates encoding and decoding basic Python data types, including `datetime` objects and `bytes`, to and from CBOR binary format. It also shows how to use `dump` and `load` for streaming data to/from file-like objects.
Consult the `CHANGES.rst` file in the GitHub repository for specific migration details if upgrading from legacy `0.x` versions. For new projects, always install the latest stable version.
For custom objects, provide a `default` callable argument to `cbor.dumps()` that returns a serializable representation of the object. Alternatively, convert custom objects to a dictionary or list before serialization.
Ensure your project runs on Python 3 (preferably 3.8+). If you need CBOR for Python 2, you will need to find a different, compatible library or port your environment to Python 3.
Before calling `cbor.dumps()` or `cbor.dump()`, convert `datetime` objects to your desired format (e.g., `int(dt.timestamp())` for UNIX timestamp, `dt.isoformat()` for ISO string) or implement a custom `default` serializer if you need more control over date handling.
Install the library using pip: `pip install cbor`
Add `import cbor` at the beginning of your Python script.
Ensure the input to `cbor.loads()` is a bytes object, typically by reading from a file in binary mode (`'rb'`) or by prefixing a string literal with `b` (e.g., `b'my_cbor_data'`). For files, use `cbor.load()` with an open binary file handle.
No dependency data recorded yet.