@@ -15,10 +15,12 @@ import Control.Arrow (left)
15import Data.Aeson (FromJSON, ToJSON, eitherDecode,
15import Data.Aeson (FromJSON, ToJSON, eitherDecode,
17import qualified Data.ByteString as BS
17import qualified Data.ByteString as BS
18−import Data.ByteString.Lazy (ByteString)
18+import Data.ByteString.Lazy (ByteString, fromStrict, toStrict)
19import Data.String.Conversions (cs)
19import Data.String.Conversions (cs)
20−import qualified Data.Text.Lazy as Text
20+import qualified Data.Text.Lazy as TextL
21−import qualified Data.Text.Lazy.Encoding as Text
21+import qualified Data.Text.Lazy.Encoding as TextL
22+import qualified Data.Text as TextS
23+import qualified Data.Text.Encoding as TextS
22import Data.Typeable
24import Data.Typeable
23import GHC.Exts (Constraint)
25import GHC.Exts (Constraint)
24import qualified Network.HTTP.Media as M
26import qualified Network.HTTP.Media as M
@@ -73,6 +75,7 @@ newtype AcceptHeader = AcceptHeader BS.ByteString
73-- > toByteString _ val = pack ("This is MINE! " ++ show val)
75-- > toByteString _ val = pack ("This is MINE! " ++ show val)
75-- > type MyAPI = "path" :> Get '[MyContentType] Int
77-- > type MyAPI = "path" :> Get '[MyContentType] Int
76class Accept ctype => MimeRender ctype a where
79class Accept ctype => MimeRender ctype a where
77 toByteString :: Proxy ctype -> a -> ByteString
80 toByteString :: Proxy ctype -> a -> ByteString
@@ -92,6 +95,20 @@ instance ( AllMimeRender ctyps a, IsNonEmpty ctyps
93--------------------------------------------------------------------------
96--------------------------------------------------------------------------
94-- * Unrender
97-- * Unrender
99+-- | Instantiate this class to register a way of deserializing a type based
100+-- on the request's @Content-Type@ header.
102+-- > data MyContentType = MyContentType String
104+-- > instance Accept MyContentType where
105+-- > contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")
107+-- > instance Show a => MimeRender MyContentType where
108+-- > fromByteString _ bs = MyContentType $ unpack bs
110+-- > type MyAPI = "path" :> ReqBody '[MyContentType] :> Get '[JSON] Int
95class Accept ctype => MimeUnrender ctype a where
112class Accept ctype => MimeUnrender ctype a where
96 fromByteString :: Proxy ctype -> ByteString -> Either String a
113 fromByteString :: Proxy ctype -> ByteString -> Either String a
@@ -162,9 +179,13 @@ type family IsNonEmpty (ls::[*]) :: Constraint where
162instance ToJSON a => MimeRender JSON a where
179instance ToJSON a => MimeRender JSON a where
163 toByteString _ = encode
180 toByteString _ = encode
182+-- | `TextL.encodeUtf8`
183+instance MimeRender PlainText TextL.Text where
165−-- | `Text.encodeUtf8`
184+ toByteString _ = TextL.encodeUtf8
186+-- | `fromStrict . TextS.encodeUtf8`
166−instance MimeRender PlainText Text.Text where
187+instance MimeRender PlainText TextS.Text where
167− toByteString _ = Text.encodeUtf8
188+ toByteString _ = fromStrict . TextS.encodeUtf8
170instance MimeRender OctetStream ByteString where
191instance MimeRender OctetStream ByteString where
@@ -177,9 +198,13 @@ instance MimeRender OctetStream ByteString where
177instance FromJSON a => MimeUnrender JSON a where
198instance FromJSON a => MimeUnrender JSON a where
178 fromByteString _ = eitherDecode
199 fromByteString _ = eitherDecode
180−-- | `left show . Text.decodeUtf8'`
201+-- | `left show . TextL.decodeUtf8'`
202+instance MimeUnrender PlainText TextL.Text where
203+ fromByteString _ = left show . TextL.decodeUtf8'
205+-- | `left show . TextS.decodeUtf8' . toStrict`
181−instance MimeUnrender PlainText Text.Text where
206+instance MimeUnrender PlainText TextS.Text where
182− fromByteString _ = left show . Text.decodeUtf8'
207+ fromByteString _ = left show . TextS.decodeUtf8' . toStrict
184-- | `Right . id`
209-- | `Right . id`
185instance MimeUnrender OctetStream ByteString where
210instance MimeUnrender OctetStream ByteString where