Skip to main content

Pure Python CBOR (de)serializer with extensive tag support

Project description

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)

String/bytes handling on Python 2

The str type is encoded as binary 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 save a little bit of space by turning off the value sharing feature by passing the value_sharing=False option to dump()/dumps().

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.

Custom encoder and decoder hooks can also be made to support value sharing. For encoder hooks, wrapping them with @cbor2.shareable_encoder is enough. Decoder hooks are slightly more complex. In order to support cyclic references, the decoder must construct a “raw” instance of the target class (usually using __new__()) and then separately decoding and applying the object’s state.

See the docstrings of cbor2.CBOREncoder, cbor2.CBORDecoder and @cbor2.shareable_encoder for details.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cbor2-3.0.3.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

cbor2-3.0.3-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cbor2-3.0.3.tar.gz.

File metadata

  • Download URL: cbor2-3.0.3.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cbor2-3.0.3.tar.gz
Algorithm Hash digest
SHA256 9c6d8c1d1db0b00abe60eb36e243b2b6b8a8c886f1f16f2bbb7aa7ecf8677a8f
MD5 c27b1f08d3a16946d1760c2323b09a80
BLAKE2b-256 b9afe1a774fcc79e905a84d23ab43af9c818e38dea66d595a57befb1cff0cd9f

See more details on using hashes here.

File details

Details for the file cbor2-3.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cbor2-3.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f81f5e16656b264a98319b725fbc1fe0e8a8402b54468d4f00e1f0924c100819
MD5 788a41296f638ebbb3daf96e1a66257f
BLAKE2b-256 c522881eaecb1c65cdbb2c2178086a04bac763090254046adc627cf989216f5f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page