Page
Library
Module
Module type
Parameter
Class
Class type
Source
Kdl
SourceOCaml implementation of the KDL Document Language v2.
KDL is a data serialization format in the same vein as JSON, YAML, TOML, XML, SDLang, edn, and so on.
Small examples can be found in the README of the ocaml-kdl repository.
A KDL value: String, Number, Bool, Null.
A KDL value with an optional type annotation. For example, (Some "u16", `Int 3201)
is an annot_value
.
A KDL property is a key-value pair.
type node = {
name : string;
annot : string option;
args : annot_value list;
props : prop list;
children : node list;
}
A KDL node. Nodes consist of the node name, optional type annotation, ordered arguments (values), unordered properties (a key-value map), and children nodes.
props
is an association list; the order of the list is unspecified.
The error location in bytes.
Parse KDL from a string. The string should be UTF8-encoded. ?fname
is an optional filename that is used in locations.
Parse KDL from a channel.
Raising version of of_channel
.
val of_chunk_gen :
?fname:string ->
(bytes -> offset:int -> len:int -> int) ->
(t, error) result
of_chunk_gen ?fname f
is an advanced function that parses KDL from byte chunks written by f
. The function f
is similar to Input; f
must write no more than ~len
bytes at offset ~offset
, returning the amount of written bytes. A return value of 0 means end of input. See also Lexing.from_function
.
Raising version of of_chunk_gen
.
val node :
?annot:string ->
string ->
?args:annot_value list ->
?props:prop list ->
node list ->
node
node ?annot name ?args ?props children
constructs a node
.
arg ?annot value
constructs an argument (that is, a value with an optional type annotation).
prop ?annot name value
constructs a property.
indent
is the number of spaces used per indentation level. By default, indent
is set to 2
.
Pretty-print a value.
Pretty-print an annotated value.
Pretty-print a property.
Pretty-print a node using !indent
as the indentation width for children nodes.
Same as pp_node
, but outputs the result in a single line.
Pretty-print a list of nodes or a KDL document using !indent
as the indentation width for children nodes.
Same as pp
, but outputs the result in a single line.
KDL defines reserved type annotations. Some of them are supported out of the box in ocaml-kdl. An annotated value can be "interpreted" using the interpret
function. If the type annotation is unknown, `Other
is returned.
typed_value
can be extended with custom type annotations, for example, the following way:
type typed_value = [ Kdl.typed_value
| `Date of (* ... *) ]
let interpret : _ -> [> typed_value ] = function
| Some "date", value -> `Date ((* ... parse ISO 8601 date ... *))
| x -> Kdl.interpret x
Interpret a type-annotated value.
Note: f32
is currently the same as f64
.