cconv

Combinators for Type Conversion in OCaml, and ppx_deriving plugin.
Legend:
Library
Module
Module type
Parameter
Class
Class type
Library cconv
Module CConv . Decode

Decode

A 'a decodee describes a way to traverse a value of some type representing a serialization format such as JSON or B-encode

type 'src source = {
emit : 'a. ( 'src, 'a ) inner_decoder -> 'src -> 'a;
}

Decode a value of type 'src

and ('src, 'into) inner_decoder = {
accept_unit : 'src source -> unit -> 'into;
accept_bool : 'src source -> bool -> 'into;
accept_float : 'src source -> float -> 'into;
accept_int : 'src source -> int -> 'into;
accept_int32 : 'src source -> int32 -> 'into;
accept_int64 : 'src source -> int64 -> 'into;
accept_nativeint : 'src source -> nativeint -> 'into;
accept_char : 'src source -> char -> 'into;
accept_string : 'src source -> string -> 'into;
accept_list : 'src source -> 'src list -> 'into;
accept_option : 'src source -> 'src option -> 'into;
accept_record : 'src source -> (string * 'src) list -> 'into;
accept_tuple : 'src source -> 'src list -> 'into;
accept_sum : 'src source -> string -> 'src list -> 'into;
}

Decode a value of type 'src into a type 'into.

type 'into decoder = {
dec : 'src. ( 'src, 'into ) inner_decoder;
}
val apply : 'src source -> 'into decoder -> 'src -> 'into

Apply a decoder to a source

Decoder Combinators
val int : int decoder
val char : char decoder
val int32 : int32 decoder
val int64 : int64 decoder
val nativeint : nativeint decoder
val float : float decoder
val bool : bool decoder
val unit : unit decoder
val string : string decoder
val list : 'a decoder -> 'a list decoder
val array : 'a decoder -> 'a array decoder
val sequence : 'a decoder -> 'a sequence decoder
val map : ( 'a -> 'b ) -> 'a decoder -> 'b decoder

Map the decoded value

val arg0 : 'src list -> unit

Only accepts an empty list/tuple

val arg1 : 'a decoder -> 'src source -> 'src list -> 'a

Only accepts a 1-element list/tuple

val arg2 : 'a decoder -> 'b decoder -> 'src source -> 'src list -> 'a * 'b

Only accepts a 2-elements list/tuple

val arg3 : 'a decoder -> 'b decoder -> 'c decoder -> 'src source -> 'src list -> 'a * 'b * 'c

Only accepts a 3-elements list/tuple

val option : 'a decoder -> 'a option decoder

Helper for options

val pair : 'a decoder -> 'b decoder -> ('a * 'b) decoder
val triple : 'a decoder -> 'b decoder -> 'c decoder -> ('a * 'b * 'c) decoder
val record_get : string -> 'into decoder -> 'src source -> (string * 'src) list -> 'into

record_get name dec l is a helper for decoding records. It is given a list of fields l, and searches name through it. If name is found with a value v, dec.accept v is called. Otherwise an error is raised

val record_get_opt : string -> 'into decoder -> 'src source -> (string * 'src) list -> 'into option
type 'into record_decoder = {
record_accept : 'src. 'src source -> (string * 'src) list -> 'into;
}
val record : 'into record_decoder -> 'into decoder

Decoder for records. It will adapt itself to association tuples and lists.

val record_fix : ( 'into decoder -> 'into record_decoder ) -> 'into decoder
type 'into sum_decoder = {
sum_accept : 'src. 'src source -> string -> 'src list -> 'into;
}
val sum : 'into sum_decoder -> 'into decoder

Decoder for sums. It will adapt itself to strings, lists and tuples

val sum_fix : ( 'into decoder -> 'into sum_decoder ) -> 'into decoder
type 'into tuple_decoder = {
tuple_accept : 'src. 'src source -> 'src list -> 'into;
}
val tuple : 'into tuple_decoder -> 'into decoder

Tuple decoder

Examples:

type mytuple = int * string * float list ;;

let decode_mytuple = tuple {
  tuple_accept=fun src l -> arg3 int int (list string) src l);
};;

(* OR, because triples are really easy: *)
let decode_mytuple = triple int int (list string);;

type point = { x:int;  y:int; color:string };;

let decode_point = record ~expected:"point" {
  record_accept=(fun src l ->
    let x = record_get "x" int src l in
    let y = record_get "y" int src l in
    let color = record_get "color" string src l in
    {x;y;color}
  );
};;