Add 'yesod-json/' from commit '41a198225f137610cae208190aca443c56efd332'
git-subtree-dir: yesod-json git-subtree-mainline:6f5459f70fgit-subtree-split:41a198225f
This commit is contained in:
commit
83813b1df4
25
yesod-json/LICENSE
Normal file
25
yesod-json/LICENSE
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
The following license covers this documentation, and the source code, except
|
||||||
|
where otherwise indicated.
|
||||||
|
|
||||||
|
Copyright 2010, Michael Snoyman. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR
|
||||||
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||||
|
EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||||
|
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
0
yesod-json/README
Normal file
0
yesod-json/README
Normal file
7
yesod-json/Setup.lhs
Executable file
7
yesod-json/Setup.lhs
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env runhaskell
|
||||||
|
|
||||||
|
> module Main where
|
||||||
|
> import Distribution.Simple
|
||||||
|
|
||||||
|
> main :: IO ()
|
||||||
|
> main = defaultMain
|
||||||
61
yesod-json/Yesod/Json.hs
Normal file
61
yesod-json/Yesod/Json.hs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
{-# LANGUAGE TypeSynonymInstances #-}
|
||||||
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
||||||
|
module Yesod.Json
|
||||||
|
( -- * Convert from a JSON value
|
||||||
|
defaultLayoutJson
|
||||||
|
, jsonToRepJson
|
||||||
|
-- * Compatibility wrapper for old API
|
||||||
|
, Json
|
||||||
|
, jsonScalar
|
||||||
|
, jsonList
|
||||||
|
, jsonMap
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Yesod.Handler (GHandler)
|
||||||
|
import Yesod.Content
|
||||||
|
( ToContent (toContent), RepHtmlJson (RepHtmlJson), RepHtml (RepHtml)
|
||||||
|
, RepJson (RepJson), Content (ContentBuilder)
|
||||||
|
)
|
||||||
|
import Yesod.Core (defaultLayout, Yesod)
|
||||||
|
import Yesod.Widget (GWidget)
|
||||||
|
import qualified Data.Aeson as J
|
||||||
|
import qualified Data.Aeson.Encode as JE
|
||||||
|
import Data.Aeson.Encode (fromValue)
|
||||||
|
import Data.Text (pack)
|
||||||
|
import Control.Arrow (first)
|
||||||
|
import Data.Map (fromList)
|
||||||
|
import qualified Data.Vector as V
|
||||||
|
import Text.Julius (ToJavascript (..))
|
||||||
|
import Data.Text.Lazy.Builder (fromLazyText)
|
||||||
|
import Data.Text.Lazy.Encoding (decodeUtf8)
|
||||||
|
|
||||||
|
instance ToContent J.Value where
|
||||||
|
toContent = flip ContentBuilder Nothing . fromValue
|
||||||
|
|
||||||
|
-- | Provide both an HTML and JSON representation for a piece of data, using
|
||||||
|
-- the default layout for the HTML output ('defaultLayout').
|
||||||
|
defaultLayoutJson :: Yesod master
|
||||||
|
=> GWidget sub master ()
|
||||||
|
-> J.Value
|
||||||
|
-> GHandler sub master RepHtmlJson
|
||||||
|
defaultLayoutJson w json = do
|
||||||
|
RepHtml html' <- defaultLayout w
|
||||||
|
return $ RepHtmlJson html' $ toContent json
|
||||||
|
|
||||||
|
-- | 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.String . pack
|
||||||
|
|
||||||
|
jsonList :: [Json] -> Json
|
||||||
|
jsonList = J.Array . V.fromList
|
||||||
|
|
||||||
|
jsonMap :: [(String, Json)] -> Json
|
||||||
|
jsonMap = J.Object . fromList . map (first pack)
|
||||||
|
|
||||||
|
instance ToJavascript J.Value where
|
||||||
|
toJavascript = fromLazyText . decodeUtf8 . JE.encode
|
||||||
28
yesod-json/yesod-json.cabal
Normal file
28
yesod-json/yesod-json.cabal
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
name: yesod-json
|
||||||
|
version: 0.2.0
|
||||||
|
license: BSD3
|
||||||
|
license-file: LICENSE
|
||||||
|
author: Michael Snoyman <michael@snoyman.com>
|
||||||
|
maintainer: Michael Snoyman <michael@snoyman.com>
|
||||||
|
synopsis: Generate content for Yesod using the aeson package.
|
||||||
|
category: Web, Yesod
|
||||||
|
stability: Stable
|
||||||
|
cabal-version: >= 1.6
|
||||||
|
build-type: Simple
|
||||||
|
homepage: http://www.yesodweb.com/
|
||||||
|
|
||||||
|
library
|
||||||
|
build-depends: base >= 4 && < 5
|
||||||
|
, yesod-core >= 0.9 && < 0.10
|
||||||
|
, aeson >= 0.3.1.1 && < 0.4
|
||||||
|
, text >= 0.8 && < 0.12
|
||||||
|
, hamlet >= 0.9 && < 0.10
|
||||||
|
, vector
|
||||||
|
, containers
|
||||||
|
, blaze-textual >= 0.1 && < 0.2
|
||||||
|
exposed-modules: Yesod.Json
|
||||||
|
ghc-options: -Wall
|
||||||
|
|
||||||
|
source-repository head
|
||||||
|
type: git
|
||||||
|
location: git://github.com/snoyberg/yesod-json.git
|
||||||
Loading…
Reference in New Issue
Block a user