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

CopyrightLennart Kolmodin
LicenseBSD3-style (see LICENSE)
MaintainerLennart Kolmodin <kolmodin@gmail.com>
Stabilityunstable
Portabilityportable to Hugs and GHC. Requires the FFI and some flexible instances.
Safe HaskellTrustworthy
LanguageHaskell98

Data.Binary

Contents

Description

Binary serialisation of Haskell values to and from lazy ByteStrings. 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

The Binary class

classBinary t whereSource#

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 -> PutSource#

Encode a value in the Put monad.

get :: Get t Source#

Decode a value in the Get monad

putList :: [t] -> PutSource#

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 -> PutSource#

Encode a value in the Put monad.

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

Decode a value in the Get monad

Instances
BinaryBoolSource# 
Instance details

Defined in Data.Binary.Class

BinaryCharSource# 
Instance details

Defined in Data.Binary.Class

BinaryDoubleSource# 
Instance details

Defined in Data.Binary.Class

BinaryFloatSource# 
Instance details

Defined in Data.Binary.Class

BinaryIntSource# 
Instance details

Defined in Data.Binary.Class

BinaryInt8Source# 
Instance details

Defined in Data.Binary.Class

BinaryInt16Source# 
Instance details

Defined in Data.Binary.Class

BinaryInt32Source# 
Instance details

Defined in Data.Binary.Class

BinaryInt64Source# 
Instance details

Defined in Data.Binary.Class

BinaryIntegerSource# 
Instance details

Defined in Data.Binary.Class

BinaryNaturalSource#

Since: 0.7.3.0

Instance details

Defined in Data.Binary.Class

BinaryOrderingSource# 
Instance details

Defined in Data.Binary.Class

BinaryWordSource# 
Instance details

Defined in Data.Binary.Class

BinaryWord8Source# 
Instance details

Defined in Data.Binary.Class

BinaryWord16Source# 
Instance details

Defined in Data.Binary.Class

BinaryWord32Source# 
Instance details

Defined in Data.Binary.Class

BinaryWord64Source# 
Instance details

Defined in Data.Binary.Class

BinaryRuntimeRepSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinaryVecCountSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinaryVecElemSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinarySomeTypeRepSource# 
Instance details

Defined in Data.Binary.Class

Binary ()Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: () -> PutSource#

get :: Get () Source#

putList :: [()] -> PutSource#

BinaryTyConSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinaryKindRepSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinaryTypeLitSortSource#

@since 0.8.5.0. See

Instance details

Defined in Data.Binary.Class

BinaryVoidSource#

Since: 0.8.0.0

Instance details

Defined in Data.Binary.Class

BinaryVersionSource#

Since: 0.8.0.0

Instance details

Defined in Data.Binary.Class

BinaryAllSource#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

BinaryAnySource#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

BinaryFingerprintSource#

Since: 0.7.6.0

Instance details

Defined in Data.Binary.Class

BinaryByteStringSource# 
Instance details

Defined in Data.Binary.Class

BinaryShortByteStringSource# 
Instance details

Defined in Data.Binary.Class

BinaryByteStringSource# 
Instance details

Defined in Data.Binary.Class

BinaryIntSetSource# 
Instance details

Defined in Data.Binary.Class

Binary a => Binary [a]Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: [a] -> PutSource#

get :: Get [a] Source#

putList :: [[a]] -> PutSource#

Binary a => Binary (Maybe a)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Maybe a -> PutSource#

get :: Get (Maybe a) Source#

putList :: [Maybe a] -> PutSource#

(Binary a, Integral a) => Binary (Ratio a)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Ratio a -> PutSource#

get :: Get (Ratio a) Source#

putList :: [Ratio a] -> PutSource#

Binary a => Binary (Complex a)Source# 
Instance details

Defined in Data.Binary.Class

Binary (Fixed a)Source#

Since: 0.8.0.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Fixed a -> PutSource#

get :: Get (Fixed a) Source#

putList :: [Fixed a] -> PutSource#

Binary a => Binary (Min a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Min a -> PutSource#

get :: Get (Min a) Source#

putList :: [Min a] -> PutSource#

Binary a => Binary (Max a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Max a -> PutSource#

get :: Get (Max a) Source#

putList :: [Max a] -> PutSource#

Binary a => Binary (First a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: First a -> PutSource#

get :: Get (First a) Source#

putList :: [First a] -> PutSource#

Binary a => Binary (Last a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Last a -> PutSource#

get :: Get (Last a) Source#

putList :: [Last a] -> PutSource#

Binary m => Binary (WrappedMonoid m)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Binary a => Binary (Option a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Binary a => Binary (Identity a)Source# 
Instance details

Defined in Data.Binary.Class

Binary a => Binary (First a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: First a -> PutSource#

get :: Get (First a) Source#

putList :: [First a] -> PutSource#

Binary a => Binary (Last a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Last a -> PutSource#

get :: Get (Last a) Source#

putList :: [Last a] -> PutSource#

Binary a => Binary (Dual a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Dual a -> PutSource#

get :: Get (Dual a) Source#

putList :: [Dual a] -> PutSource#

Binary a => Binary (Sum a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Sum a -> PutSource#

get :: Get (Sum a) Source#

putList :: [Sum a] -> PutSource#

Binary a => Binary (Product a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Binary a => Binary (NonEmpty a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Binary e => Binary (IntMap e)Source# 
Instance details

Defined in Data.Binary.Class

Binary e => Binary (Tree e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Tree e -> PutSource#

get :: Get (Tree e) Source#

putList :: [Tree e] -> PutSource#

Binary e => Binary (Seq e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Seq e -> PutSource#

get :: Get (Seq e) Source#

putList :: [Seq e] -> PutSource#

Binary a => Binary (Set a)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Set a -> PutSource#

get :: Get (Set a) Source#

putList :: [Set a] -> PutSource#

(Binary a, Binary b) => Binary (Either a b)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Either a b -> PutSource#

get :: Get (Either a b) Source#

putList :: [Either a b] -> PutSource#

Typeable a => Binary (TypeRep a)Source# 
Instance details

Defined in Data.Binary.Class

(Binary a, Binary b) => Binary (a, b)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b) -> PutSource#

get :: Get (a, b) Source#

putList :: [(a, b)] -> PutSource#

(Binary i, Ix i, Binary e, IArrayUArray e) => Binary (UArray i e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: UArray i e -> PutSource#

get :: Get (UArray i e) Source#

putList :: [UArray i e] -> PutSource#

(Binary i, Ix i, Binary e) => Binary (Array i e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Array i e -> PutSource#

get :: Get (Array i e) Source#

putList :: [Array i e] -> PutSource#

(Binary a, Binary b) => Binary (Arg a b)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Arg a b -> PutSource#

get :: Get (Arg a b) Source#

putList :: [Arg a b] -> PutSource#

(Binary k, Binary e) => Binary (Map k e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: Map k e -> PutSource#

get :: Get (Map k e) Source#

putList :: [Map k e] -> PutSource#

(Binary a, Binary b, Binary c) => Binary (a, b, c)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c) -> PutSource#

get :: Get (a, b, c) Source#

putList :: [(a, b, c)] -> PutSource#

Binary (f a) => Binary (Alt f a)Source#

Since: 0.8.4.0

Instance details

Defined in Data.Binary.Class

Methods

put :: Alt f a -> PutSource#

get :: Get (Alt f a) Source#

putList :: [Alt f a] -> PutSource#

(Binary a, Binary b, Binary c, Binary d) => Binary (a, b, c, d)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d) -> PutSource#

get :: Get (a, b, c, d) Source#

putList :: [(a, b, c, d)] -> PutSource#

(Binary a, Binary b, Binary c, Binary d, Binary e) => Binary (a, b, c, d, e)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e) -> PutSource#

get :: Get (a, b, c, d, e) Source#

putList :: [(a, b, c, d, e)] -> PutSource#

(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f) => Binary (a, b, c, d, e, f)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e, f) -> PutSource#

get :: Get (a, b, c, d, e, f) Source#

putList :: [(a, b, c, d, e, f)] -> PutSource#

(Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g) => Binary (a, b, c, d, e, f, g)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e, f, g) -> PutSource#

get :: Get (a, b, c, d, e, f, g) Source#

putList :: [(a, b, c, d, e, f, g)] -> PutSource#

(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)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e, f, g, h) -> PutSource#

get :: Get (a, b, c, d, e, f, g, h) Source#

putList :: [(a, b, c, d, e, f, g, h)] -> PutSource#

(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)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e, f, g, h, i) -> PutSource#

get :: Get (a, b, c, d, e, f, g, h, i) Source#

putList :: [(a, b, c, d, e, f, g, h, i)] -> PutSource#

(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)Source# 
Instance details

Defined in Data.Binary.Class

Methods

put :: (a, b, c, d, e, f, g, h, i, j) -> PutSource#

get :: Get (a, b, c, d, e, f, g, h, i, j) Source#

putList :: [(a, b, c, d, e, f, g, h, i, j)] -> PutSource#

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.

classGBinaryGet f whereSource#

Minimal complete definition

gget

Methods

gget :: Get (f t) Source#

Instances
GBinaryGet (V1 :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get (V1 t) Source#

GBinaryGet (U1 :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get (U1 t) Source#

Binary a => GBinaryGet (K1 i a :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get (K1 i a t) Source#

(GSumGet a, GSumGet b, SumSize a, SumSize b) => GBinaryGet (a :+: b :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get ((a :+: b) t) Source#

(GBinaryGet a, GBinaryGet b) => GBinaryGet (a :*: b :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get ((a :*: b) t) Source#

GBinaryGet a => GBinaryGet (M1 i c a :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gget :: Get (M1 i c a t) Source#

classGBinaryPut f whereSource#

Minimal complete definition

gput

Methods

gput :: f t -> PutSource#

Instances
GBinaryPut (V1 :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: V1 t -> PutSource#

GBinaryPut (U1 :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: U1 t -> PutSource#

Binary a => GBinaryPut (K1 i a :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: K1 i a t -> PutSource#

(GSumPut a, GSumPut b, SumSize a, SumSize b) => GBinaryPut (a :+: b :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: (a :+: b) t -> PutSource#

(GBinaryPut a, GBinaryPut b) => GBinaryPut (a :*: b :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: (a :*: b) t -> PutSource#

GBinaryPut a => GBinaryPut (M1 i c a :: * -> *)Source# 
Instance details

Defined in Data.Binary.Generic

Methods

gput :: M1 i c a t -> PutSource#

The Get and Put monads

dataGet a Source#

Instances
MonadGetSource# 
Instance details

Defined in Data.Binary.Get.Internal

Methods

(>>=) :: Get a -> (a -> Get b) -> Get b #

(>>) :: Get a -> Get b -> Get b #

return :: a -> Get a #

fail :: String -> Get a #

FunctorGetSource# 
Instance details

Defined in Data.Binary.Get.Internal

Methods

fmap :: (a -> b) -> Get a -> Get b #

(<$) :: a -> Get b -> Get a #

MonadFailGetSource# 
Instance details

Defined in Data.Binary.Get.Internal

Methods

fail :: String -> Get a #

ApplicativeGetSource# 
Instance details

Defined in Data.Binary.Get.Internal

Methods

pure :: a -> Get a #

(<*>) :: Get (a -> b) -> Get a -> Get b #

liftA2 :: (a -> b -> c) -> Get a -> Get b -> Get c #

(*>) :: Get a -> Get b -> Get b #

(<*) :: Get a -> Get b -> Get a #

AlternativeGetSource#

Since: 0.7.0.0

Instance details

Defined in Data.Binary.Get.Internal

Methods

empty :: Get a #

(<|>) :: Get a -> Get a -> Get a #

some :: Get a -> Get [a] #

many :: Get a -> Get [a] #

MonadPlusGetSource#

Since: 0.7.1.0

Instance details

Defined in Data.Binary.Get.Internal

Methods

mzero :: Get a #

mplus :: Get a -> Get a -> Get a #

typePut = PutM () Source#

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

Useful helpers for writing instances

putWord8 :: Word8 -> PutSource#

Efficiently write a byte into the output buffer

getWord8 :: GetWord8Source#

Read a Word8 from the monad state

Binary serialisation

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

Encode a value using binary serialisation to a lazy ByteString.

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

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

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

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 () Source#

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 Source#

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) Source#

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

close