Created
April 3, 2012 18:43
-
-
Save sw17ch/2294572 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE DeriveDataTypeable #-} | |
module Main where | |
import Data.Generics (Data, Typeable) | |
import qualified Data.Binary as B | |
import Data.Binary.Generic -- binary-generic package | |
import Text.JSON.Generic (JSValue, toJSON) | |
import Text.JSON.Pretty (render, pp_value) | |
data Foo = Floob { an_int :: Int, a_string :: String } | |
deriving (Show, Data, Typeable) | |
instance B.Binary Foo where | |
get = getGeneric | |
put = putGeneric | |
main :: IO () | |
main = do | |
putStrLn "Generic serialization example." | |
let floob = Floob 9 "some stringy thing" | |
-- Binary | |
let encoded = B.encode floob | |
let decoded = B.decode encoded :: Foo | |
putStrLn "Binary" | |
print floob | |
print encoded | |
print decoded | |
-- JSON | |
putStrLn "" | |
putStrLn "JSON" | |
putStrLn $ render $ pp_value $ toJSON floob | |
{- | |
- Binary | |
- Floob {an_int = 9, a_string = "some stringy thing"} | |
- Chunk "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\t\SOHs\SOHo\SOHm\SOHe\SOH \SOHs\SOHt\SOHr\SOHi\SOHn\SOHg\SOHy\SOH \SOHt\SOHh\SOHi\SOHn\SOHg\NUL" Empty | |
- Floob {an_int = 9, a_string = "some stringy thing"} | |
- | |
- JSON | |
- {"an_int": 9, "a_string": "some stringy thing"} | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment