cbor
A command-line tool for encoding and decoding CBOR (Concise Binary Object Representation, RFC 8949) using serde. Convert between CBOR and JSON, YAML, and TOML, inspect the contents of CBOR files, and pipe it all through standard Unix tools.
CBOR is a compact binary data format with a data model close to JSON's but with
smaller payloads, richer types (byte strings, bignums, tags), and fast binary
parsing. cbor makes working with it as easy as jq/yq make working with
their formats.
Why
- Pipe-friendly. Reads files or stdin, writes stdout. Composes with
cat,curl,grep, and friends. - Multi-format. Convert JSON, YAML, TOML, and CBOR freely — all four directions.
- Streaming. Handles arbitrarily long inputs one item at a time, and supports CBOR sequences (multiple concatenated items in one stream, RFC 8742).
- Schema-less. Works on any data via serde's dynamic
Valuetype. No structs, no setup. - Deep inspection. Renders CBOR in Concise Diagnostic Notation (RFC 8949 §8) — human-readable, standard, and CBOR-aware (byte strings, tags, bignums).
- Delimiters. Custom output delimiters for joining multiple items.
Installation
Homebrew
Cargo
Debian
Installs the public key, downloads the latest .deb file, and installs it:
|
Build from source
# Binary is at target/release/cbor
Usage
The three subcommands are designed to chain:
import convert JSON/YAML/TOML/CBOR -> CBOR
export convert CBOR -> JSON/YAML/TOML/CBOR
inspect render CBOR -> Concise Diagnostic Notation
Import (encode to CBOR)
Format is auto-detected from the file extension (.json, .yaml, .yml, .toml). To force it, or when reading
from stdin:
|
Export (decode from CBOR)
# Pretty-printed JSON
Convert between formats
Because both commands stream, you can chain them to convert any format to any other through CBOR as an intermediate:
# JSON -> YAML
|
# YAML -> TOML
|
Inspect (debug)
inspect renders each CBOR item in Concise Diagnostic Notation — a
human-readable form that preserves CBOR-specific details JSON debug output
would lose, like byte strings:
|
}
Inspect a CBOR file directly:
CDN distinguishes CBOR types that JSON can't represent:
| CBOR type | CDN output |
|---|---|
| Byte string | h'0102' |
| Tagged unsigned bignum | 16909060 |
| Integer | 42 |
| Text string | "hello" |
| Array | [1, 2, 3] |
| Map | {"a": "b"} |
| Null | null |
| Boolean | true / false |
Multiple files and streams
Pass multiple files; each is processed in turn:
Concatenated items from stdin are supported too — useful for CBOR sequences and for joining multiple JSON documents:
|
Custom delimiters
When exporting multiple items, join them with a custom delimiter. The default
is \n (for YAML, the document separator ---):
# Comma-separated JSON values
# Shorthand
The escapes \n and \t are interpreted literally.
Full reference
cbor [OPTIONS] [COMMAND]
Commands:
inspect Deep inspection of CBOR files, for debugging, learning, or repairing
import Convert a file of some other type to CBOR
export Convert CBOR files to some other type
help Print help for a command
Options:
-v, --verbose... Increase verbosity (repeatable)
-d, --delimiter <DELIMITER> Delimiter between multiple output items
-h, --help Print help
-V, --version Print version
Each subcommand has its own --help:
Library
The core conversion functions are exposed as a library, in case you want to embed them in another Rust project:
use import_from_reader;
// Convert a JSON stream to CBOR.
let json = br#"{"key": "value"}"#;
let mut cbor = Vecnew;
import_from_reader.unwrap;
Library functions return anyhow::Result, so errors propagate naturally:
use Context;
use export_from_reader;
export_from_reader
.context?;
See the src/ modules for the full API surface:
| Module | Purpose |
|---|---|
import |
Encode JSON/YAML/TOML/CBOR streams to CBOR |
export |
Decode CBOR streams to JSON/YAML/TOML/CBOR |
inspect |
Render CBOR as Concise Diagnostic Notation |
files |
Path helpers: format detection, existence checks |
config |
CLI definition (clap) |
Standards
- RFC 8949 — Concise Binary Object Representation
- RFC 8742 — CBOR Sequences (concatenated items in one stream)
- RFC 8949 §8 — Concise Diagnostic Notation
Development
Examples of every supported conversion are in examples/test.sh.
Building release packages
Cross-compilation and Debian packaging are configured in
.github/workflows/rust_build.yml. Local .deb build:
License
Apache-2.0. See LICENSE.