Saswat Padhi
Saswat Padhi

Reputation: 6532

OCaml serialization with types

Is there any OCaml library that would serialize my data along with type information? For example: serialize [1;2;3] may give me <int list: (1,2,3)> or similar?

I want to store the type information with the data. Is it possible? I tried looking at Sexplib, but I cannot figure out if they support this.

Thanks!

Upvotes: 0

Views: 710

Answers (2)

ivg
ivg

Reputation: 35210

If all that you need is to store typenames, then you can use typerep library, to retrieve them, and then adapt sexp converters to handle this information.

Another option is to store data actually as a valid OCaml string, and to load it using compiler libs, i.e. actually parse and evaluate. In that case you can store types or whatever you want. You may find odn library useful for dumping data as OCaml.

Upvotes: 2

camlspotter
camlspotter

Reputation: 9030

I think sexplib or bin_prot are the current best bet. Sexplib does not attach type names to the data and I guess bin_prot does not either, but type safety is assured at the reader side.

If you really want "type information" attached to the serialized data, you can write your own serializer using ppx_deriving. It is rather new but much easier than writing CamlP4 based serializer like sexplib, bin_prot and meta_conv.

Upvotes: 2

Related Questions