Created
March 15, 2012 22:47
-
-
Save aslatter/2047445 to your computer and use it in GitHub Desktop.
Generic Serialization w/Cereal
This file contains hidden or 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 DeriveGeneric #-} | |
| import Data.Int | |
| import Data.Serialize | |
| import GHC.Generics | |
| data DemoType | |
| = Case1 {this::Int8, that::String} | |
| | Case2 | |
| | Case3 {foo::DemoType2} | |
| deriving (Generic, Show) | |
| data DemoType2 = DemoType2 Double | |
| deriving (Generic, Show) | |
| instance Serialize DemoType | |
| instance Serialize DemoType2 | |
| testValue1 = Case1 4 "hello, world" | |
| testValue2 = Case2 | |
| testValue3 = Case3 $ DemoType2 4.3 | |
| roundTrip :: Serialize a => a -> Either String a | |
| roundTrip = decode . encode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment