Skip to main content
Build Status Code Coverage

This library provides encoding and decoding for the Concise Binary Object Representation (CBOR) (RFC 7049) serialization format.

Usage

from cbor2 import *

# Serialize an object as a bytestring
data = dumps(['hello', 'world'])

# Deserialize a bytestring
obj = loads(data)

# Efficiently deserialize from a file
with open('input.cbor', 'rb') as fp:
    obj = load(fp)

# Efficiently serialize an object to a file
with open('output.cbor', 'wb') as fp:
    dump(obj, fp)

# Iteratively decode a datastream
for chunk in CBORDecoder().decode(data):
  ...

String/bytes handling on Python 2

Bytestrings are encoded as binary strings on Python 2. If you want to encode strings as text on Python 2, use unicode strings instead.

Date/time handling

CBOR does not support naïve datetimes (that is, datetimes where tzinfo is missing). When the encoder encounters such a datetime, it needs to know which timezone it belongs to. To this end, you can specify a default timezone by passing a datetime.tzinfo instance to dump()/dumps() call as the timezone argument. Decoded datetimes are always timezone aware.

By default, datetimes are serialized in a manner that retains their timezone offsets. You can optimize the data stream size by passing datetime_as_timestamp=False to dump()/dumps(), but this causes the timezone offset information to be lost.

Cyclic (recursive) data structures

By default, both the encoder and decoder support cyclic data structures (ie. containers that contain references to themselves). When serializing, this requires some extra space in the data stream. If you know you won’t have cyclic structures in your data, you can turn off the value sharing feature by passing the value_sharing=False option to dump()/dumps().

Tag support

In addition to all standard CBOR tags, this library supports many extended tags:

Tag

Semantics

Python type(s)

0

Standard date/time string

datetime.date / datetime.datetime

1

Epoch-based date/time

datetime.date / datetime.datetime

2

Positive bignum

int / long

3

Negative bignum

int / long

4

Decimal fraction

decimal.Decimal

5

Bigfloat

decimal.Decimal

28

Mark shared value

N/A

29

Reference shared value

N/A

30

Rational number

fractions.Fraction

35

Regular expression

_sre.SRE_Pattern (result of re.compile(...))

36

MIME message

email.message.Message

37

Binary UUID

uuid.UUID

Customizing encoding/decoding

The encoder allows you to specify a mapping of types to callables that handle the encoding of some otherwise unsupported type. The decoder, on the other hand, allows you to specify a mapping of semantic tag numbers to callables that implement custom transformation logic for tagged values.

See the docstrings of cbor2.CBOREncoder and cbor2.CBORDecoder for details.

Metadata

Release files for cbor2 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cbor2 2.0.0
File Size Uploaded
cbor2-2.0.0.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cbor2 2.0.0
File Interpreter ABI Platform
cbor2-2.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.2 kB

Release files / cbor2-2.0.0.tar.gz

Download URL cbor2-2.0.0.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
36250692464ee26d5ed450a50847e41bf4961a14ec1816b06dfd82fc00578e92
BLAKE2b-256 checksum
How to use checksums
22553238832db2aaac5a519dc97757eb690a6a915de630af9d1392cc14dfecf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / cbor2-2.0.0-py2.py3-none-any.whl

Download URL cbor2-2.0.0-py2.py3-none-any.whl
Size 12.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f6e104e216ba8d325c9e6917a99222ee75c31e3a406c7898916670d0de929f86
BLAKE2b-256 checksum
How to use checksums
8839d9870ffe2868993558da12d9e54fa727740be5d850cd2840c67e33a274c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

6.1.1

49 release files

6.1.0

49 release files

6.0.1

43 release files

6.0.0

43 release files

5.9.0

44 release files

5.8.0

44 release files

5.7.1

50 release files

5.7.0

50 release files

5.6.3

37 release files

5.6.2

37 release files

5.6.0

37 release files

5.5.0

37 release files

5.4.5

36 release files

5.4.4

36 release files

5.4.2

13 release files

5.4.1

13 release files

5.3.0

13 release files

5.2.0

1 release file

5.1.2

1 release file

5.1.1

1 release file

5.1.0

1 release file

5.0.1

1 release file

5.0.0

1 release file

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

This release

2.0.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page