package decoders
Module Decode.Make
Derive decoders for a Decodeable.value.
Parameters
module M : DecodeableSignature
type value = M.valueThe type of values to be decoded (e.g. JSON or Yaml).
type error = value exposed_errorval pp_error : Format.formatter -> error -> unittype 'a decoder = (M.value, 'a) exposed_decoderThe type of decoders.
Use the functions below to construct decoders for your data types.
To run a decoder, pass it to decode_value.
Primitives
val string : string decoderDecode a string.
val int : int decoderDecode an int.
val float : float decoderDecode a float.
val bool : bool decoderDecode a bool.
Data structures
Decode a collection into an OCaml list, skipping elements for which the decoder returns None.
Object primitives
Decode an object, requiring exactly one field.
Inconsistent structure
Mapping
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.
Working with object keys
val keys : string list decoderDecode 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.
Fancy decoding
val succeed : 'a -> 'a decoderA decoder that always succeeds with the argument, ignoring the input.
val fail : string -> 'a decoderA 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 ... endRunning decoders
module Pipeline : sig ... end