/
Person.hs
38 lines (30 loc) · 904 Bytes
/
Person.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
module Person where
import GHC.Generics
import Data.Aeson
import Data.Text as T
data Person = Person {
name :: T.Text
, address :: T.Text
, city :: T.Text
, state :: T.Text
, zip :: T.Text
, country :: T.Text
, telephone :: T.Text
} deriving (Generic, Show)
cityState :: Person -> T.Text
cityState p = T.pack $ T.unpack (Person.city p) ++ ", " ++ T.unpack (Person.state p)
addressFields :: Person -> [T.Text]
addressFields p = fmap (\f -> f p) [
Person.address,
Person.cityState,
Person.zip,
Person.country
]
instance ToJSON Person where
-- No need to provide a toJSON implementation.
-- For efficiency, we write a simple toEncoding implementation, as
-- the default version uses toJSON.
toEncoding = genericToEncoding defaultOptions
instance FromJSON Person
-- No need to provide a parseJSON implementation.