Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize all values to ByteString, to avoid garbling Unicode strings…
…. Encode strings in UTF8. Network/Memcache/Serializable.hs (Serializable): Support ByteString. Serialize all types to ByteString instead of String. Encode/decode strings to/from UTF8. (toString, fromString, toStringL, fromStringL): Renamed to serialize, deserialize, serializeL, deserializeL. Updated all references. Network/Memcache/Protocol.hs: Store all values in memcache as ByteString's. (hGetNetLn): Rewrote to use hGetLine. memcached.cabal: Depend on the bytestring and utf8-light packages.
- Loading branch information
Showing
5 changed files
with
56 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,56 @@ | ||
-- Memcached interface. | ||
-- Copyright (C) 2005 Evan Martin <martine@danga.com> | ||
|
||
module Network.Memcache.Serializable(Serializable, toString, fromString) where | ||
module Network.Memcache.Serializable(Serializable, serialize, deserialize) where | ||
|
||
import Data.ByteString (ByteString) | ||
import Codec.Binary.UTF8.Light (encode, decode) | ||
|
||
-- It'd be nice to use "show" for serialization, but when we | ||
-- serialize a String we want to serialize it without the quotes. | ||
|
||
-- TODO: | ||
-- - allow serializing bytes as Ptr | ||
-- to do this, add a "putToHandle", etc. method in Serializable | ||
-- where the default uses toString, but for Ptr uses socket stuff. | ||
-- where the default uses serialize, but for Ptr uses socket stuff. | ||
|
||
--import Foreign.Marshal.Utils | ||
--import Foreign.Storable (Storable, sizeOf) | ||
|
||
class Serializable a where | ||
toString :: a -> String | ||
fromString :: String -> Maybe a | ||
serialize :: a -> ByteString | ||
deserialize :: ByteString -> Maybe a | ||
|
||
toStringL :: [a] -> String | ||
fromStringL :: String -> [a] | ||
serializeL :: [a] -> ByteString | ||
deserializeL :: ByteString -> [a] | ||
|
||
toStringL = error "unimp" | ||
fromStringL = error "unimp" | ||
serializeL = error "unimp" | ||
deserializeL = error "unimp" | ||
|
||
instance Serializable Char where | ||
-- people will rarely want to serialize a single char, | ||
-- but we define them for completeness. | ||
toString x = [x] | ||
fromString (c:[]) = Just c | ||
fromString _ = Nothing | ||
serialize x = encode [x] | ||
deserialize s = | ||
case decode s of | ||
(c:[]) -> Just c | ||
_ -> Nothing | ||
|
||
-- the real use is for serializing strings. | ||
toStringL = id | ||
fromStringL = id | ||
serializeL = encode | ||
deserializeL = decode | ||
|
||
instance Serializable ByteString where | ||
serialize = id | ||
deserialize = Just | ||
|
||
-- ...do I really need to copy everything instance of Show? | ||
instance Serializable Int where | ||
toString = show | ||
fromString = Just . read | ||
serialize = encode . show | ||
deserialize = Just . read . decode | ||
|
||
instance (Serializable a) => Serializable [a] where | ||
toString = toStringL | ||
fromString = Just . fromStringL | ||
serialize = serializeL | ||
deserialize = Just . deserializeL | ||
|
||
-- vim: set ts=2 sw=2 et : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters