Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize/deserialize objects sent over the network in Haskell?

I see that there are many ways to serialize/deserialize Haskell objects:

  • Data.Serialize -> encode, decode functions
  • Data.Binary http://code.haskell.org/binary/
  • MsgPack, JSON, BSON, etc

In my application, I want to setup a simple TCP client-server, where client may send serialized Haskell record objects. How does one decide between these serialization alternatives?

Additionally, when objects serialized into strings are sent over the network using Network.Socket, strings are returned. Is there a slightly higher level library, that works at the level of whole TCP messages? In other words, is there a way to avoid writing parsing code on the receive end that:

  • collects results of a sequence of recv() calls,
  • detect that a whole object has been received, and
  • then parse it into a haskell type?

In my application, the objects are not expected to be too large (maybe about ~1MB max).

like image 982
donatello Avatar asked Oct 21 '14 06:10

donatello


1 Answers

As for the second part of your question, two things are required:

  1. An incremental parser that doesn't need to have the whole document in memory to start parsing, and which can be fed with the partial chunks of data arriving from the wire. Also, when the parsing succeeds it must return any "leftover data" along with the parsed value.

  2. A source of data with "pushback capabilities", that allows you to "unread" any leftovers so that they are available to the next parsing attempt.

The most popular library providing (1) is attoparsec. As for (2), all the three main streaming libraries (conduit, io-streams, and pipes) offer some kind of pushback functionality (the latter using the auxiliary pipes-parse package). All three libraries can integrate with attoparsec parsers as well (see here, here and here).

(Another option, of course, is to prepend each message with its lenght are read only the exact number of bytes.)

like image 104
danidiaz Avatar answered Oct 11 '22 13:10

danidiaz