Skip to main content

Pure Python CBOR (de)serializer with extensive tag support

Project description

Build Status Code Coverage Documentation Status

About

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

It is implemented in pure python with an optional C backend.

On PyPy, cbor2 runs with almost identical performance to the C backend.

Features

  • Simple api like json or pickle modules.

  • Support many CBOR tags with stdlib objects.

  • Generic tag decoding.

  • Shared value references including cyclic references.

  • Optional C module backend tested on big- and little-endian architectures.

  • Extensible tagged value handling using tag_hook and object_hook on decode and default on encode.

  • Command-line diagnostic tool, converting CBOR file or stream to JSON python -m cbor2.tool (This is a lossy conversion, for diagnostics only)

  • Thorough test suite.

Installation

pip install cbor2

Requirements

  • cPython==2.7 or cPython>=3.5 or PyPy (Python 2.7 and 3.5 support will be removed with release 6.0.0)

  • C-extension: Any C compiler that can build Python extensions. Any modern libc with the exception of Glibc<2.9

Building the C-Extension

To force building of the optional C-extension, set OS env CBOR2_BUILD_C_EXTENSION=1. To disable building of the optional C-extension, set OS env CBOR2_BUILD_C_EXTENSION=0. If this environment variable is unset, setup.py will default to auto detecting a compatible C library and attempt to compile the extension.

Usage

Basic Usage

Command-line Usage

python -m cbor2.tool converts CBOR data in raw binary or base64 encoding into a representation that allows printing as JSON. This is a lossy transformation as each datatype is converted into something that can be represented as a JSON value.

Usage:

# Pass hexadecimal through xxd.
$ echo a16568656c6c6f65776f726c64 | xxd -r -ps | python -m cbor2.tool --pretty
{
    "hello": "world"
}
# Decode Base64 directly
$ echo ggEC | python -m cbor2.tool --decode
[1, 2]
# Read from a file encoded in Base64
$ python -m cbor2.tool -d tests/examples.cbor.b64
{...}

It can be used in a pipeline with json processing tools like jq to allow syntax coloring, field extraction and more.

CBOR data items concatenated into a sequence can be decoded also:

$ echo ggECggMEggUG | python -m cbor2.tool -d --sequence
[1, 2]
[3, 4]
[5, 6]

Multiple files can also be sent to a single output file:

$ python -m cbor2.tool -o all_files.json file1.cbor file2.cbor ... fileN.cbor

Security

This library has not been tested against malicious input. In theory it should be as safe as JSON, since unlike pickle the decoder does not execute any code.

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-5.2.0.tar.gz (81.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: cbor2-5.2.0.tar.gz
  • Upload date:
  • Size: 81.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.10

File hashes

Hashes for cbor2-5.2.0.tar.gz
Algorithm Hash digest
SHA256 a33aa2e5534fd74401ac95686886e655e3b2ce6383b3f958199b6e70a87c94bf
MD5 0940aa8bfd1a07f06a983bb6dc78f1ca
BLAKE2b-256 0215f4d7fb47753099fcd9d7f7c81920d422a3ab2e710400ec0f4a1b511b51ae

See more details on using hashes here.

Supported by

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