@@ -15,10 +15,12 @@ import Control.Arrow (left)
1515import Data.Aeson (FromJSON, ToJSON, eitherDecode,
1616 encode)
1717import qualified Data.ByteString as BS
18−import Data.ByteString.Lazy (ByteString)
18+import Data.ByteString.Lazy (ByteString, fromStrict, toStrict)
1919import Data.String.Conversions (cs)
20−import qualified Data.Text.Lazy as Text
21−import qualified Data.Text.Lazy.Encoding as Text
20+import qualified Data.Text.Lazy as TextL
21+import qualified Data.Text.Lazy.Encoding as TextL
22+import qualified Data.Text as TextS
23+import qualified Data.Text.Encoding as TextS
2224import Data.Typeable
2325import GHC.Exts (Constraint)
2426import qualified Network.HTTP.Media as M
@@ -73,6 +75,7 @@ newtype AcceptHeader = AcceptHeader BS.ByteString
7375-- > toByteString _ val = pack ("This is MINE! " ++ show val)
7476-- >
7577-- > type MyAPI = "path" :> Get '[MyContentType] Int
78+--
7679class Accept ctype => MimeRender ctype a where
7780 toByteString :: Proxy ctype -> a -> ByteString
7881
@@ -92,6 +95,20 @@ instance ( AllMimeRender ctyps a, IsNonEmpty ctyps
9295
9396--------------------------------------------------------------------------
9497-- * Unrender
98+
99+-- | Instantiate this class to register a way of deserializing a type based
100+-- on the request's @Content-Type@ header.
101+--
102+-- > data MyContentType = MyContentType String
103+-- >
104+-- > instance Accept MyContentType where
105+-- > contentType _ = "example" // "prs.me.mine" /: ("charset", "utf-8")
106+-- >
107+-- > instance Show a => MimeRender MyContentType where
108+-- > fromByteString _ bs = MyContentType $ unpack bs
109+-- >
110+-- > type MyAPI = "path" :> ReqBody '[MyContentType] :> Get '[JSON] Int
111+--
95112class Accept ctype => MimeUnrender ctype a where
96113 fromByteString :: Proxy ctype -> ByteString -> Either String a
97114
@@ -162,9 +179,13 @@ type family IsNonEmpty (ls::[*]) :: Constraint where
162179instance ToJSON a => MimeRender JSON a where
163180 toByteString _ = encode
164181
165−-- | `Text.encodeUtf8`
166−instance MimeRender PlainText Text.Text where
167− toByteString _ = Text.encodeUtf8
182+-- | `TextL.encodeUtf8`
183+instance MimeRender PlainText TextL.Text where
184+ toByteString _ = TextL.encodeUtf8
185+
186+-- | `fromStrict . TextS.encodeUtf8`
187+instance MimeRender PlainText TextS.Text where
188+ toByteString _ = fromStrict . TextS.encodeUtf8
168189
169190-- | `id`
170191instance MimeRender OctetStream ByteString where
@@ -177,9 +198,13 @@ instance MimeRender OctetStream ByteString where
177198instance FromJSON a => MimeUnrender JSON a where
178199 fromByteString _ = eitherDecode
179200
180−-- | `left show . Text.decodeUtf8'`
181−instance MimeUnrender PlainText Text.Text where
182− fromByteString _ = left show . Text.decodeUtf8'
201+-- | `left show . TextL.decodeUtf8'`
202+instance MimeUnrender PlainText TextL.Text where
203+ fromByteString _ = left show . TextL.decodeUtf8'
204+
205+-- | `left show . TextS.decodeUtf8' . toStrict`
206+instance MimeUnrender PlainText TextS.Text where
207+ fromByteString _ = left show . TextS.decodeUtf8' . toStrict
183208
184209-- | `Right . id`
185210instance MimeUnrender OctetStream ByteString where