More generic types on Yesod.Json using FromJSON/ToJSON.
This commit is contained in:
parent
697ebbb2f5
commit
00603fe2f0
@ -5,8 +5,10 @@ module Yesod.Json
|
|||||||
( -- * Convert from a JSON value
|
( -- * Convert from a JSON value
|
||||||
defaultLayoutJson
|
defaultLayoutJson
|
||||||
, jsonToRepJson
|
, jsonToRepJson
|
||||||
|
|
||||||
-- * Convert to a JSON value
|
-- * Convert to a JSON value
|
||||||
, parseJsonBody
|
, parseJsonBody
|
||||||
|
|
||||||
-- * Produce JSON values
|
-- * Produce JSON values
|
||||||
, J.Value (..)
|
, J.Value (..)
|
||||||
, object
|
, object
|
||||||
@ -20,6 +22,7 @@ import Yesod.Content
|
|||||||
)
|
)
|
||||||
import Yesod.Core (defaultLayout, Yesod)
|
import Yesod.Core (defaultLayout, Yesod)
|
||||||
import Yesod.Widget (GWidget)
|
import Yesod.Widget (GWidget)
|
||||||
|
import Control.Arrow (second)
|
||||||
import qualified Data.Aeson as J
|
import qualified Data.Aeson as J
|
||||||
import qualified Data.Aeson.Encode as JE
|
import qualified Data.Aeson.Encode as JE
|
||||||
import Data.Aeson.Encode (fromValue)
|
import Data.Aeson.Encode (fromValue)
|
||||||
@ -40,35 +43,44 @@ instance ToContent J.Value where
|
|||||||
. toLazyText
|
. toLazyText
|
||||||
. fromValue
|
. fromValue
|
||||||
|
|
||||||
-- | Provide both an HTML and JSON representation for a piece of data, using
|
-- | Provide both an HTML and JSON representation for a piece of
|
||||||
-- the default layout for the HTML output ('defaultLayout').
|
-- data, using the default layout for the HTML output
|
||||||
defaultLayoutJson :: Yesod master
|
-- ('defaultLayout').
|
||||||
=> GWidget sub master ()
|
--
|
||||||
-> J.Value
|
-- /Since: 0.3.0/
|
||||||
|
defaultLayoutJson :: (Yesod master, J.ToJSON a)
|
||||||
|
=> GWidget sub master () -- ^ HTML
|
||||||
|
-> a -- ^ JSON
|
||||||
-> GHandler sub master RepHtmlJson
|
-> GHandler sub master RepHtmlJson
|
||||||
defaultLayoutJson w json = do
|
defaultLayoutJson w json = do
|
||||||
RepHtml html' <- defaultLayout w
|
RepHtml html' <- defaultLayout w
|
||||||
return $ RepHtmlJson html' $ toContent json
|
return $ RepHtmlJson html' $ toContent (J.toJSON json)
|
||||||
|
|
||||||
-- | Wraps the 'Content' generated by 'jsonToContent' in a 'RepJson'.
|
-- | Wraps a data type in a 'RepJson'. The data type must
|
||||||
jsonToRepJson :: J.Value -> GHandler sub master RepJson
|
-- support conversion to JSON via 'J.ToJSON'.
|
||||||
jsonToRepJson = return . RepJson . toContent
|
|
||||||
|
|
||||||
-- | Parse the request body as a JSON value.
|
|
||||||
--
|
--
|
||||||
-- /Since: 0.2.3/
|
-- /Since: 0.3.0/
|
||||||
parseJsonBody :: GHandler sub master J.Value
|
jsonToRepJson :: J.ToJSON a => a -> GHandler sub master RepJson
|
||||||
|
jsonToRepJson = return . RepJson . toContent . J.toJSON
|
||||||
|
|
||||||
|
-- | Parse the request body to a data type as a JSON value. The
|
||||||
|
-- data type must support conversion from JSON via 'J.FromJSON'.
|
||||||
|
-- If you want the raw JSON value, just ask for a @'J.Result'
|
||||||
|
-- 'J.Value'@.
|
||||||
|
--
|
||||||
|
-- /Since: 0.3.0/
|
||||||
|
parseJsonBody :: J.FromJSON a => GHandler sub master (J.Result a)
|
||||||
parseJsonBody = do
|
parseJsonBody = do
|
||||||
req <- waiRequest
|
req <- waiRequest
|
||||||
lift $ requestBody req $$ sinkParser J.json'
|
fmap J.fromJSON $ lift $ requestBody req $$ sinkParser J.json'
|
||||||
|
|
||||||
instance ToJavascript J.Value where
|
instance ToJavascript J.Value where
|
||||||
toJavascript = fromLazyText . decodeUtf8 . JE.encode
|
toJavascript = fromLazyText . decodeUtf8 . JE.encode
|
||||||
|
|
||||||
-- | Convert a list of pairs to an 'J.Object'.
|
-- | Convert a list of pairs to an 'J.Object'.
|
||||||
object :: [(Text, J.Value)] -> J.Value
|
object :: J.ToJSON a => [(Text, a)] -> J.Value
|
||||||
object = J.object
|
object = J.object . map (second J.toJSON)
|
||||||
|
|
||||||
-- | Convert a list of values to an 'J.Array'.
|
-- | Convert a list of values to an 'J.Array'.
|
||||||
array :: [J.Value] -> J.Value
|
array :: J.ToJSON a => [a] -> J.Value
|
||||||
array = J.Array . V.fromList
|
array = J.Array . V.fromList . map J.toJSON
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user