Permalink
Browse files

Messing with Data.Binary

  • Loading branch information...
1 parent 3c0ab6a commit 36716af68db0bd0c96d91d6c97e775f812bf67db Zach Moazeni committed Apr 3, 2012
Showing with 27 additions and 0 deletions.
  1. +27 −0 experiment/BinaryTest.hs
View
@@ -0,0 +1,27 @@
+import Data.Binary
+import Data.Text.Lazy (pack, unpack, concat)
+import Data.Text.Lazy.Encoding (encodeUtf8, decodeUtf8)
+import Control.Monad (replicateM)
+import Data.ByteString.Lazy (ByteString)
+
+type Strings = [String]
+data CustomType = CustomType { customStrings :: [String] }
+ deriving (Show)
+
+instance Binary CustomType where
+ put (CustomType strings) = do put $ length(strings)
+ let encodedStrings = map (encodeUtf8 . pack) strings
+ mapM_ put encodedStrings
+
+ get = do num <- get :: Get Int
+ encodedStrings <- replicateM num (get :: Get ByteString)
+ let strings = map (unpack . decodeUtf8) encodedStrings
+ return (CustomType strings)
+
+
+main :: IO ()
+main = do let encoded = encode $ CustomType ["fo✘⊥o", "bar"]
+ decoded = decode encoded :: CustomType
+ print encoded
+ print decoded
+ putStrLn . head $ customStrings decoded

0 comments on commit 36716af

Please sign in to comment.