@@ -1,29 +1,30 @@
1+{-# LANGUAGE ConstraintKinds #-}
2+{-# LANGUAGE DataKinds #-}
1−{-# LANGUAGE DeriveDataTypeable #-}
3+{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE DataKinds #-}
3−{-# LANGUAGE ConstraintKinds #-}
4−{-# LANGUAGE FlexibleInstances #-}
4+{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6−{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE OverloadedStrings #-}
7−{-# LANGUAGE PolyKinds #-}
7+{-# LANGUAGE PolyKinds #-}
8−{-# LANGUAGE ScopedTypeVariables #-}
8+{-# LANGUAGE ScopedTypeVariables #-}
9−{-# LANGUAGE TypeFamilies #-}
9+{-# LANGUAGE TypeFamilies #-}
10−{-# LANGUAGE TypeOperators #-}
10+{-# LANGUAGE TypeOperators #-}
11−{-# LANGUAGE UndecidableInstances #-}
11+{-# LANGUAGE UndecidableInstances #-}
12module Servant.API.ContentTypes where
12module Servant.API.ContentTypes where
14+import Control.Arrow (left)
15+import Data.Aeson (FromJSON, ToJSON, eitherDecode,
14import qualified Data.ByteString as BS
17import qualified Data.ByteString as BS
15import Data.ByteString.Lazy (ByteString)
18import Data.ByteString.Lazy (ByteString)
16import Data.String.Conversions (cs)
19import Data.String.Conversions (cs)
20+import qualified Data.Text.Lazy as Text
21+import qualified Data.Text.Lazy.Encoding as Text
17import Data.Typeable
22import Data.Typeable
18import GHC.Exts (Constraint)
23import GHC.Exts (Constraint)
19import qualified Network.HTTP.Media as M
24import qualified Network.HTTP.Media as M
21-- * Provided content types
26-- * Provided content types
22−data XML deriving Typeable
23−data HTML deriving Typeable
24data JSON deriving Typeable
27data JSON deriving Typeable
25−data JavaScript deriving Typeable
26−data CSS deriving Typeable
27data PlainText deriving Typeable
28data PlainText deriving Typeable
28data OctetStream deriving Typeable
29data OctetStream deriving Typeable
@@ -41,26 +42,10 @@ data OctetStream deriving Typeable
41class Accept ctype where
42class Accept ctype where
42 contentType :: Proxy ctype -> M.MediaType
43 contentType :: Proxy ctype -> M.MediaType
44−-- | @text/html;charset=utf-8@
45−instance Accept HTML where
46− contentType _ = "text" M.// "html" M./: ("charset", "utf-8")
48-- | @application/json;charset=utf-8@
45-- | @application/json;charset=utf-8@
49instance Accept JSON where
46instance Accept JSON where
50 contentType _ = "application" M.// "json" M./: ("charset", "utf-8")
47 contentType _ = "application" M.// "json" M./: ("charset", "utf-8")
52−-- | @application/xml;charset=utf-8@
53−instance Accept XML where
54− contentType _ = "application" M.// "xml" M./: ("charset", "utf-8")
56−-- | @application/javascript;charset=utf-8@
57−instance Accept JavaScript where
58− contentType _ = "application" M.// "javascript" M./: ("charset", "utf-8")
60−-- | @text/css;charset=utf-8@
61−instance Accept CSS where
62− contentType _ = "text" M.// "css" M./: ("charset", "utf-8")
64-- | @text/plain;charset=utf-8@
49-- | @text/plain;charset=utf-8@
65instance Accept PlainText where
50instance Accept PlainText where
66 contentType _ = "text" M.// "plain" M./: ("charset", "utf-8")
51 contentType _ = "text" M.// "plain" M./: ("charset", "utf-8")
@@ -168,3 +153,34 @@ instance ( MimeUnrender ctyp a
169type family IsNonEmpty (ls::[*]) :: Constraint where
154type family IsNonEmpty (ls::[*]) :: Constraint where
170 IsNonEmpty (x ': xs) = ()
155 IsNonEmpty (x ': xs) = ()
158+--------------------------------------------------------------------------
159+-- * MimeRender Instances
162+instance ToJSON a => MimeRender JSON a where
163+ toByteString _ = encode
165+-- | `Text.encodeUtf8`
166+instance MimeRender PlainText Text.Text where
167+ toByteString _ = Text.encodeUtf8
170+instance MimeRender OctetStream ByteString where
173+--------------------------------------------------------------------------
174+-- * MimeUnrender Instances
177+instance FromJSON a => MimeUnrender JSON a where
178+ fromByteString _ = eitherDecode
180+-- | `left show . Text.decodeUtf8'`
181+instance MimeUnrender PlainText Text.Text where
182+ fromByteString _ = left show . Text.decodeUtf8'
185+instance MimeUnrender OctetStream ByteString where
186+ fromByteString _ = Right . id