package cconv

  1. Overview
  2. Docs
On This Page
  1. Composite Types
Legend:
Library
Module
Module type
Parameter
Class
Class type
type 'a output = {
  1. unit : 'a;
  2. bool : bool -> 'a;
  3. float : float -> 'a;
  4. char : char -> 'a;
  5. int : int -> 'a;
  6. nativeint : nativeint -> 'a;
  7. int32 : int32 -> 'a;
  8. int64 : int64 -> 'a;
  9. string : string -> 'a;
  10. list : 'a list -> 'a;
  11. option : 'a option -> 'a;
  12. record : (string * 'a) list -> 'a;
  13. tuple : 'a list -> 'a;
  14. sum : string -> 'a list -> 'a;
}
val string_target : string output

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

type -'src encoder = {
  1. 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 option : 'a encoder -> 'a option 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 = {
  1. 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 = {
  1. 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 = {
  1. 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]
  });;
OCaml

Innovation. Community. Security.