Module Imandrakit_ser_pack.Ser

Serialization

type state

State used for serialization.

It contains an in-progress heap, and a hash table for hashconsing.

type 'a t = state -> 'a -> value

Serializer for type 'a

val create : unit -> state

New state.

type ptr = value

An integer + tag for value

val unit : value

Build a value null

val int64 : int64 -> value

Build a value integer

val int : int -> value

Build a value integer

val bool : bool -> value

Build a value bool

val float : float -> value

Build a value float

val string : string -> value

Build a value text string (UTF8)

val bytes : string -> value

Build a value blob (raw bytes)

val list : value list -> value

Build a value list

val map : (string * value) list -> value

Build a value map

val list_of : 'a t -> 'a list t

list_of ser encodes a list of values using ser for each

val map_of : 'b t -> (string * 'b) list t

Build a map by serializing the given association list

val add_entry : state -> value -> ptr

add_entry st c turns c into a heap entry and returns a pointer to it. The pointer is a small value value (a tagged integer).

val add_entry_hashcons : state -> value -> ptr

add_entry_hashcons st c turns c into a heap entry. c is first compared to existing hashconsed entries (at a runtime cost) to see if we can reuse them instead of inserting a new value.

val add_string : ?hashcons:bool -> state -> string -> value

Same as add_entry state (`Text s), except that large strings will be hashconsed unconditionally.

val add_bytes : ?hashcons:bool -> state -> string -> value

Same as add_string

val delay : (unit -> 'a t) -> 'a t

delay f is like f(), but f is only called when needed.

val fix : ('a t -> 'a t) -> 'a t

fix f is a recursive serializer. f receives a serializer for recursive cases and must use it to implement the serialization for the current value.

val result : 'a t -> 'b t -> ('a, 'b) result t
type 'a cache_key
val create_cache_key : (module Stdlib.Hashtbl.HashedType with type t = 'a) -> 'a cache_key

Create a new (generative) cache key for a hashable + comparable type.

NOTE this should be called only at module toplevel, as a constant, not dynamically inside a function: let key = value_pack.Ser.create_cache_key (module …);;. Indeed, this is generative, so creating multiple keys for a type will result in sub-par or inexistant caching.

val with_cache : 'a cache_key -> 'a t -> 'a t

with_cache key enc is the same encoder as enc, but with caching. When encoding a value x:'a, the cache key is used to detect if x was already encoded to some entry, and uses a pointer to this entry instead of re-serializing x.

val finalize_value : state -> key:value -> value

Turn the state into a pack with given key as entrypoint.

val finalize_cbor_string : state -> key:value -> string

Same as finalize_value but also turns the resulting packed value into a string.