Efficient, pure binary serialisation using ByteStrings in Haskell.
Haskell C
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
benchmarks Fix CI Jun 7, 2018
docker Add docker-compose.yml to run tests on multiple ghc versions. Aug 8, 2018
docs/hcar Minor updates to HCAR May 9, 2008
src/Data Merge pull request #146 from Daniel-Diaz/master Jun 7, 2018
tests Hide 'total' from Test.QuickCheck. Fixes shadowing warning. Aug 1, 2018
tools/derive Don't use tyConString Jan 10, 2017
.dockerignore Add initial Dockerfile running binary's tests. Aug 6, 2018
.gitignore Update .gitignore and .hgignore with cabal sandbox files May 11, 2014
.hgignore Update .gitignore and .hgignore with cabal sandbox files May 11, 2014
LICENSE fix license, the 'regents' weren't involed.. Jan 20, 2007
README.md Add badges to README Sep 21, 2016
Setup.lhs Add #! to Setup.lhs Jan 12, 2007
binary.cabal Add two more other-modules. Aug 6, 2018
changelog.md Bump to and add changelog. Aug 1, 2018
generics-bench.cache.gz Fix CI Jun 7, 2018


binary package

Hackage version Stackage version Build Status

Efficient, pure binary serialisation using lazy ByteStrings.

The binary package provides Data.Binary, containing the Binary class, and associated methods, for serialising values to and from lazy ByteStrings. A key feature of binary is that the interface is both pure, and efficient. The binary package is portable to GHC and Hugs.

Installing binary from Hackage

binary is part of The Glasgow Haskell Compiler (GHC) and therefore if you have either GHC or The Haskell Platform installed, you already have binary.

More recent versions of binary than you might have installed may be available. You can use cabal-install to install a later version from Hackage.

$ cabal update
$ cabal install binary

Building binary

binary comes with both a test suite and a set of benchmarks. While developing, you probably want to enable both. Here's how to get the latest version of the repository, configure and build.

$ git clone git@github.com:kolmodin/binary.git
$ cd binary
$ cabal update
$ cabal configure --enable-tests --enable-benchmarks
$ cabal build

Run the test suite.

$ cabal test

Using binary


import Data.Binary

and then write an instance of Binary for the type you wish to serialise. An example doing exactly this can be found in the Data.Binary module. You can also use the Data.Binary.Builder module to efficiently build lazy bytestrings using the Builder monoid. Or, alternatively, the Data.Binary.Get and Data.Binary.Put to serialize/deserialize using the Get and Put monads.

More information in the haddock documentation.

Deriving binary instances using GHC's Generic

Beginning with GHC 7.2, it is possible to use binary serialization without writing any instance boilerplate code.

{-# LANGUAGE DeriveGeneric #-}

import Data.Binary
import GHC.Generics (Generic)

data Foo = Foo deriving (Generic)

-- GHC will automatically fill out the instance
instance Binary Foo


  • Lennart Kolmodin
  • Duncan Coutts
  • Don Stewart
  • Spencer Janssen
  • David Himmelstrup
  • Björn Bringert
  • Ross Paterson
  • Einar Karttunen
  • John Meacham
  • Ulf Norell
  • Tomasz Zielonka
  • Stefan Karrmann
  • Bryan O'Sullivan
  • Bas van Dijk
  • Florian Weimer

For a full list of contributors, see here.