Library
Module
Module type
Parameter
Class
Class type
User-facing Decoder interface.
type error = value exposed_error
val pp_error : Format.formatter -> error -> unit
The type of decoders.
Use the functions below to construct decoders for your data types.
To run a decoder, pass it to decode_value
.
val string : string decoder
Decode a string
.
val int : int decoder
Decode an int
.
val float : float decoder
Decode a float
.
val bool : bool decoder
Decode a bool
.
Decode a collection into an OCaml list, skipping elements for which the decoder returns None.
Decode an object, requiring exactly one field.
Map functions are useful for decoding complex objects.
For example, given an object with structure
{
"name": "Joe"
"age": 42
}
we want to decode it to our OCaml type
type person =
{ name : string
; age : int
}
We define a helper function to construct values of this type:
let as_person name age =
{ name = name
; age = age
}
The decoder looks like this:
let person_decoder : person decoder =
map2 as_person
(field "name" string)
(field "age" int)
Try two decoders and then combine the result. We can use this to decode objects with many fields.
val keys : string list decoder
Decode all of the keys of an object to a list of strings.
Decode an object into a list of key-value pairs.
Decode an object into a list of values, where the value decoder depends on the key.
keys'
is for when your keys might not be strings - probably only likely for Yaml.
Decode an object into a String.Map.t
.
val succeed : 'a -> 'a decoder
A decoder that always succeeds with the argument, ignoring the input.
val fail : string -> 'a decoder
A decoder that always fails with the given message, ignoring the input.
Create decoders that depend on previous results.
Recursive decoders.
let my_decoder = fix (fun my_decoder -> ...)
allows you to define my_decoder
in terms of itself.
module Infix : sig ... end
module Pipeline : sig ... end