$ show agronholm/cbor2
This library provides encoding and decoding for the Concise Binary Object Representation (CBOR) serialization format (RFC 8949). It features a hybrid architecture with core implementation in Rust for performance and safety, exposed through Python bindings. The library supports advanced CBOR features including value sharing for cyclic references, string referencing for deduplication of repeated strings, canonical encoding, and extensive semantic tag support for types like dates, UUIDs, IP addresses, and more. It provides both a programmatic API and a command-line diagnostic tool.
The LLM identified 4 files worth highlighting for reuse:
A complete, iterative CBOR decoder implemented in Rust that avoids stack overflow by using an explicit stack instead of recursion. It implements full CBOR specification support including value sharing (tags 28/29), string referencing (tag 25), and all semantic tags. The decoder properly handles edge cases like indefinite-length items, duplicate keys, and provides detailed error messages wrapped in CBORDecodeError. The implementation demonstrates advanced Rust programming with careful memory management and state tracking.
A full-featured CBOR encoder in Rust that supports all major and semantic tags. It implements canonical encoding (sorting map keys by encoded length), value sharing for efficient serialization of repeated/cyclic references, and string referencing for deduplication of repeated strings. The encoder handles special cases like float canonicalization to smallest possible representation, complex numbers (tag 43000), and various numeric types. It demonstrates sophisticated Rust programming with careful buffer management and state tracking for sharing mechanisms.
A sophisticated string referencing system that maintains synchronized namespaces between encoder and decoder. It determines when to create references based on string length thresholds (different ranges for different namespace indices) and properly handles both string and byte string references. The mechanism ensures that references are only created when they actually save space, and maintains correctness even with large strings and high index values. This solves the common problem of inefficient serialization of repeated string data.
A robust implementation of CBOR value sharing (RFC 7049 section 2.2) that handles cyclic data structures. It uses a two-phase decoding approach where shareable values are first created as empty containers, then populated after their contents are decoded. The mechanism properly handles initialization order, detects uninitialized shared values, and prevents encoding cycles when sharing is disabled. This solves the challenging problem of serializing recursive data structures.