package cconv

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

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 = {
  1. emit : 'a. ('src, 'a) inner_decoder -> 'src -> 'a;
}

Decode a value of type 'src

and ('src, 'into) inner_decoder = {
  1. accept_unit : 'src source -> unit -> 'into;
  2. accept_bool : 'src source -> bool -> 'into;
  3. accept_float : 'src source -> float -> 'into;
  4. accept_int : 'src source -> int -> 'into;
  5. accept_string : 'src source -> string -> 'into;
  6. accept_list : 'src source -> 'src list -> 'into;
  7. accept_option : 'src source -> 'src option -> 'into;
  8. accept_record : 'src source -> (string * 'src) list -> 'into;
  9. accept_tuple : 'src source -> 'src list -> 'into;
  10. accept_sum : 'src source -> string -> 'src list -> 'into;
}

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

type 'into decoder = {
  1. 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 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 = {
  1. 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 = {
  1. 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 = {
  1. 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}
  );
};;
OCaml

Innovation. Community. Security.