{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveGeneric #-}
module Json.SubmitMediaTypes 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 Self = Self {
selfHref :: Text
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Self where
parseJSON (Object v) = Self <$> v .: "href"
parseJSON _ = mzero
instance ToJSON Self where
toJSON (Self {..}) = object ["href" .= selfHref]
toEncoding (Self {..}) = pairs ("href" .= selfHref)
data Links = Links {
linksSelf :: Self
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Links where
parseJSON (Object v) = Links <$> v .: "self"
parseJSON _ = mzero
instance ToJSON Links where
toJSON (Links {..}) = object ["self" .= linksSelf]
toEncoding (Links {..}) = pairs ("self" .= linksSelf)
data Metadata = Metadata {
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON Metadata where
parseJSON (Object v) = return Metadata
parseJSON _ = mzero
instance ToJSON Metadata where
toJSON (Metadata {}) = object []
data TopLevel = TopLevel {
topLevelStatus :: Text,
topLevelMediaId :: Text,
topLevelLinks :: Links,
topLevelMetadata :: Metadata
} deriving (Show,Eq,GHC.Generics.Generic)
instance FromJSON TopLevel where
parseJSON (Object v) = TopLevel <$> v .: "status" <*> v .: "mediaId" <*> v .: "_links" <*> v .: "metadata"
parseJSON _ = mzero
instance ToJSON TopLevel where
toJSON (TopLevel {..}) = object ["status" .= topLevelStatus, "mediaId" .= topLevelMediaId, "_links" .= topLevelLinks, "metadata" .= topLevelMetadata]
toEncoding (TopLevel {..}) = pairs ("status" .= topLevelStatus<>"mediaId" .= topLevelMediaId<>"_links" .= topLevelLinks<>"metadata" .= topLevelMetadata)
parse :: ByteString -> Either String TopLevel
parse = eitherDecode