从yesod处理程序返回JSON

时间:2013-06-24 18:32:10

标签: json api haskell yesod

我正在尝试从Yesod的处理程序中编写一个最简单的JSON响应,但有一些非常愚蠢的错误(显然)。我的处理程序代码是这样的:

-- HelloYesod/Handler/Echo.hs
module Handler.Echo where

import           Data.Aeson      (object, (.=))
import qualified Data.Aeson      as J
import           Data.Text       (pack)
import           Import
import           Yesod.Core.Json (returnJson)

getEchoR :: String -> Handler RepJson
getEchoR theText = do
  let json = object $ ["data" .= "val"]
  return json

错误是这样的:

Handler/Echo.hs:12:10:
    Couldn't match expected type `RepJson' with actual type `Value'
    In the first argument of `return', namely `json'
    In a stmt of a 'do' block: return json
    In the expression:
      do { let json = object $ ...;
           return json }
Build failure, pausing...

1 个答案:

答案 0 :(得分:7)

我也被这个人抓住了:你只需改变你的类型签名就可以了:

getEchoR :: String -> Handler Value

我的理解是整个Rep系统在Yesod 1.2中被弃用,因此Handler现在返回Html和Value而不是RepHtml和RepJson。

希望这有帮助!