Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do automatic data serialization of data objects?

One of the huge benefits in languages that have some sort of reflection/introspecition is that objects can be automatically constructed from a variety of sources.

For example, in Java I can use the same objects for persisting to a db (with Hibernate), serializing to XML (with JAXB), and serializing to JSON (json-lib). You can do the same in Ruby and Python also usually following some simple rules for properties or annotations for Java.

Thus I don't need lots "Domain Transfer Objects". I can concentrate on the domain I am working in.

It seems in very strict FP like Haskell and Ocaml this is not possible. Particularly Haskell. The only thing I have seen is doing some sort of preprocessing or meta-programming (ocaml). Is it just accepted that you have to do all the transformations from the bottom upwards?

In other words you have to do lots of boring work to turn a data type in haskell into a JSON/XML/DB Row object and back again into a data object.

like image 263
Adam Gent Avatar asked Apr 22 '10 11:04

Adam Gent


1 Answers

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 ByteStrings
  • cereal: Similar to binary, but a slightly different interface and uses strict ByteStrings
  • 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.

like image 124
C. A. McCann Avatar answered Sep 28 '22 16:09

C. A. McCann