Add JSONResponse type

Closes #1481
This commit is contained in:
Maximilian Tagher 2018-03-11 21:05:29 -07:00
parent ff5618bd15
commit ba2593c15f
4 changed files with 25 additions and 1 deletions

View File

@ -1,3 +1,7 @@
## 1.6.3
* Add a type `JSONResponse a` that can be used to type a Handler as returning some value `a`, which should be encoded as JSON. [#1481](https://github.com/yesodweb/yesod/issues/1481)
## 1.6.2
* Derive a `Show` instance for `ResourceTree` and `FlatResource` [#1492](https://github.com/yesodweb/yesod/pull/1492)

View File

@ -251,6 +251,12 @@ instance HasContentType J.Value where
instance HasContentType J.Encoding where
getContentType _ = typeJson
instance ToContent (JSONResponse a) where
toContent (JSONResponse a) = toContent $ toEncoding a
instance HasContentType (JSONResponse a) where
getContentType _ = typeJson
instance HasContentType Html where
getContentType _ = typeHtml
@ -288,6 +294,8 @@ instance ToTypedContent J.Value where
toTypedContent v = TypedContent typeJson (toContent v)
instance ToTypedContent J.Encoding where
toTypedContent e = TypedContent typeJson (toContent e)
instance ToTypedContent (JSONResponse a) where
toTypedContent c = TypedContent typeJson (toContent c)
instance ToTypedContent Html where
toTypedContent h = TypedContent typeHtml (toContent h)
instance ToTypedContent T.Text where

View File

@ -308,6 +308,18 @@ newtype RepXml = RepXml Content
type ContentType = ByteString -- FIXME Text?
-- | Wrapper around types so that Handlers can be typed them, encoded as JSON.
-- Example usage in a type signature:
--
-- > postSignupR :: Handler (JSONResponse CreateUserResponse)
--
-- And in the implementation:
--
-- > return $ JSONResponse $ CreateUserResponse userId
--
-- @since 1.6.3
data JSONResponse a = ToJSON a => JSONResponse a
-- | Prevents a response body from being fully evaluated before sending the
-- request.
--

View File

@ -1,5 +1,5 @@
name: yesod-core
version: 1.6.2
version: 1.6.3
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>