OCaml YAML library

I know of ocaml-yaml, but it relies on a C FFI binding. I would like to avoid this in order to ensure I can compile to JS and maintain consistent behaviour. (Depending on some JS-native yaml library for JS-targeted builds would work for 99% of cases, but variance in yaml handling between it and ocaml-yaml would inevitably cause all sorts of headaches, e.g. https://arp242.net/yaml-config.html#its-not-portable)

So, is anyone aware of a YAML library similar to ocaml-yaml that doesn’t rely upon any FFI bindings?

1 Like

In general, implementing full Yaml from scratch is a big endeavour, and not a lot of fun unless you really need it. However, I would be willing to accept a pure OCaml version of the JSON-compatible subset as a PR into ocaml-yaml. This would cover the vast majority of usecases of Yaml, and could be fuzz tested alongside the existing C bindings to ensure that their behaviour is the same.

I’m only aware of @kit-ty-kate starting a pure Yaml implementation; noone else has been brave enough to try :wink:


Is there a good YAML test suite out there like the one I found for JSON? The daunting thing about writing a full-featured YAML parser/formatter is the test suite.

YAML does have an official test suite (https://github.com/yaml/yaml-test-suite), but it labors under the same objections that apply to the specification AFAIK, i.e. fundamental security vulnerabilities and JSON incompatibilities that have been discussed in depth elsewhere.

That said, YAML is indeed challenging to implement (even if you aren’t attempting to align with upstream test suites like the one linked above), which is at least part of why so many languages primary YAML support libraries (like ocaml-yaml) very reasonably decide to simply use an existing library via whatever FFI mechanisms they have available to them.

1 Like