{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveGeneric #-}
module Json.ProgressTypes where
import System.Exit (exitFailure, exitSuccess)
import System.IO (stderr, hPutStrLn)
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.ByteString.Lazy
import System.Environment (getArgs)
import Control.Monad (forM_, mzero, join)
import Control.Applicative
import Data.Aeson.AutoType.Alternative
import Data.Aeson(eitherDecode, Value(..), FromJSON(..), ToJSON(..),
pairs,
(.:), (.:?), (.=), object)
import Data.Monoid
import Data.Text (Text)
import qualified GHC.Generics
o .:?? val = fmap join (o .:? val)
data Tasks = Tasks {
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Tasks where
parseJSON (Object v) = return Tasks
parseJSON _ = mzero
instance ToJSON Tasks where
toJSON (Tasks {}) = object []
data Progress = Progress {
progressStatus :: Text,
progressJobId :: Text,
progressTasks :: Tasks
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Progress where
parseJSON (Object v) = Progress <$> v .: "status" <*> v .: "jobId" <*> v .: "tasks"
parseJSON _ = mzero
instance ToJSON Progress where
toJSON (Progress {..}) = object ["status" .= progressStatus, "jobId" .= progressJobId, "tasks" .= progressTasks]
toEncoding (Progress {..}) = pairs ("status" .= progressStatus<>"jobId" .= progressJobId<>"tasks" .= progressTasks)
data TopLevel = TopLevel {
topLevelStatus :: Text,
topLevelProgress :: Progress,
topLevelMediaId :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON TopLevel where
parseJSON (Object v) = TopLevel <$> v .: "status" <*> v .: "progress" <*> v .: "mediaId"
parseJSON _ = mzero
instance ToJSON TopLevel where
toJSON (TopLevel {..}) = object ["status" .= topLevelStatus, "progress" .= topLevelProgress, "mediaId" .= topLevelMediaId]
toEncoding (TopLevel {..}) = pairs ("status" .= topLevelStatus<>"progress" .= topLevelProgress<>"mediaId" .= topLevelMediaId)
parse :: ByteString -> Either String TopLevel
parse = eitherDecode