Skip to content

Commit

Permalink
Merge pull request #1592 from StevenXL/add-jsonresponse-type
Browse files Browse the repository at this point in the history
Introduce JSONResponse.
  • Loading branch information
snoyberg committed Apr 13, 2019
2 parents 6a7370a + ab096c6 commit 42fbab9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions yesod-core/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for yesod-core

## 1.6.14

* Introduce `JSONResponse`. [issue #1481](https://github.com/yesodweb/yesod/issues/1481) and [PR #1592](https://github.com/yesodweb/yesod/pull/1592)

## 1.6.13

* Introduce `maxContentLengthIO`. [issue #1588](https://github.com/yesodweb/yesod/issues/1588) and [PR #1589](https://github.com/yesodweb/yesod/pull/1589)
Expand Down
6 changes: 6 additions & 0 deletions yesod-core/src/Yesod/Core/Content.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ instance ToContent (ContentType, Content) where
toContent = snd
instance ToContent TypedContent where
toContent (TypedContent _ c) = c
instance ToContent (JSONResponse a) where
toContent (JSONResponse a) = toContent $ J.toEncoding a

instance ToContent Css where
toContent = toContent . renderCss
Expand Down Expand Up @@ -160,6 +162,8 @@ deriving instance ToContent RepJson
instance HasContentType RepPlain where
getContentType _ = typePlain
deriving instance ToContent RepPlain
instance HasContentType (JSONResponse a) where
getContentType _ = typeJson

instance HasContentType RepXml where
getContentType _ = typeXml
Expand Down Expand Up @@ -292,6 +296,8 @@ instance ToTypedContent [Char] where
toTypedContent = toTypedContent . pack
instance ToTypedContent Text where
toTypedContent t = TypedContent typePlain (toContent t)
instance ToTypedContent (JSONResponse a) where
toTypedContent c = TypedContent typeJson (toContent c)
instance ToTypedContent a => ToTypedContent (DontFullyEvaluate a) where
toTypedContent (DontFullyEvaluate a) =
let TypedContent ct c = toTypedContent a
Expand Down
16 changes: 16 additions & 0 deletions yesod-core/src/Yesod/Core/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}
module Yesod.Core.Types where

import Data.Aeson (ToJSON)
import qualified Data.ByteString.Builder as BB
import Control.Arrow (first)
import Control.Exception (Exception)
Expand Down Expand Up @@ -303,6 +305,20 @@ newtype RepXml = RepXml Content

type ContentType = ByteString -- FIXME Text?

-- | Wrapper around types so that Handlers can return a domain type, even when
-- the data will eventually be encoded as JSON.
-- Example usage in a type signature:
--
-- > postSignupR :: Handler (JSONResponse CreateUserResponse)
--
-- And in the implementation:
--
-- > return $ JSONResponse $ CreateUserResponse userId
--
-- @since 1.6.14
data JSONResponse a where
JSONResponse :: ToJSON a => a -> JSONResponse a

-- | Prevents a response body from being fully evaluated before sending the
-- request.
--
Expand Down

0 comments on commit 42fbab9

Please sign in to comment.