Skip to main content

CBOR (de)serializer with extensive tag support

Project description

Testing Status Publish Status Code Coverage Documentation Status

About

This library provides encoding and decoding for the Concise Binary Object Representation (CBOR) (RFC 8949) serialization format. The specification is fully compatible with the original RFC 7049. 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.

  • String references compact encoding with repeated strings replaced with indices.

  • 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

  • Python >= 3.8 (or PyPy3 3.8+)

  • 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

The provided command line tool (cbor2) 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.

The tool can alternatively be invoked with python -m cbor2.tool.

Usage:

# Pass hexadecimal through xxd.
$ echo a16568656c6c6f65776f726c64 | xxd -r -ps | cbor2 --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 | cbor2 -d --sequence
[1, 2]
[3, 4]
[5, 6]

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

$ cbor2 -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.6.5.tar.gz (100.9 kB view hashes)

Uploaded Source

Built Distributions

cbor2-5.6.5-py3-none-any.whl (23.8 kB view hashes)

Uploaded Python 3

cbor2-5.6.5-cp313-cp313-win_amd64.whl (66.8 kB view hashes)

Uploaded CPython 3.13 Windows x86-64

cbor2-5.6.5-cp313-cp313-musllinux_1_2_x86_64.whl (266.3 kB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp313-cp313-musllinux_1_2_aarch64.whl (264.7 kB view hashes)

Uploaded CPython 3.13 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (269.1 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (275.9 kB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp313-cp313-macosx_11_0_arm64.whl (67.7 kB view hashes)

Uploaded CPython 3.13 macOS 11.0+ ARM64

cbor2-5.6.5-cp313-cp313-macosx_10_13_x86_64.whl (67.6 kB view hashes)

Uploaded CPython 3.13 macOS 10.13+ x86-64

cbor2-5.6.5-cp312-cp312-win_amd64.whl (66.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

cbor2-5.6.5-cp312-cp312-musllinux_1_2_x86_64.whl (266.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp312-cp312-musllinux_1_2_aarch64.whl (264.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (270.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (276.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp312-cp312-macosx_11_0_arm64.whl (67.6 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

cbor2-5.6.5-cp312-cp312-macosx_10_13_x86_64.whl (67.6 kB view hashes)

Uploaded CPython 3.12 macOS 10.13+ x86-64

cbor2-5.6.5-cp311-cp311-win_amd64.whl (66.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

cbor2-5.6.5-cp311-cp311-musllinux_1_2_x86_64.whl (247.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp311-cp311-musllinux_1_2_aarch64.whl (249.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (249.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (260.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp311-cp311-macosx_11_0_arm64.whl (67.4 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

cbor2-5.6.5-cp311-cp311-macosx_10_9_x86_64.whl (66.5 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

cbor2-5.6.5-cp310-cp310-win_amd64.whl (66.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cbor2-5.6.5-cp310-cp310-musllinux_1_2_x86_64.whl (240.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp310-cp310-musllinux_1_2_aarch64.whl (241.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (242.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (253.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp310-cp310-macosx_11_0_arm64.whl (67.4 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

cbor2-5.6.5-cp310-cp310-macosx_10_9_x86_64.whl (66.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

cbor2-5.6.5-cp39-cp39-win_amd64.whl (66.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cbor2-5.6.5-cp39-cp39-musllinux_1_2_x86_64.whl (239.5 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp39-cp39-musllinux_1_2_aarch64.whl (240.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (240.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (252.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp39-cp39-macosx_11_0_arm64.whl (67.4 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

cbor2-5.6.5-cp39-cp39-macosx_10_9_x86_64.whl (66.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

cbor2-5.6.5-cp38-cp38-win_amd64.whl (66.3 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cbor2-5.6.5-cp38-cp38-musllinux_1_2_x86_64.whl (254.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

cbor2-5.6.5-cp38-cp38-musllinux_1_2_aarch64.whl (254.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARM64

cbor2-5.6.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (256.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cbor2-5.6.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (264.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

cbor2-5.6.5-cp38-cp38-macosx_11_0_arm64.whl (67.5 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

cbor2-5.6.5-cp38-cp38-macosx_10_9_x86_64.whl (66.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

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