Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Network/Memcache/Serializable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

module Network.Memcache.Serializable(Serializable, serialize, deserialize) where

import Prelude hiding (head)
import Data.ByteString (ByteString)
import Codec.Binary.UTF8.Light (encode, decode)
import Data.Text hiding (map)
import Data.Text.Encoding

-- It'd be nice to use "show" for serialization, but when we
-- serialize a String we want to serialize it without the quotes.
Expand All @@ -30,24 +32,21 @@ class Serializable a where
instance Serializable Char where
-- people will rarely want to serialize a single char,
-- but we define them for completeness.
serialize x = encode [x]
deserialize s =
case decode s of
(c:[]) -> Just c
_ -> Nothing
serialize x = encodeUtf8 $ singleton x
deserialize s = Just $ head $ decodeUtf8 s

-- the real use is for serializing strings.
serializeL = encode
deserializeL = decode
serializeL = encodeUtf8 . pack
deserializeL = unpack . decodeUtf8

instance Serializable ByteString where
serialize = id
deserialize = Just

-- ...do I really need to copy everything instance of Show?
instance Serializable Int where
serialize = encode . show
deserialize = Just . read . decode
serialize = serializeL . show
deserialize = Just . read . unpack . decodeUtf8

instance (Serializable a) => Serializable [a] where
serialize = serializeL
Expand Down
2 changes: 1 addition & 1 deletion memcached.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build-depends:
base >3 && <5,
network,
bytestring >=0.9 && <0.10,
utf8-light >=0.4 && <1.0
text

extra-source-files:
Setup.hs,
Expand Down