rpc-0.0.0: Data/Serialize/Send.hs
module Data.Serialize.Send (hSerialize, hDeserialize) where
import System.IO (hFlush, Handle)
import Data.Serialize
import Data.Serialize.Put
import Data.Serialize.Get
import Data.ByteString as B
import Control.Monad
-- |Serializes an object to the given handle
hSerialize :: Serialize a => Handle -> a -> IO ()
hSerialize h x = hPut h $ runPut (putWord8 (fromIntegral (B.length a))) `append` a
where a = encode x
-- |Deserializes an object to the given handle
hDeserialize :: Serialize a => Handle -> IO a
hDeserialize h = do
(Right l) <- liftM (runGet getWord8) (hGet h 1)
(Right x) <- liftM decode (hGet h (fromIntegral l))
return x