Created
July 20, 2016 20:48
-
-
Save raichoo/0e918d87120dc8360b405b463d1f6331 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 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