cconv

Combinators for Type Conversion in OCaml, and ppx_deriving plugin.
Legend:
Library
Module
Module type
Parameter
Class
Class type
Library cconv
Module CConv . Encode
type 'a output = {
unit : 'a;
bool : bool -> 'a;
float : float -> 'a;
char : char -> 'a;
int : int -> 'a;
nativeint : nativeint -> 'a;
int32 : int32 -> 'a;
int64 : int64 -> 'a;
string : string -> 'a;
list : 'a list -> 'a;
option : 'a option -> 'a;
record : (string * 'a) list -> 'a;
tuple : 'a list -> 'a;
sum : string -> 'a list -> 'a;
}
val string_target : string output

Print values. Caution, inefficient! Should be used for debugging only

type -'src encoder = {
emit : 'into. 'into output -> 'src -> 'into;
}

A way to encode values of type 'src into any serialization format

val unit : unit encoder
val bool : bool encoder
val float : float encoder
val char : char encoder
val int : int encoder
val nativeint : nativeint encoder
val int32 : int32 encoder
val int64 : int64 encoder
val string : string encoder
val list : 'a encoder -> 'a list encoder
val map : ( 'a -> 'b ) -> 'b encoder -> 'a encoder
val array : 'a encoder -> 'a array encoder
val sequence : 'a encoder -> 'a sequence encoder
Composite Types
val apply : 'into output -> 'src encoder -> 'src -> 'into

Helper to apply an encoder to a value

type 'r record_encoder = {
record_emit : 'into. 'into output -> 'r -> (string * 'into) list;
}
val record : 'r record_encoder -> 'r encoder

Encode a record, using the polymorphic record record_encoder to generate an association list

val record_fix : ( 'r encoder -> 'r record_encoder ) -> 'r encoder

Fixpoint on record definition

Example:

type point = {x:int; y:int; c:string};;
let enc_point = record
  {record_emit=fun into {x;y;c} ->
     [ "x", into.int x
     ; "y", into.int y
     ; "c", into.string c
     ]
 } ;;
type 't tuple_encoder = {
tuple_emit : 'into. 'into output -> 't -> 'into list;
}
val tuple : 'a tuple_encoder -> 'a encoder

General encoding of tuples (returns a list of values)

val pair : 'a encoder -> 'b encoder -> ('a * 'b) encoder
val triple : 'a encoder -> 'b encoder -> 'c encoder -> ('a * 'b * 'c) encoder
val quad : 'a encoder -> 'b encoder -> 'c encoder -> 'd encoder -> ('a * 'b * 'c * 'd) encoder
type 's sum_encoder = {
sum_emit : 'into. 'into output -> 's -> string * 'into list;
}
val sum : 'a sum_encoder -> 'a encoder
val sum0 : ( 'a -> string ) -> 'a encoder

Constant sums, only put the name

val sum_fix : ( 'a encoder -> 'a sum_encoder ) -> 'a encoder

Fixpoint on sum types

Example:

type tree = Empty | Leaf of int | Node of tree * tree;;
let encode_tree = sum_fix
  (fun self -> {sum_emit=fun into x -> match x with
    | Empty -> "empty", []
    | Leaf i -> "leaf", [int.emit into i]
    | Node (l,r) -> "node", [self.emit into l; self.emit into r]
  });;
val option : 'a encoder -> 'a option encoder