forked from snoyberg/yesod-js
/
Types.hs
44 lines (39 loc) · 1.3 KB
/
Types.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
39
40
41
42
43
44
{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}
module Types
( PersonId (..)
, Person (..)
, PersonSummary (..)
, Singleton (..)
) where
import Data.Aeson (ToJSON (..), FromJSON (..), (.:), (.=), object, Value (Object, Array))
import Data.Text (Text)
import Control.Applicative ((<$>), (<*>))
import qualified Data.Vector as V
newtype PersonId = PersonId Text
deriving (ToJSON, FromJSON, Ord, Eq)
data Person = Person Text Int
instance ToJSON Person where
toJSON (Person name age) = object
[ "name" .= name
, "age" .= age
]
instance FromJSON Person where
parseJSON (Object o) = Person
<$> o .: "name"
<*> o .: "age"
parseJSON _ = fail "Expected an object"
data PersonSummary = PersonSummary PersonId Text
instance ToJSON PersonSummary where
toJSON (PersonSummary pid name) = object
[ "id" .= pid
, "name" .= name
]
newtype Singleton a = Singleton { unSingleton :: a }
instance ToJSON a => ToJSON (Singleton a) where
toJSON = Array . V.singleton . toJSON . unSingleton
instance FromJSON a => FromJSON (Singleton a) where
parseJSON (Array a) =
case V.toList a of
[x] -> Singleton <$> parseJSON x
_ -> fail "Not a single-element array"
parseJSON _ = fail "Not an array"