Skip to content

Instantly share code, notes, and snippets.

@parambirs
Last active October 20, 2015 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parambirs/6a28126bf0f1c80f0ebb to your computer and use it in GitHub Desktop.
Save parambirs/6a28126bf0f1c80f0ebb to your computer and use it in GitHub Desktop.
{-# 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