binary-0.8.5.1: Binary serialisation for Haskell values using lazy ByteStrings

Copyright | Lennart Kolmodin
License | BSD3-style (see LICENSE)
Maintainer | Lennart Kolmodin <kolmodin@gmail.com>
Stability | unstable
Portability | portable to Hugs and GHC. Requires the FFI and some flexible instances.
Safe Haskell | Trustworthy
Language | Haskell98

Data.Binary

Contents

- The Binary class
  - Example
- Generic support
- The Get and Put monads
- Useful helpers for writing instances
- Binary serialisation
- IO functions for serialisation

Description

Binary serialisation of Haskell values to and from lazy `ByteString`s. The Binary library provides methods for encoding Haskell values as streams of bytes directly in memory. The resulting `ByteString` can then be written to disk, sent over the network, or further processed (for example, compressed with gzip).

The `binary` package is notable in that it provides both pure, and high performance serialisation.

Values encoded using the `Binary` class are always encoded in network order (big endian) form, and encoded data should be portable across machine endianness, word size, or compiler version. For example, data encoded using the `Binary` class could be written on any machine, and read back on any another.

If the specifics of the data format is not important to you, for example, you are more interested in serializing and deserializing values than in which format will be used, it is possible to derive `Binary` instances using the generic support. See `GBinaryGet` and `GBinaryPut`.

If you have specific requirements about the encoding format, you can use the encoding and decoding primitives directly, see the modules Data.Binary.Get and Data.Binary.Put.

Synopsis

- class Binary t where
- class GBinaryGet f where
- class GBinaryPut f where
- data Get a
- type Put = PutM ()
- putWord8 :: Word8 -> Put
- getWord8 :: Get Word8
- encode :: Binary a => a -> ByteString
- decode :: Binary a => ByteString -> a
- decodeOrFail :: Binary a => ByteString -> Either (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
- encodeFile :: Binary a => FilePath -> a -> IO ()
- decodeFile :: Binary a => FilePath -> IO a
- decodeFileOrFail :: Binary a => FilePath -> IO (Either (ByteOffset, String) a)
- module Data.Word

# The Binary class

class Binary t where #

The `Binary` class provides `put` and `get`, methods to encode and decode a Haskell value to a lazy `ByteString`. It mirrors the `Read` and `Show` classes for textual representation of Haskell types, and is suitable for serialising Haskell values to disk, over the network.

For decoding and generating simple external binary formats (e.g. C structures), Binary may be used, but in general is not suitable for complex protocols. Instead use the `Put` and `Get` primitives directly.

Instances of Binary should satisfy the following property:

    decode . encode == id

That is, the `get` and `put` methods should be the inverse of each other. A range of instances are provided for basic Haskell types.

Methods

put :: t -> Put #

Encode a value in the Put monad.

get :: Get t #

Decode a value in the Get monad

putList :: [t] -> Put #

Encode a list of values in the Put monad. The default implementation may be overridden to be more efficient but must still have the same encoding format.

put :: (Generic t, GBinaryPut (Rep t)) => t -> Put #

Encode a value in the Put monad.

get :: (Generic t, GBinaryGet (Rep t)) => Get t #

Decode a value in the Get monad

Instances

Binary Bool # |
Methods put :: Bool -> Put # get :: Get Bool # putList :: [Bool] -> Put #
Binary Char # |
Methods put :: Char -> Put # get :: Get Char # putList :: [Char] -> Put #
Binary Double # |
Methods put :: Double -> Put # get :: Get Double # putList :: [Double] -> Put #
Binary Float # |
Methods put :: Float -> Put # get :: Get Float # putList :: [Float] -> Put #
Binary Int # |
Methods put :: Int -> Put # get :: Get Int # putList :: [Int] -> Put #
Binary Int8 # |
Methods put :: Int8 -> Put # get :: Get Int8 # putList :: [Int8] -> Put #
Binary Int16 # |
Methods put :: Int16 -> Put # get :: Get Int16 # putList :: [Int16] -> Put #
Binary Int32 # |
Methods put :: Int32 -> Put # get :: Get Int32 # putList :: [Int32] -> Put #
Binary Int64 # |
Methods put :: Int64 -> Put # get :: Get Int64 # putList :: [Int64] -> Put #
Binary Integer # |
Methods put :: Integer -> Put # get :: Get Integer # putList :: [Integer] -> Put #
Binary Natural # | Since: 0.7.3.0
Methods put :: Natural -> Put # get :: Get Natural # putList :: [Natural] -> Put #
Binary Ordering # |
Methods put :: Ordering -> Put # get :: Get Ordering # putList :: [Ordering] -> Put #
Binary Word # |
Methods put :: Word -> Put # get :: Get Word # putList :: [Word] -> Put #
Binary Word8 # |
Methods put :: Word8 -> Put # get :: Get Word8 # putList :: [Word8] -> Put #
Binary Word16 # |
Methods put :: Word16 -> Put # get :: Get Word16 # putList :: [Word16] -> Put #
Binary Word32 # |
Methods put :: Word32 -> Put # get :: Get Word32 # putList :: [Word32] -> Put #
Binary Word64 # |
Methods put :: Word64 -> Put # get :: Get Word64 # putList :: [Word64] -> Put #
Binary RuntimeRep # | @since 0.8.5.0. See
Methods put :: RuntimeRep -> Put # get :: Get RuntimeRep # putList :: [RuntimeRep] -> Put #
Binary VecCount # | @since 0.8.5.0. See
Methods put :: VecCount -> Put # get :: Get VecCount # putList :: [VecCount] -> Put #
Binary VecElem # | @since 0.8.5.0. See
Methods put :: VecElem -> Put # get :: Get VecElem # putList :: [VecElem] -> Put #
Binary SomeTypeRep # |
Methods put :: SomeTypeRep -> Put # get :: Get SomeTypeRep # putList :: [SomeTypeRep] -> Put #
Binary () # |
Methods put :: () -> Put # get :: Get () # putList :: [()] -> Put #
Binary TyCon # | @since 0.8.5.0. See
Methods put :: TyCon -> Put # get :: Get TyCon # putList :: [TyCon] -> Put #
Binary KindRep # | @since 0.8.5.0. See
Methods put :: KindRep -> Put # get :: Get KindRep # putList :: [KindRep] -> Put #
Binary TypeLitSort # | @since 0.8.5.0. See
Methods put :: TypeLitSort -> Put # get :: Get TypeLitSort # putList :: [TypeLitSort] -> Put #
Binary Void # | Since: 0.8.0.0
Methods put :: Void -> Put # get :: Get Void # putList :: [Void] -> Put #
Binary Version # | Since: 0.8.0.0
Methods put :: Version -> Put # get :: Get Version # putList :: [Version] -> Put #
Binary All # | Since: 0.8.4.0
Methods put :: All -> Put # get :: Get All # putList :: [All] -> Put #
Binary Any # | Since: 0.8.4.0
Methods put :: Any -> Put # get :: Get Any # putList :: [Any] -> Put #
Binary Fingerprint # | Since: 0.7.6.0
Methods put :: Fingerprint -> Put # get :: Get Fingerprint # putList :: [Fingerprint] -> Put #
Binary ByteString # |
Methods put :: ByteString -> Put # get :: Get ByteString # putList :: [ByteString] -> Put #
Binary ShortByteString # |
Methods put :: ShortByteString -> Put # get :: Get ShortByteString # putList :: [ShortByteString] -> Put #
Binary ByteString # |
Methods put :: ByteString -> Put # get :: Get ByteString # putList :: [ByteString] -> Put #
Binary IntSet # |
Methods put :: IntSet -> Put # get :: Get IntSet # putList :: [IntSet] -> Put #
Binary a => Binary [a] # |
Methods put :: [a] -> Put # get :: Get [a] # putList :: [[a]] -> Put #
Binary a => Binary (Maybe a) # |
Methods put :: Maybe a -> Put # get :: Get (Maybe a) # putList :: [Maybe a] -> Put #
(Binary a, Integral a) => Binary (Ratio a) # |
Methods put :: Ratio a -> Put # get :: Get (Ratio a) # putList :: [Ratio a] -> Put #
Binary a => Binary (Complex a) # |
Methods put :: Complex a -> Put # get :: Get (Complex a) # putList :: [Complex a] -> Put #
Binary (Fixed a) # | Since: 0.8.0.0
Methods put :: Fixed a -> Put # get :: Get (Fixed a) # putList :: [Fixed a] -> Put #
Binary a => Binary (Min a) # | Since: 0.8.4.0
Methods put :: Min a -> Put # get :: Get (Min a) # putList :: [Min a] -> Put #
Binary a => Binary (Max a) # | Since: 0.8.4.0
Methods put :: Max a -> Put # get :: Get (Max a) # putList :: [Max a] -> Put #
Binary a => Binary (First a) # | Since: 0.8.4.0
Methods put :: First a -> Put # get :: Get (First a) # putList :: [First a] -> Put #
Binary a => Binary (Last a) # | Since: 0.8.4.0
Methods put :: Last a -> Put # get :: Get (Last a) # putList :: [Last a] -> Put #
Binary m => Binary (WrappedMonoid m) # | Since: 0.8.4.0
Methods put :: WrappedMonoid m -> Put # get :: Get (WrappedMonoid m) # putList :: [WrappedMonoid m] -> Put #
Binary a => Binary (Option a) # | Since: 0.8.4.0
Methods put :: Option a -> Put # get :: Get (Option a) # putList :: [Option a] -> Put #
Binary a => Binary (NonEmpty a) # | Since: 0.8.4.0
Methods put :: NonEmpty a -> Put # get :: Get (NonEmpty a) # putList :: [NonEmpty a] -> Put #
Binary a => Binary (Dual a) # | Since: 0.8.4.0
Methods put :: Dual a -> Put # get :: Get (Dual a) # putList :: [Dual a] -> Put #
Binary a => Binary (Sum a) # | Since: 0.8.4.0
Methods put :: Sum a -> Put # get :: Get (Sum a) # putList :: [Sum a] -> Put #
Binary a => Binary (Product a) # | Since: 0.8.4.0
Methods put :: Product a -> Put # get :: Get (Product a) # putList :: [Product a] -> Put #
Binary a => Binary (First a) # | Since: 0.8.4.0
Methods put :: First a -> Put # get :: Get (First a) # putList :: [First a] -> Put #
Binary a => Binary (Last a) # | Since: 0.8.4.0
Methods put :: Last a -> Put # get :: Get (Last a) # putList :: [Last a] -> Put #
Binary e => Binary (IntMap e) # |
Methods put :: IntMap e -> Put # get :: Get (IntMap e) # putList :: [IntMap e] -> Put #
Binary e => Binary (Tree e) # |
Methods put :: Tree e -> Put # get :: Get (Tree e) # putList :: [Tree e] -> Put #
Binary e => Binary (Seq e) # |
Methods put :: Seq e -> Put # get :: Get (Seq e) # putList :: [Seq e] -> Put #
Binary a => Binary (Set a) # |
Methods put :: Set a -> Put # get :: Get (Set a) # putList :: [Set a] -> Put #
(Binary a, Binary b) => Binary (Either a b) # |
Methods put :: Either a b -> Put # get :: Get (Either a b) # putList :: [Either a b] -> Put #
Typeable k a => Binary (TypeRep k a) # |
Methods put :: TypeRep k a -> Put # get :: Get (TypeRep k a) # putList :: [TypeRep k a] -> Put #
(Binary a, Binary b) => Binary (a, b) # |
Methods put :: (a, b) -> Put # get :: Get (a, b) # putList :: [(a, b)] -> Put #
(Binary i, Ix i, Binary e) => Binary (Array i e) # |
Methods put :: Array i e -> Put # get :: Get (Array i e) # putList :: [Array i e] -> Put #
(Binary a, Binary b) => Binary (Arg a b) # | Since: 0.8.4.0
Methods put :: Arg a b -> Put # get :: Get (Arg a b) # putList :: [Arg a b] -> Put #
(Binary k, Binary e) => Binary (Map k e) # |
Methods put :: Map k e -> Put # get :: Get (Map k e) # putList :: [Map k e] -> Put #
(Binary i, Ix i, Binary e, IArray UArray e) => Binary (UArray i e) # |
Methods put :: UArray i e -> Put # get :: Get (UArray i e) # putList :: [UArray i e] -> Put #
(Binary a, Binary b, Binary c) => Binary (a, b, c) # |
Methods put :: (a, b, c) -> Put # get :: Get (a, b, c) # putList :: [(a, b, c)] -> Put #
Binary (f a) => Binary (Alt k f a) # | Since: 0.8.4.0
Methods put :: Alt k f a -> Put # get :: Get (Alt k f a) # putList :: [Alt k f a] -> Put #
(Binary a, Binary b, Binary c, Binary d) => Binary (a, b, c, d) # |
Methods put :: (a, b, c, d) -> Put # get :: Get (a, b, c, d) # putList :: [(a, b, c, d)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e) => Binary (a, b, c, d, e) # |
Methods put :: (a, b, c, d, e) -> Put # get :: Get (a, b, c, d, e) # putList :: [(a, b, c, d, e)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f) => Binary (a, b, c, d, e, f) # |
Methods put :: (a, b, c, d, e, f) -> Put # get :: Get (a, b, c, d, e, f) # putList :: [(a, b, c, d, e, f)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g) => Binary (a, b, c, d, e, f, g) # |
Methods put :: (a, b, c, d, e, f, g) -> Put # get :: Get (a, b, c, d, e, f, g) # putList :: [(a, b, c, d, e, f, g)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g, Binary h) => Binary (a, b, c, d, e, f, g, h) # |
Methods put :: (a, b, c, d, e, f, g, h) -> Put # get :: Get (a, b, c, d, e, f, g, h) # putList :: [(a, b, c, d, e, f, g, h)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g, Binary h, Binary i) => Binary (a, b, c, d, e, f, g, h, i) # |
Methods put :: (a, b, c, d, e, f, g, h, i) -> Put # get :: Get (a, b, c, d, e, f, g, h, i) # putList :: [(a, b, c, d, e, f, g, h, i)] -> Put #
(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g, Binary h, Binary i, Binary j) => Binary (a, b, c, d, e, f, g, h, i, j) # |
Methods put :: (a, b, c, d, e, f, g, h, i, j) -> Put # get :: Get (a, b, c, d, e, f, g, h, i, j) # putList :: [(a, b, c, d, e, f, g, h, i, j)] -> Put #

## Example

To serialise a custom type, an instance of Binary for that type is required. For example, suppose we have a data structure:

    data Exp = IntE Int
             | OpE  String Exp Exp
       deriving Show

We can encode values of this type into bytestrings using the following instance, which proceeds by recursively breaking down the structure to serialise:

    instance Binary Exp where
          put (IntE i)      = do put (0 :: Word8)
                                 put i
          put (OpE s e1 e2) = do put (1 :: Word8)
                                 put s
                                 put e1
                                 put e2

          get = do t <- get :: Get Word8
                   case t of
                        0 -> do i <- get
                                return (IntE i)
                        1 -> do s  <- get
                                e1 <- get
                                e2 <- get
                                return (OpE s e1 e2)

Note how we write an initial tag byte to indicate each variant of the data type.

We can simplify the writing of `get` instances using monadic combinators:

          get = do tag <- getWord8
                   case tag of
                       0 -> liftM  IntE get
                       1 -> liftM3 OpE  get get get

To serialise this to a bytestring, we use `encode`, which packs the data structure into a binary format, in a lazy bytestring

    > let e = OpE "*" (IntE 7) (OpE "/" (IntE 4) (IntE 2))
    > let v = encode e

Where `v` is a binary encoded data structure. To reconstruct the original data, we use `decode`

    > decode v :: Exp
    OpE "*" (IntE 7) (OpE "/" (IntE 4) (IntE 2))

The lazy ByteString that results from `encode` can be written to disk, and read from disk using Data.ByteString.Lazy IO functions, such as hPutStr or writeFile:

    > writeFile "/tmp/exp.txt" (encode e)

And read back with:

    > readFile "/tmp/exp.txt" >>= return . decode :: IO Exp
    OpE "*" (IntE 7) (OpE "/" (IntE 4) (IntE 2))

We can also directly serialise a value to and from a Handle, or a file:

    > v <- decodeFile  "/tmp/exp.txt" :: IO Exp
    OpE "*" (IntE 7) (OpE "/" (IntE 4) (IntE 2))

And write a value to disk

    > encodeFile "/tmp/a.txt" v

# Generic support

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

This mechanism makes use of GHC's efficient built-in generics support.

class GBinaryGet f where #

Minimal complete definition

gget

Methods

gget :: Get (f t) #

class GBinaryPut f where #

Minimal complete definition

gput

Methods

gput :: f t -> Put #

# The Get and Put monads

data Get a #

Instances

Monad Get # |
Methods (>>=) :: Get a -> (a -> Get b) -> Get b Source # (>>) :: Get a -> Get b -> Get b Source # return :: a -> Get a Source # fail :: String -> Get a Source #
Functor Get # |
Methods fmap :: (a -> b) -> Get a -> Get b Source # (<$) :: a -> Get b -> Get a Source #
MonadFail Get # |
Methods fail :: String -> Get a Source #
Applicative Get # |
Methods pure :: a -> Get a Source # (<*>) :: Get (a -> b) -> Get a -> Get b Source # liftA2 :: (a -> b -> c) -> Get a -> Get b -> Get c Source # (*>) :: Get a -> Get b -> Get b Source # (<*) :: Get a -> Get b -> Get a Source #
Alternative Get # | Since: 0.7.0.0
Methods empty :: Get a Source # (<|>) :: Get a -> Get a -> Get a Source # some :: Get a -> Get [a] Source # many :: Get a -> Get [a] Source #
MonadPlus Get # | Since: 0.7.1.0
Methods mzero :: Get a Source # mplus :: Get a -> Get a -> Get a Source #

type Put = PutM () #

Put merely lifts Builder into a Writer monad, applied to ().

# Useful helpers for writing instances

putWord8 :: Word8 -> Put #

Efficiently write a byte into the output buffer

getWord8 :: Get Word8 #

Read a Word8 from the monad state

# Binary serialisation

encode :: Binary a => a -> ByteString #

Encode a value using binary serialisation to a lazy ByteString.

decode :: Binary a => ByteString -> a #

Decode a value from a lazy ByteString, reconstructing the original structure.

decodeOrFail :: Binary a => ByteString -> Either (ByteString, ByteOffset, String) (ByteString, ByteOffset, a) #

Decode a value from a lazy ByteString. Returning `Left` on failure and `Right` on success. In both cases the unconsumed input and the number of consumed bytes is returned. In case of failure, a human-readable error message will be returned as well.

Since: 0.7.0.0

# IO functions for serialisation

encodeFile :: Binary a => FilePath -> a -> IO () #

Lazily serialise a value to a file.

This is just a convenience function, it's defined simply as:

    encodeFile f = B.writeFile f . encode

So for example if you wanted to compress as well, you could use:

    B.writeFile f . compress . encode

decodeFile :: Binary a => FilePath -> IO a #

Decode a value from a file. In case of errors, `error` will be called with the error message.

Since: 0.7.0.0

decodeFileOrFail :: Binary a => FilePath -> IO (Either (ByteOffset, String) a) #

Decode a value from a file. In case of success, the value will be returned in `Right`. In case of decoder errors, the error message together with the byte offset will be returned.

module Data.Word