Yamlrw - A Pure OCaml YAML Parser and Emitter
Yamlrw is a pure OCaml implementation of YAML 1.1/1.2 parsing and emission. It provides both a high-level JSON-compatible interface and a lower-level streaming API for fine-grained control.
Quick Start
Parse a YAML string:
let value = Yamlrw.of_string "name: Alice\nage: 30" in
match value with
| `O [("name", `String "Alice"); ("age", `Float 30.)] -> ...
| _ -> ...Serialize to YAML:
let yaml = `O [("name", `String "Bob"); ("active", `Bool true)] in
let s = Yamlrw.to_string yaml in
(* "name: Bob\nactive: true\n" *)Use the Util module for convenient access:
let name = Yamlrw.Util.(get_string (get "name" value)) in
let age = Yamlrw.Util.(get_int (get "age" value)) inRelated Libraries
Yamlrw_unix- Unix file I/O integrationYamlrw_eio- Eio async file and flow operationsYamlt- YAML codec using Jsont type descriptions (higher-level)
Error Handling
module Error : sig ... endexception Yamlrw_error of Error.tRaised on parse or emit errors.
Core Types
type value = [ | `Null(*YAML null, ~, or empty values
*)| `Bool of bool(*YAML booleans (true, false, yes, no, on, off)
*)| `Float of float(*All YAML numbers (integers stored as floats)
*)| `String of string(*YAML strings
*)| `A of value list(*YAML sequences/arrays
*)| `O of (string * value) list(*YAML mappings/objects with string keys
*)
]type yaml = [ | `Scalar of Scalar.t(*YAML scalar value with style and metadata
*)| `Alias of string(*Alias reference to an anchored node
*)| `A of yaml Sequence.t(*YAML sequence with style and metadata
*)| `O of (yaml, yaml) Mapping.t(*YAML mapping with style and metadata
*)
]Full YAML representation preserving anchors, tags, and aliases.
This type is structurally equivalent to Yaml.t. Use this when you need access to YAML-specific features like anchors and aliases for node reuse, type tags for custom types, scalar styles (plain, quoted, literal, folded), and collection styles (block vs flow).
For additional operations, see Yaml, Scalar, Sequence, and Mapping.
type document = {version : (int * int) option;(*Optional YAML version directive (e.g., (1, 2) for YAML 1.2)
*)root : yaml option;(*Root content of the document
*)implicit_start : bool;(*Whether the document start marker (---) is implicit
*)implicit_end : bool;(*Whether the document end marker (...) is implicit
*)
}A YAML document with directives and metadata.
This type is structurally equivalent to Document.t. A YAML stream can contain multiple documents, each separated by document markers.
For additional operations, see Document.
Character Encoding
module Encoding : sig ... endCharacter encoding detection and handling
Parsing
val of_string :
?resolve_aliases:bool ->
?max_nodes:int ->
?max_depth:int ->
string ->
valueParse a YAML string into a JSON-compatible value.
val yaml_of_string :
?resolve_aliases:bool ->
?max_nodes:int ->
?max_depth:int ->
string ->
yamlParse a YAML string preserving full YAML metadata (anchors, tags, etc).
By default, aliases are NOT resolved, preserving the document structure.
val documents_of_string : string -> document listParse a multi-document YAML stream.
Use this when your YAML input contains multiple documents separated by document markers (---).
Formatting Styles
module Scalar_style : sig ... endScalar formatting styles
module Layout_style : sig ... endCollection layout styles
Serialization
val to_buffer :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
?buffer:Buffer.t ->
value ->
Buffer.tSerialize a value to a buffer.
val to_string :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
value ->
stringSerialize a value to a YAML string.
val yaml_to_buffer :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
?buffer:Buffer.t ->
yaml ->
Buffer.tSerialize a full YAML value to a buffer.
val yaml_to_string :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
yaml ->
stringSerialize a full YAML value to a string.
val documents_to_buffer :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
?resolve_aliases:bool ->
?buffer:Buffer.t ->
document list ->
Buffer.tSerialize multiple documents to a buffer.
val documents_to_string :
?encoding:Encoding.t ->
?scalar_style:Scalar_style.t ->
?layout_style:Layout_style.t ->
?resolve_aliases:bool ->
document list ->
stringSerialize multiple documents to a YAML stream.
Buffer Parsing
Parse YAML from a buffer into a JSON-compatible value.
Parse YAML from a buffer preserving full YAML metadata.
Parse a multi-document YAML stream from a buffer.
Conversion
Convert full YAML to JSON-compatible value.
Pretty Printing & Equality
val pp : Format.formatter -> value -> unitPretty-print a value.
Util - Value Combinators
Combinators for working with value values.
This module provides constructors, accessors, and transformations for JSON-compatible YAML values.
module Util : sig ... endStream - Low-Level Event API
Low-level streaming API for event-based YAML processing.
This is useful for:
- Processing very large YAML files incrementally
- Building custom YAML transformers
- Fine-grained control over YAML emission
module Stream : sig ... endInternal Modules
These modules are exposed for advanced use cases requiring fine-grained control over parsing, emission, or data structures.
For typical usage, prefer the top-level functions and Util.
module Position : sig ... endSource position tracking.
module Span : sig ... endSource span (range of positions).
module Chomping : sig ... endBlock scalar chomping modes.
module Tag : sig ... endYAML type tags.
module Value : sig ... endJSON-compatible value type and operations.
module Scalar : sig ... endYAML scalar with metadata.
module Sequence : sig ... endYAML sequence with metadata.
module Mapping : sig ... endYAML mapping with metadata.
module Yaml : sig ... endFull YAML value type.
module Document : sig ... endYAML document with directives.
module Token : sig ... endLexical tokens.
module Scanner : sig ... endLexical scanner.
module Event : sig ... endParser events.
module Parser : sig ... endEvent-based parser.
module Loader : sig ... endDocument loader.
module Emitter : sig ... endEvent-based emitter.
module Input : sig ... endInput stream utilities.
module Serialize : sig ... endBuffer serialization utilities.