CBORDex is a Swift Package that provides a lightweight CBOR encoder/decoder pair. It focuses on ergonomics for app and service code that needs to produce deterministic, standards-aligned CBOR for tasks such as signing, transport framing, or diagnostic tooling. The implementation follows RFC 8949 and offers an opt-in canonical mode for lexicographic map ordering and preferred floating-point encodings.
Add the package to your Package.swift manifest:
.package(url: "https://github.com/codelynx/CBORDex.git", from: "0.1.0")Then depend on CBORDex in your target:
.target(
name: "App",
dependencies: ["CBORDex"]
)Start by constructing a CBORValue tree and using CBOREncoder to serialise it:
let value: CBORValue = .map([
(.textString("device"), .unsigned(42)),
(.textString("ok"), .bool(true))
])
var options = CBOREncoder.Options()
options.canonicalMapOrdering = true
let encoder = CBOREncoder(options: options)
let data = try encoder.encode(value)
// data is ready for transmission or signingTo parse binary CBOR, hand the data buffer to CBORDecoder:
let decoder = CBORDecoder()
let decoded = try decoder.decode(data)The resulting CBORValue is Equatable, making test assertions straightforward. Extend the enum with helpers if you want domain-specific projections.
Canonical mode currently covers map ordering and preferred floating-point forms. Other determinism rules from RFC 8949 (e.g., prohibiting duplicate keys or indefinite-length items) are not enforced yet, so layer additional validation if your protocol demands fully canonical CBOR.
The package is split into three main files under Sources/CBORDex/:
CBORValue.swiftdefines the strongly typed in-memory model for CBOR items.CBOREncoder.swiftwalks aCBORValuetree and emits RFC 8949-compliant bytes, including optional canonical ordering and preferred floating-point encodings.CBORDecoder.swiftimplements a streaming cursor that reconstructsCBORValueinstances from binary input and surfaces errors such as trailing data or invalid UTF-8.
Tests under Tests/CBORDexTests/ exercise encoding, decoding, canonical ordering, and error paths via XCTest.
- Encoding/decoding semantics are based on RFC 8949.
- Canonical behaviors target RFC 8949 §4.2 (preferred serialization); duplicate-key checks and rejection of indefinite-length items are left to callers for now.
- Streaming (incremental) decoding/encoding is not implemented; all data is materialised as
CBORValuebefore processing.
- Swift tools version: 6.2
- Minimum supported platforms: macOS 11, iOS 14, tvOS 14, watchOS 7
- No external dependencies beyond Foundation
This package was bootstrapped with the help of an OpenAI Codex agent working alongside a human developer. The agent handled code exploration, implementation, documentation, and testing tasks through the Codex CLI, while the human collaborator reviewed direction, requested adjustments, and made product decisions. That pairing model keeps iteration fast yet supervised: the agent performs repeatable chores (formatting, SwiftPM builds, regression tests) and summarizes results so the developer can focus on design trade-offs and RFC alignment.
I tried the same concept project based on Anthropic Claude code. You may visit to see the different and the similarities between the two projects. https://github.com/codelynx/CBORCla
Distributed under the MIT License. See LICENSE for details.