Skip to content

Instantly share code, notes, and snippets.

@aslatter
Created March 15, 2012 22:47
Show Gist options
  • Select an option

  • Save aslatter/2047445 to your computer and use it in GitHub Desktop.

Select an option

Save aslatter/2047445 to your computer and use it in GitHub Desktop.
Generic Serialization w/Cereal
{-# 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