Skip to main content

CBOR encoding and decoding for msgspec, backed by cbor2.

Project description

msgspec-cbor

CBOR (RFC 8949) encoding and decoding for msgspec, backed by cbor2.

msgspec-cbor is a thin, pure-Python bridge that mirrors the msgspec.toml / msgspec.yaml wrapper modules: it lowers objects with msgspec.to_builtins, hands them to cbor2's native encoder, and validates decoded output back into your types with msgspec.convert. Rich types (datetime, bytes, Decimal, UUID, …) travel as real CBOR tags — never base64 or ISO strings.

Install

pip install msgspec-cbor

Usage

import msgspec
import msgspec_cbor

class Point(msgspec.Struct):
    x: int
    y: int

data = msgspec_cbor.encode(Point(1, 2))        # -> bytes
point = msgspec_cbor.decode(data, type=Point)  # -> Point(x=1, y=2)

Omit type= to decode into plain CBOR/Python objects with no validation.

Supported types

The default type table is the intersection of what cbor2 encodes as a native CBOR tag and what msgspec.convert accepts back — so every rich type round-trips losslessly.

Python type On the wire Notes
int, float, bool, str, None native CBOR scalar
bytes, bytearray byte string (major type 2) not base64
list, tuple array (major type 4)
dict, msgspec.Struct, dataclass, attrs map (major type 5)
datetime.datetime tag 0 tz-aware only; naive raises msgspec.EncodeError
datetime.date tag 1004
decimal.Decimal tag 4
uuid.UUID tag 37
datetime.time text string not natively CBOR; string fallback
set, frozenset array (major type 4) decode into a set[...]-typed field to coerce back
fractions.Fraction, datetime.timedelta not built in supply enc_hook / dec_hook if needed

Malformed input raises msgspec.DecodeError; a shape mismatch against type= raises msgspec.ValidationError. cbor2's own exceptions never escape the public API.

API

def encode(obj, *, enc_hook=None, order=None) -> bytes: ...
def decode(buf, *, type=Any, strict=True, dec_hook=None) -> Any: ...

The signatures mirror msgspec.toml / msgspec.yaml, so this is a drop-in sibling format.

order accepts msgspec's "deterministic" / "sorted" (lexicographic) plus "canonical", which emits CBOR canonical length-first key ordering (cbor2's canonical=True) for byte-exact wire formats.

Not affiliated

This is an independent bridge and is not affiliated with or endorsed by the msgspec project.

Attribution

msgspec-cbor is MIT licensed and copies no code from either project.

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

msgspec_cbor-0.2.0.tar.gz (114.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msgspec_cbor-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file msgspec_cbor-0.2.0.tar.gz.

File metadata

  • Download URL: msgspec_cbor-0.2.0.tar.gz
  • Upload date:
  • Size: 114.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for msgspec_cbor-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4e340d01e533c9190998dd54a460d0c05306f1afb04eccedf77be1c3962c83b5
MD5 5947bb21a6995eef318bcec0a9736fb3
BLAKE2b-256 216dd74123817748e5588020ce55ad80b8c629bc29250e19fa4c1fe113879bf3

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgspec_cbor-0.2.0.tar.gz:

Publisher: ci.yaml on JPHutchins/msgspec-cbor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file msgspec_cbor-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: msgspec_cbor-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for msgspec_cbor-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87e506b067bc8a7a0ae60bc307f48f3258b0ace4bd349023ea97d5e6e857d48a
MD5 44ec81ed6b89d7b910d2d16782e6ceb6
BLAKE2b-256 39eb2da515d6356f972540178fefa09adad57a375604a2fdddbee3c81c7173bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgspec_cbor-0.2.0-py3-none-any.whl:

Publisher: ci.yaml on JPHutchins/msgspec-cbor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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