I would like to convert my json to below format. And to convert from below format to my record. Please check the code that I have written below.
{
"uid" : "bob",
"emailid" : "bob@bob.com",
"email_verified" : "Y" // "Y" for EmailVerified and "N" for EmailNotVerified
}
I have below code where I am trying to convert user type to and from json using Aeson library in Haskell
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import Data.Monoid ((<>))
import GHC.Generics
import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson.Types
data User = User {
userId :: String,
userEmail :: String,
userEmailVerified :: EmailState
} deriving (Show, Generic)
data EmailState = EmailVerified | EmailNotVerified deriving (Generic, Show)
instance ToJSON User where
toJSON u = object [
"uid" .= userId u,
"emailid" .= userEmail u,
"email_verified" .= userEmailVerified u
]
instance FromJSON User where
parseJSON = withObject "User" $ \v -> User
<$> v .: "uid"
<*> v .: "emailid"
<*> v .: "email_verified"
instance ToJSON EmailState
instance FromJSON EmailState
However, My format that I am currently able to generate is like below
{
"uid" : "bob",
"emailid" : "bob@bob.com",
"email_verified" : "EmailVerified"
}