Sexplib's syntax extension makes serialization and deserialization of arbitrary user-defined data structures easy in OCaml. It is generally done by adding a with sexp
annotation to the end of a type definition:
type a = A of int | B of float with sexp
This does not seem to generalize directly to functor-based types, nor is it clear how the Sexplib standard type converters can capture even the standard functors.
So far I have worked around this by flattening a specific Map type instance (e.g. int Map.Make(String).t
) to a list before serialization, and vice versa, but surely this hasn't been completely overlooked by the generally ambitious authors of Sexplib/Jane Street Core. I also notice that older versions of Batteries mix in custom sexp serialization to their major modules like [Bat]Map, but that this has been removed for some time.
How are Maps or other complex functor types commonly used with Sexplib serialization?
One way is to define a new functor that takes in the additional information needed to serialize the data. Here's a complete implementation I have used in the past with Batteries. Note I also preferred the exceptionless and labeled version of Map, so I've opened those, but you could of course remove that.
If I recall correctly, Batteries removed such features to reduce dependencies on additional libraries. Your other option is to use Core, which has these functions out-of-the-box.