cbor2-cli
Inspect, convert and debug CBOR
(RFC 8949) from the terminal. This
crate installs the cbor command, built on
cbor2.
English | 简体中文
Or install it from the ldclabs Homebrew tap:
Usage: cbor [COMMAND] [INPUT]
Commands:
(none) Show each CBOR item as one line of diagnostic notation (§8)
decode Convert CBOR items to pretty-printed JSON, or to
pretty-printed diagnostic notation with --diag
encode Convert JSON values to CBOR items
validate
Validate one or more complete CBOR items
Why cbor2-cli
| Need | Command support |
|---|---|
| Inspect pasted CBOR | Run cbor <hex-or-base64> to render RFC 8949 diagnostic notation. |
| Preserve wire details | Bare cbor captures each item as raw bytes, so indefinite lengths, segmented strings, undefined and simple values remain visible. |
| Decode for JSON tools | cbor decode pretty-prints CBOR as JSON, one document per item. |
| Encode fixtures | cbor encode turns JSON values into CBOR bytes; cbor encode --hex prints copyable lowercase hex for agents and docs. |
| Work with sequences | Multiple JSON values become a CBOR sequence; CBOR sequences decode item by item. |
| Validate inputs | cbor validate <hex-or-file> checks one or more complete CBOR items and prints valid on success. |
| Script reliably | Data errors exit with status 1, usage errors with status 2. |
INPUT is a file path, a hex string (optionally 0x-prefixed), a
base64/base64url string, or - for stdin; stdin is the default. An
argument containing a path separator is always a file path. Everything
streams: multiple JSON values become a CBOR sequence (RFC 8742), and a
CBOR sequence becomes one output document or line per item. Data errors
exit with status 1, usage errors with status 2.
Agent-friendly usage
For code agents, prefer text-first commands unless a pipeline needs raw bytes:
|
Use raw cbor encode only when piping directly into another binary command.
Use cbor encode --hex when the result needs to be pasted into a test, a
prompt, a review comment or another cbor invocation.
Show: cbor
The everyday command. It renders each item as the human-readable text
form of RFC 8949 §8 — what CBOR specs and test vectors are written in —
and it is exact: every item is captured as its wire bytes, so
indefinite-length items keep their _ markers, segmented strings appear
as (_ ...), undefined and unassigned simple values appear as
themselves, byte strings render as h'...' and bignums print as plain
integers, exactly as in RFC 8949 Appendix A.
}
}
)
}
decode
cbor decode decodes each item into the data model and pretty-prints it
as JSON, or — with -d/--diag — as indented diagnostic notation.
Unlike the bare cbor, this re-spells the item (indefinite lengths and
non-preferred encodings are not preserved).
{
}
{
}
JSON conversion is best-effort where CBOR is richer: byte strings become
lowercase hex strings, non-string map keys are JSON-encoded into strings,
non-finite floats and undefined become null, integers beyond the
64-bit ranges become strings, and tags are dropped (keeping the inner
value).
encode
cbor encode reads JSON text (from a file or stdin) and writes each
value as a CBOR item. Add --hex for copyable lowercase hex text:
| |
}
| |
|
validate
cbor validate checks that the input contains one or more complete CBOR
items. It prints valid on success, exits with status 1 for malformed data
and status 2 for usage errors:
License
Licensed under the MIT License.