CBOR codecs for OCaml
OCaml 98.1%
Dune 0.4%
Other 1.5%
35 2 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-cbort https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-cbort
git@git.recoil.org:gazagnaire.org/ocaml-cbort git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-cbort

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

cbort#

Native CBOR codec with type-safe combinators for OCaml.

Overview#

cbort provides type-safe CBOR (RFC 8949) encoding and decoding using a combinator-based approach. Define codecs once and use them for both encoding and decoding OCaml values to and from CBOR binary format.

Features#

  • Type-safe codecs: Define encoding/decoding rules once, use them bidirectionally
  • Combinator-based API: Compose complex codecs from simple building blocks
  • RFC 8949 compliant: Full support for CBOR data model including tags, maps, arrays, and all numeric types
  • Arbitrary precision integers: Uses Zarith for unlimited integer range
  • Detailed error reporting: Path-aware error messages for debugging decode failures
  • Zero-copy streaming: Built on bytesrw for efficient I/O

Installation#

opam install cbort

Usage#

open Cbort

(* Define a codec for a record type *)
type person = { name : string; age : int }

let person_codec =
  let open Obj in
  let* name = mem "name" (fun p -> p.name) string in
  let* age = mem "age" (fun p -> p.age) int in
  return { name; age }
  |> finish

(* Encode to CBOR bytes *)
let encoded = encode_string person_codec { name = "Alice"; age = 30 }

(* Decode from CBOR bytes *)
let decoded = decode_string person_codec encoded

Documentation#

API documentation is available at the project homepage.

Licence#

ISC License. See LICENSE.md for details.