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.

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.1.0.tar.gz (114.2 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.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: msgspec_cbor-0.1.0.tar.gz
  • Upload date:
  • Size: 114.2 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.1.0.tar.gz
Algorithm Hash digest
SHA256 f68fe307a64f442694c32f8320adda32c5fa43144b222a83b057cb27826a2eb3
MD5 cc99417ae3675be383125e6f945c8fbb
BLAKE2b-256 8634910e022a39b536dcee0edb803eb5eee81a169e2cb743858234e1b4cae2e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgspec_cbor-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: msgspec_cbor-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 92cc6dd8016af58ada2bc734ccf716ac70fe1b9e78f1ef2982afc410a6700e17
MD5 af7d4cbda609265f928c67cc5bfc621e
BLAKE2b-256 19701ef82c3a79805b9f5996a07d334186f688c8592ade07874c86123813512e

See more details on using hashes here.

Provenance

The following attestation bundles were made for msgspec_cbor-0.1.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