Added support for aeson's toEncoding function (>= 0.11)
This commit is contained in:
parent
2a01710f4b
commit
18cd7834d6
@ -251,9 +251,20 @@ instance ToContent J.Value where
|
|||||||
#else
|
#else
|
||||||
. fromValue
|
. fromValue
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
instance ToContent J.Encoding where
|
||||||
|
toContent = flip ContentBuilder Nothing . J.fromEncoding
|
||||||
|
#endif
|
||||||
|
|
||||||
instance HasContentType J.Value where
|
instance HasContentType J.Value where
|
||||||
getContentType _ = typeJson
|
getContentType _ = typeJson
|
||||||
|
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
instance HasContentType J.Encoding where
|
||||||
|
getContentType _ = typeJson
|
||||||
|
#endif
|
||||||
|
|
||||||
instance HasContentType Html where
|
instance HasContentType Html where
|
||||||
getContentType _ = typeHtml
|
getContentType _ = typeHtml
|
||||||
|
|
||||||
@ -289,6 +300,10 @@ instance ToTypedContent RepXml where
|
|||||||
toTypedContent (RepXml c) = TypedContent typeXml c
|
toTypedContent (RepXml c) = TypedContent typeXml c
|
||||||
instance ToTypedContent J.Value where
|
instance ToTypedContent J.Value where
|
||||||
toTypedContent v = TypedContent typeJson (toContent v)
|
toTypedContent v = TypedContent typeJson (toContent v)
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
instance ToTypedContent J.Encoding where
|
||||||
|
toTypedContent e = TypedContent typeJson (toContent e)
|
||||||
|
#endif
|
||||||
instance ToTypedContent Html where
|
instance ToTypedContent Html where
|
||||||
toTypedContent h = TypedContent typeHtml (toContent h)
|
toTypedContent h = TypedContent typeHtml (toContent h)
|
||||||
instance ToTypedContent T.Text where
|
instance ToTypedContent T.Text where
|
||||||
|
|||||||
@ -6,6 +6,9 @@ module Yesod.Core.Json
|
|||||||
defaultLayoutJson
|
defaultLayoutJson
|
||||||
, jsonToRepJson
|
, jsonToRepJson
|
||||||
, returnJson
|
, returnJson
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
, returnJsonEncoding
|
||||||
|
#endif
|
||||||
, provideJson
|
, provideJson
|
||||||
|
|
||||||
-- * Convert to a JSON value
|
-- * Convert to a JSON value
|
||||||
@ -24,6 +27,9 @@ module Yesod.Core.Json
|
|||||||
|
|
||||||
-- * Convenience functions
|
-- * Convenience functions
|
||||||
, jsonOrRedirect
|
, jsonOrRedirect
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
, jsonEncodingOrRedirect
|
||||||
|
#endif
|
||||||
, acceptsJson
|
, acceptsJson
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@ -59,7 +65,11 @@ defaultLayoutJson :: (Yesod site, J.ToJSON a)
|
|||||||
-> HandlerT site IO TypedContent
|
-> HandlerT site IO TypedContent
|
||||||
defaultLayoutJson w json = selectRep $ do
|
defaultLayoutJson w json = selectRep $ do
|
||||||
provideRep $ defaultLayout w
|
provideRep $ defaultLayout w
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
provideRep $ fmap J.toEncoding json
|
||||||
|
#else
|
||||||
provideRep $ fmap J.toJSON json
|
provideRep $ fmap J.toJSON json
|
||||||
|
#endif
|
||||||
|
|
||||||
-- | Wraps a data type in a 'RepJson'. The data type must
|
-- | Wraps a data type in a 'RepJson'. The data type must
|
||||||
-- support conversion to JSON via 'J.ToJSON'.
|
-- support conversion to JSON via 'J.ToJSON'.
|
||||||
@ -75,12 +85,24 @@ jsonToRepJson = return . J.toJSON
|
|||||||
returnJson :: (Monad m, J.ToJSON a) => a -> m J.Value
|
returnJson :: (Monad m, J.ToJSON a) => a -> m J.Value
|
||||||
returnJson = return . J.toJSON
|
returnJson = return . J.toJSON
|
||||||
|
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
-- | Convert a value to a JSON representation via aeson\'s 'J.toEncoding' function.
|
||||||
|
--
|
||||||
|
-- Since ?
|
||||||
|
returnJsonEncoding :: (Monad m, J.ToJSON a) => a -> m J.Encoding
|
||||||
|
returnJsonEncoding = return . J.toEncoding
|
||||||
|
#endif
|
||||||
|
|
||||||
-- | Provide a JSON representation for usage with 'selectReps', using aeson\'s
|
-- | Provide a JSON representation for usage with 'selectReps', using aeson\'s
|
||||||
-- 'J.toJSON' function to perform the conversion.
|
-- 'J.toJSON' (aeson >= 0.11: 'J.toEncoding') function to perform the conversion.
|
||||||
--
|
--
|
||||||
-- Since 1.2.1
|
-- Since 1.2.1
|
||||||
provideJson :: (Monad m, J.ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()
|
provideJson :: (Monad m, J.ToJSON a) => a -> Writer (Endo [ProvidedRep m]) ()
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
provideJson = provideRep . return . J.toEncoding
|
||||||
|
#else
|
||||||
provideJson = provideRep . return . J.toJSON
|
provideJson = provideRep . return . J.toJSON
|
||||||
|
#endif
|
||||||
|
|
||||||
-- | Parse the request body to a data type as a JSON value. The
|
-- | Parse the request body to a data type as a JSON value. The
|
||||||
-- data type must support conversion from JSON via 'J.FromJSON'.
|
-- data type must support conversion from JSON via 'J.FromJSON'.
|
||||||
@ -129,9 +151,32 @@ jsonOrRedirect :: (MonadHandler m, J.ToJSON a)
|
|||||||
=> Route (HandlerSite m) -- ^ Redirect target
|
=> Route (HandlerSite m) -- ^ Redirect target
|
||||||
-> a -- ^ Data to send via JSON
|
-> a -- ^ Data to send via JSON
|
||||||
-> m J.Value
|
-> m J.Value
|
||||||
jsonOrRedirect r j = do
|
jsonOrRedirect = jsonOrRedirect' J.toJSON
|
||||||
|
|
||||||
|
#if MIN_VERSION_aeson(0, 11, 0)
|
||||||
|
-- | jsonEncodingOrRedirect simplifies the scenario where a POST handler sends a different
|
||||||
|
-- response based on Accept headers:
|
||||||
|
--
|
||||||
|
-- 1. 200 with JSON data if the client prefers
|
||||||
|
-- @application\/json@ (e.g. AJAX, see 'acceptsJSON').
|
||||||
|
--
|
||||||
|
-- 2. 3xx otherwise, following the PRG pattern.
|
||||||
|
-- Since ?
|
||||||
|
jsonEncodingOrRedirect :: (MonadHandler m, J.ToJSON a)
|
||||||
|
=> Route (HandlerSite m) -- ^ Redirect target
|
||||||
|
-> a -- ^ Data to send via JSON
|
||||||
|
-> m J.Encoding
|
||||||
|
jsonEncodingOrRedirect = jsonOrRedirect' J.toEncoding
|
||||||
|
#endif
|
||||||
|
|
||||||
|
jsonOrRedirect' :: (MonadHandler m, J.ToJSON a)
|
||||||
|
=> (a -> b)
|
||||||
|
-> Route (HandlerSite m) -- ^ Redirect target
|
||||||
|
-> a -- ^ Data to send via JSON
|
||||||
|
-> m b
|
||||||
|
jsonOrRedirect' f r j = do
|
||||||
q <- acceptsJson
|
q <- acceptsJson
|
||||||
if q then return (J.toJSON j)
|
if q then return (f j)
|
||||||
else redirect r
|
else redirect r
|
||||||
|
|
||||||
-- | Returns @True@ if the client prefers @application\/json@ as
|
-- | Returns @True@ if the client prefers @application\/json@ as
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user