7

The Data.Binary documentation shows writing an instance by hand. Is there a way around this? I saw here there is another library, SerTH, which has a (Template Haskell based) deriving mechanism, but the link to it seems broken. Also, if you know other libraries, good performance is critical for us.

Thank you in advance!

gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
  • GHC 7.2 supports this natively using default deriving... I wrote the code for it awhile back and it was pretty simple – alternative Jan 03 '12 at 17:04

3 Answers3

7

See http://hackage.haskell.org/packages/archive/binary/0.7.1.0/doc/html/Data-Binary.html#g:3

 {-# LANGUAGE DeriveGeneric #-}

 import Data.Binary
 import GHC.Generics (Generic)

 data Foo = Foo
          deriving (Generic)

 -- GHC will automatically fill out the instance
 instance Binary Foo
jmr
  • 71
  • 1
  • 2
5

Neil Mitchells Derive package has a template haskell macro for deriving binary instances.

aleator
  • 4,436
  • 20
  • 31
5

Since you asked about other libraries:

The cereal data serialisation library has cereal-derive, which works with the new Generics support in GHC 7.2. This has a compile-time speed advantage over Template Haskell (I tend to avoid TH these days just because it makes compilation even slower) and a run-time speed advantage over datatype-generic methods like SYB and Uniplate.

cereal is very similar to binary, but uses strict ByteStrings; binary hasn't been updated since 2009 and cereal has niceties such as IEEE-754 float format support, so I can't see any reason not to use it over binary if you want deriving.

ehird
  • 40,602
  • 3
  • 180
  • 182