Skip to content

Instantly share code, notes, and snippets.

@raichoo
Created July 20, 2016 20:48
Show Gist options
  • Save raichoo/0e918d87120dc8360b405b463d1f6331 to your computer and use it in GitHub Desktop.
Save raichoo/0e918d87120dc8360b405b463d1f6331 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
module AesonTag where
import Data.Aeson
import GHC.Generics
data Foo
= Bar Int
| Quux { tag :: Bool }
deriving (Generic, Show)
instance FromJSON Foo
instance ToJSON Foo
test :: IO ()
test = do
let x = Bar 1337
y = Quux True
print (encode x) -- "{\"tag\":\"Bar\",\"contents\":1337}"
print (decode (encode x) :: Maybe Foo) -- Just (Bar 1337)
print (encode y) -- "{\"tag\":true}"
print (decode (encode y) :: Maybe Foo) -- Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment