Safe Haskell | Safe-Infered |
---|

This module provides the `Serializable`

type class and functions
to convert to and from `Payload`

s. It's implemented in terms of the
`cereal`

package's Data.Serialize.

- class (Serialize a, Typeable a) => Serializable a
- serialEncode :: Serializable a => a -> IO Payload
- serialEncodePure :: Serializable a => a -> Payload
- serialDecode :: forall a. Serializable a => Payload -> IO (Maybe a)
- serialDecodePure :: forall a. Serializable a => Payload -> Maybe a
- dynamicDecodePure :: Serializable a => DynamicPayload -> Maybe a
- dynamicEncodePure :: Serializable a => a -> DynamicPayload
- data Payload
- data DynamicPayload
- type PayloadLength = Int
- hPutPayload :: Handle -> Payload -> IO ()
- hGetPayload :: Handle -> IO Payload
- payloadLength :: Payload -> PayloadLength
- getPayloadType :: Payload -> String
- getDynamicPayloadType :: DynamicPayload -> String
- getPayloadContent :: Payload -> ByteString
- genericPut :: Data a => a -> Put
- genericGet :: forall a. Data a => Get a

# Documentation

class (Serialize a, Typeable a) => Serializable a

Data that can be sent as a message must implement
this class. The class has no functions of its own,
but instead simply requires that the type implement
both `Typeable`

and `Binary`

. Typeable can usually
be derived automatically. Binary requires the put and get
functions, which can be easily implemented by hand,
or you can use the `genericGet`

and `genericPut`

flavors,
which will work automatically for types implementing
`Data`

.

(Serialize a, Typeable a) => Serializable a |

serialEncode :: Serializable a => a -> IO Payload

serialEncodePure :: Serializable a => a -> Payload

serialDecode :: forall a. Serializable a => Payload -> IO (Maybe a)

serialDecodePure :: forall a. Serializable a => Payload -> Maybe a

dynamicDecodePure :: Serializable a => DynamicPayload -> Maybe a

dynamicEncodePure :: Serializable a => a -> DynamicPayload

data DynamicPayload

type PayloadLength = Int

hPutPayload :: Handle -> Payload -> IO ()

hGetPayload :: Handle -> IO Payload

getPayloadType :: Payload -> String

genericPut :: Data a => a -> Put

Data types that can be used in messaging must
be serializable, which means that they must implement
the `get`

and `put`

methods from `Binary`

. If you
are too lazy to write these functions yourself,
you can delegate responsibility to this function.
It's usually sufficient to do something like this:

import Data.Data (Data) import Data.Typeable (Typeable) import Data.Binary (Binary, get, put) data MyType = MkMyType Foobar Int [(String, Waddle Baz)] | MkSpatula deriving (Data, Typeable) instance Binary MyType where put = genericPut get = genericGet

genericGet :: forall a. Data a => Get a

This is the counterpart `genericPut`