diff --git a/Yesod/Json.hs b/Yesod/Json.hs index d35a4710..4f83f4d5 100644 --- a/Yesod/Json.hs +++ b/Yesod/Json.hs @@ -1,8 +1,14 @@ {-# LANGUAGE TypeSynonymInstances #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Yesod.Json - ( defaultLayoutJson + ( -- ^ Convert from a JSON value + defaultLayoutJson , jsonToRepJson + -- ^ Compatibility wrapper for old API + , Json + , jsonScalar + , jsonList + , jsonMap ) where import Yesod.Handler (GHandler) @@ -14,6 +20,9 @@ import Yesod.Core (defaultLayout, Yesod) import Yesod.Widget (GWidget) import qualified Data.JSON.Types as J import qualified Text.JSON.Enumerator as J +import Data.Text.Lazy (pack) +import Control.Arrow (first) +import Data.Map (fromList) instance ToContent J.Value where toContent = flip ContentBuilder Nothing . J.renderValue @@ -31,3 +40,14 @@ defaultLayoutJson w json = do -- | Wraps the 'Content' generated by 'jsonToContent' in a 'RepJson'. jsonToRepJson :: J.Value -> GHandler sub master RepJson jsonToRepJson = return . RepJson . toContent + +type Json = J.Value + +jsonScalar :: String -> Json +jsonScalar = J.ValueAtom . J.AtomText . pack + +jsonList :: [Json] -> Json +jsonList = J.ValueArray + +jsonMap :: [(String, Json)] -> Json +jsonMap = J.ValueObject . fromList . map (first pack) diff --git a/yesod-json.cabal b/yesod-json.cabal index 5c5d15d4..95147a5d 100644 --- a/yesod-json.cabal +++ b/yesod-json.cabal @@ -16,6 +16,8 @@ library , yesod-core >= 0.7 && < 0.8 , json-enumerator >= 0.0 && < 0.1 , json-types >= 0.1 && < 0.2 + , containers >= 0.2 && < 0.5 + , text >= 0.8 && < 0.12 exposed-modules: Yesod.Json ghc-options: -Wall