CBOR (Concise Binary Object Representation, RFC 8949) is rapidly becoming the go-to binary serialization format for resource-constrained environments. Unlike JSON, which wastes bandwidth on repeated field names and whitespace, CBOR encodes data in a compact binary form while preserving JSON’s flexible data model — objects, arrays, strings, numbers, booleans, and null. It is the backbone of the IETF’s COSE (CBOR Object Signing and Encryption) standard and is used in FIDO2 WebAuthn, W3C Web of Things, and Constrained Application Protocol (CoAP).
For C and C++ developers working in embedded systems, IoT firmware, or protocol implementation, choosing the right CBOR library is critical. In this guide, we compare four open-source CBOR libraries — libcbor, tinycbor, QCBOR, and libbinn — across API design, memory footprint, standard compliance, and integration ease.
What Makes CBOR Different from JSON and Protocol Buffers?
| Feature | JSON | Protocol Buffers | CBOR |
|---|---|---|---|
| Encoding | Text (UTF-8) | Binary (schema) | Binary (self-describing) |
| Size Overhead | High (field names, punctuation) | Low | Very Low |
| Schema Required | No | Yes (.proto) | No |
| Streaming Parsing | Limited | Yes | Yes |
| Integer Representation | IEEE 754 double only | Fixed/variable encoding | Native int64/uint64/bigint/float |
| Map Key Types | String only | Integer/string | Any CBOR type |
| Binary Data | Base64-encoded | Native bytes | Native byte strings |
| Standard | RFC 8259 | Google-defined | RFC 8949 (IETF) |
| Ideal For | Web APIs, config | RPC, microservices | IoT, firmware, security tokens |
CBOR’s key advantage is that it is self-describing (no schema required) yet compact (binary encoding). This makes it ideal for firmware update packages, sensor telemetry, and cryptographic message formats.
libcbor: The Reference Implementation
libcbor (github.com/PJK/libcbor) is the most feature-complete and standards-compliant CBOR implementation for C. Written by Petr Krajča, it closely tracks the RFC 8949 specification and supports the full CBOR data model including indefinite-length strings, big integers (bignum), decimal fractions, and tags.
| |
Integration via CMake or pkg-config:
| |
| |
Key strengths:
- Complete RFC 8949 compliance (all major types, all tags)
- Streaming parser API for memory-constrained environments
- Reference counting for safe memory management
- Active maintenance (last commit April 2026)
- Thorough test suite with > 1,200 test vectors
Limitations: Larger code size (~40KB compiled) compared to tinycbor, C-only (no C++ convenience wrappers), and the reference-counting API has a steeper learning curve.
tinycbor: Intel’s Embedded-First CBOR
tinycbor (github.com/intel/tinycbor) was developed by Intel for resource-constrained embedded systems. It is designed to compile to minimal binary size and uses a simple iterator-based API that avoids dynamic memory allocation.
| |
Key strengths:
- Tiny binary footprint (~8KB compiled with
-Os) - Iterator-based pull parser — zero heap allocations
- Supports C89 for maximum embedded compatibility
- Active maintenance by Intel (last commit June 2026)
- Used in OpenThread, Zephyr RTOS, and mbed OS
Limitations: C-only API (verbose for C++ projects), fewer tag type helpers than libcbor, and no built-in schema validation.
QCBOR: Security and Standards Compliance
QCBOR (github.com/laurencelundblade/QCBOR) stands out for its focus on security and standards compliance. Developed by Laurence Lundblade (a former Qualcomm engineer and IETF COSE co-author), QCBOR is designed specifically for cryptographic applications — it is used in the IETF SUIT (firmware update manifest) and COSE (CBOR Object Signing and Encryption) standards.
| |
| |
Key strengths:
- Purpose-built for cryptographic standards (COSE, SUIT, CWT)
- Spiffy decode API — type-safe, float-compatible decoding
- Detailed error codes for robust error handling
- Designed for deterministic encoding (critical for digital signatures)
- No dynamic memory allocation in the core encode/decode path
Limitations: Narrower focus than libcbor (prioritizes crypto use cases), no streaming encoder, and the UsefulBuf abstraction takes time to learn.
libbinn: A Lighter Alternative
libbinn (github.com/liteserver/binn) is not strictly a CBOR library — it uses its own compact binary format that is conceptually similar and was designed to be even simpler than CBOR. It uses a minimal API surface and is popular in game development and real-time systems.
| |
Key strengths:
- Extremely simple API — three functions cover 90% of use cases
- Faster than CBOR for simple data types (no tag system overhead)
- Portable (no external dependencies)
- Good for inter-process communication and save files
- Single .c/.h pair
Limitations: Not CBOR-compliant (proprietary format), slower for complex nested structures, no streaming support, and less active maintenance (last update July 2025).
Library Comparison Matrix
| Criterion | libcbor | tinycbor | QCBOR | libbinn |
|---|---|---|---|---|
| GitHub Stars | 401 | 615 | 231 | 481 |
| Standard | Full RFC 8949 | Full RFC 8949 | RFC 8949 (COSE focus) | Proprietary |
| Binary Size | ~40KB | ~8KB | ~15KB | ~10KB |
| Allocation | Heap (ref-counted) | Stack/static | Stack/static | Heap |
| Streaming Parser | Yes | Yes (pull parser) | No | No |
| Tag Support | All RFC tags | Common tags | COSE-specific | None |
| Floating Point | Half/single/double | Half/single/double | Half/single/double | Single/double |
| Big Integer | Yes (bignum) | Yes (biguint) | Yes | No |
| Indefinite Length | Yes | Yes | No | No |
| C++ Wrapper | No (C-only) | No (C-only) | No (C-only) | C++ compatible |
Integration Patterns for IoT Firmware
When deploying CBOR in embedded Linux or bare-metal environments, the key concerns are memory allocation and binary size. Here is a typical firmware message pipeline using tinycbor:
| |
For broader protocol integration, see our binary serialization frameworks guide comparing Rust-native alternatives, and our schema-based serialization comparison covering Protocol Buffers, Cap’n Proto, and FlatBuffers.
Choosing Based on Your Use Case
- IoT firmware with tight memory constraints: tinycbor — smallest binary, pull parser with zero heap allocation
- Cryptographic protocol implementation (COSE, CWT): QCBOR — designed specifically for these standards
- General-purpose CBOR with full RFC compliance: libcbor — most complete implementation
- Game development or IPC with minimal API surface: libbinn — simplest API, fast for simple data
For JSON-based workflows, see our self-hosted JSON parser comparison covering simdjson, RapidJSON, and orjson.
FAQ
Why use CBOR instead of JSON for IoT?
JSON requires text encoding (5 bytes for "temp", plus quotes and colons), while CBOR encodes the same data in 1-2 bytes per field. For a typical sensor payload of 100 bytes in JSON, CBOR often reduces it to 30-50 bytes — critical when transmitting over LPWAN (LoRaWAN, NB-IoT) where every byte costs battery life.
Is CBOR faster to parse than JSON?
Yes, significantly. Binary integer parsing is O(1) while JSON number parsing requires scanning ASCII digits, handling scientific notation, and converting to binary. Benchmarks show CBOR parsing is 3-5x faster than equivalent JSON for typical payloads.
Can CBOR handle cyclic data structures?
No. Like JSON, CBOR is a tree-structured format and does not support object references or cycles. Use a graph serialization library or design your data structures to avoid cycles.
What is CBOR diagnostic notation?
CBOR diagnostic notation is a human-readable text representation defined in RFC 8949 Appendix G. For example, the CBOR bytes ¡ÿ map to {1: 255} in diagnostic notation. All four libraries support diagnostic output for debugging.
Does libbinn interoperate with standard CBOR implementations?
No. libbinn uses its own binary format that is not CBOR-compliant. If interoperability with CBOR-speaking services (FIDO2 servers, CoAP endpoints) is required, use libcbor, tinycbor, or QCBOR instead.
💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com