{-# LANGUAGE AutoDeriveTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
module Servant.Yaml where
import Data.Yaml
(FromJSON, ToJSON, decodeEither', encode,
prettyPrintParseException)
import Servant.API
(Accept (..), MimeRender (..), MimeUnrender (..))
import qualified Data.ByteString.Lazy as LBS
import qualified Network.HTTP.Media as M
data YAML
instance Accept YAML where
contentType _ = "application" M.// "x-yaml"
instance ToJSON a => MimeRender YAML a where
mimeRender _ = LBS.fromStrict . encode
instance FromJSON a => MimeUnrender YAML a where
mimeUnrender _ = either (Left . prettyPrintParseException) Right . decodeEither' . LBS.toStrict