Last active
October 20, 2015 11:02
-
-
Save parambirs/6a28126bf0f1c80f0ebb 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 OverloadedStrings #-} | |
module Article where | |
import Data.Text.Lazy | |
import Data.Text.Lazy.Encoding | |
import Data.Aeson | |
import Control.Applicative | |
-- Define the Article constructor | |
-- e.g. Article 12 "some title" "some body text" | |
data Article = Article Integer Text Text -- id title bodyText | |
deriving (Show) | |
-- Tell Aeson how to create an Article object from JSON string. | |
instance FromJSON Article where | |
parseJSON (Object v) = Article <$> | |
v .:? "id" .!= 0 <*> -- the field "id" is optional | |
v .: "title" <*> | |
v .: "bodyText" | |
-- Tell Aeson how to convert an Article object to a JSON string. | |
instance ToJSON Article where | |
toJSON (Article id title bodyText) = | |
object ["id" .= id, | |
"title" .= title, | |
"bodyText" .= bodyText] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment