I can't speak to OCaml, but I'd say that the main difficulty in Haskell is that deserialization requires knowing the type in advance--there's no universal way to mechanically deserialize from a format, figure out what the resulting value is, and go from there, as is possible in languages with unsound or dynamic type systems.
Setting aside the type issue, there are various approaches to serializing data in Haskell:
The built-in type classes Read
/Show
(de)serialize algebraic data types and most built-in types as strings. Well-behaved instances should generally be such that read . show
is equivalent to id
, and that the result of show
can be parsed as Haskell source code constructing the serialized value.
Various serialization packages can be found on Hackage; typically these require that the type to be serialized be an instance of some type class, with the package providing instances for most built-in types. Sometimes they merely require an automatically derivable instance of the type-reifying, reflective metaprogramming Data
class (the charming fully qualified name for which is Data.Data.Data
), or provide Template Haskell code to auto-generate instances.
For truly unusual serialization formats--or to create your own package like the previously mentioned ones--one can reach for the biggest hammer available, sort of a "big brother" to Read
and Show
: parsing and pretty-printing. Numerous packages are available for both, and while it may sound intimidating at first, parsing and pretty-printing are in fact amazingly painless in Haskell.
A glance at Hackage indicates that serialization packages already exist for various formats, including binary data, JSON, YAML, and XML, though I've not used any of them so I can't personally attest to how well they work. Here's a non-exhaustive list to get you started:
- binary: Performance-oriented serialization to lazy
ByteString
s
- cereal: Similar to binary, but a slightly different interface and uses strict
ByteString
s
- genericserialize: Serialization via built-in metaprogramming, output format is extensible, includes R5RS sexp output.
- json: Lightweight serialization of JSON data
- RJson: Serialization to JSON via built-in metaprogramming
- hexpat-pickle: Combinators for serialization to XML, using the "hexpat" package
- regular-xmlpickler: Serialization to XML of recursive data structures using the "regular" package
The only other problem is that, inevitably, not all types will be serializable--if nothing else, I suspect you're going to have a hard time serializing polymorphic types, existential types, and functions.