You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

25 lines
751 B

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Worldcup.Events (Event(..), parseEvents) where
import Data.Aeson
import Data.Aeson.Types as AET
import Prelude as P
data Event = Event {
eventId :: Int,
eventName :: String } deriving (Show)
instance FromJSON Event where
parseJSON = withObject "event" $ \o -> do
eventId <- o .: "id"
eventName <- o .: "key"
return Event{..}
parseEvents :: Either String [Value] -> [Event]
parseEvents (Left x) = error x
parseEvents (Right xs) = P.foldl reduce [] xs where
reduce acc x = case (parseEither parseJSON x :: Either String Event) of
(Left s) -> error s
(Right v) -> v : acc