Data.Binary

About

Data.Binary is a library for high performance binary serialisation
of Haskell data. It uses the
ByteString library to achieve efficient, lazy reading and
writing of structures in binary format.

Chris Eidhof writes on his use of Data.Binary implementing a
full-text search engine:

"The communication with Sphinx is done using a quite low-level binary
protocol, but Data.Binary saved the day: it made it very easy for us
to decode all the binary things. Especially the use of the Get and
Put monads are a big improvement over the manual reading and keeping
track of positions, as is done in the PHP/Python clients."

Example

For example, to serialise an interpreter's abstract syntax tree to
binary format:

importData.BinaryimportControl.MonadimportCodec.Compression.GZip-- A Haskell AST structuredataExp=IntEInt|OpEStringExpExpderivingEq-- An instance of Binary to encode and decode an Exp in binaryinstanceBinaryExpwhereput(IntEi)=put(0::Word8)>>putiput(OpEse1e2)=put(1::Word8)>>puts>>pute1>>pute2get=dotag<-getWord8casetagof0->liftMIntEget1->liftM3OpEgetgetget-- A test expressione=OpE"*"(IntE7)(OpE"/"(IntE4)(IntE2))-- Serialise and compress with gzip, then decompress and deserialisemain=dolett=compress(encodee)printtlete'=decode(decompresst)print(e==e')