CBOR (de)serializer with extensive tag support

5.9.0 · active · verified Sun Mar 29

cbor2 is a Python library for encoding and decoding Concise Binary Object Representation (CBOR) data, fully compatible with RFC 8949. It offers a simple API similar to the `json` or `pickle` modules, with extensive support for CBOR tags and standard library objects. Currently at version 5.9.0, it is actively maintained with a regular release cadence, featuring a highly performant Rust-based backend for improved memory safety and performance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic encoding and decoding of Python objects to and from CBOR bytestrings using `cbor2.dumps()` and `cbor2.loads()`.

import cbor2

data_to_encode = {'name': 'Alice', 'age': 30, 'is_active': True}

# Encode Python object to CBOR bytestring
encoded_data = cbor2.dumps(data_to_encode)
print(f"Encoded CBOR: {encoded_data}")

# Decode CBOR bytestring back to Python object
decoded_data = cbor2.loads(encoded_data)
print(f"Decoded Python object: {decoded_data}")

# Example with file-like objects
# import io
# output_buffer = io.BytesIO()
# cbor2.dump(data_to_encode, output_buffer)
# output_buffer.seek(0)
# decoded_from_file = cbor2.load(output_buffer)
# print(f"Decoded from file-like object: {decoded_from_file}")

view raw JSON →