Streaming YAML 1.2 codec library for OCaml
OCaml 99%
1%
Dune <1%

README.md

Yaml #

Yaml is a type-safe YAML 1.2 codec library for OCaml.

Design #

Yaml provides bidirectional encoding and decoding using a combinator-based approach inspired by Jsont, and mirrors the shape of the related toml, sexp, and json packages.

Each codec 'a Yaml.t defines both a decoder and an encoder, composed through combinators to build complex types from simple primitives.

Packages #

  • nox-yaml — core value types, codec combinators, encode/decode API. Modules are exposed as Yaml.*.
  • nox-yaml.bytesrw — streaming YAML 1.2 parser and emitter using Bytesrw (Yaml_bytesrw).
  • nox-yaml.json — use existing Json.Codec.t codecs (from the nox-json library) to decode and encode YAML (Yaml_json). Replacement for the deprecated yamlt package.

Installation #

Install with opam:

$ opam install nox-yaml

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-yaml

For Eio integration, wrap an Eio flow as a Bytesrw reader/writer with bytesrw-eio and call Yaml.decode / Yaml.encode directly.

Example #

Codecs describe both the YAML shape and the OCaml value they produce:

type service = { hostname : string; port : int }

let service_codec =
  let open Yaml.Codec in
  Mapping.(
    map (fun hostname port -> { hostname; port })
    |> mem "hostname" string ~enc:(fun s -> s.hostname)
    |> mem "port" int ~enc:(fun s -> s.port)
    |> seal)

let example =
  Yaml.of_string service_codec "hostname: localhost\nport: 8080"