Haskell Aeson解构泛型解析

时间:2012-11-08 11:13:59

标签: haskell aeson

我有一个

风格的JSON请求
{"command":"get","params":{"something":"something else"}}

以及来自Yesod book

的此代码段
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai (Response, responseLBS, Application, requestBody)
import Network.HTTP.Types (status200, status400)
import Network.Wai.Handler.Warp (run)
import Data.Aeson.Parser (json)
import Data.Conduit.Attoparsec (sinkParser)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (Value(..), encode, object, (.=))
import Control.Exception (SomeException)
import Data.ByteString (ByteString)
import Data.Conduit (ResourceT, ($$))
import Control.Exception.Lifted (handle)

main :: IO ()
main = run 3000 app

app :: Application
app req = handle invalidJson $ do
    value <- requestBody req $$ sinkParser json
    newValue <- liftIO $ modValue value
    return $ responseLBS
        status200
        [("Content-Type", "application/json")]
        $ encode newValue

invalidJson :: SomeException -> ResourceT IO Response
invalidJson ex = return $ responseLBS
    status400
    [("Content-Type", "application/json")]
    $ encode $ object
        [ ("message" .= show ex)
        ]

-- Application-specific logic would go here.
modValue :: Value -> IO Value
modValue (Object o) 
    | -- key "command" corresponds to value "get"
    | otherwise = fail "Invalid command"

但我无法理解如何解构生成的Value数据结构。我如何获取密钥等的值。我意识到我可以解析为明确定义的数据结构,但这会给我的用例带来其他类型的问题。

在modValue中我发表评论,我无法弄清楚要放什么。我试过把它当作一张地图,因为它是在Aeson里面实现的,但显然不会打字。

编辑:

将Data.HashMap添加到导入并使用行

    | M.lookup "command" o == Just "get" = return $ object [("result" .= (String "YAY"))]

给出以下错误消息。

main.hs:39:26:
Couldn't match expected type `M.Map k0 a0'
            with actual type `aeson-0.6.0.2:Data.Aeson.Types.Internal.Object'
In the second argument of `M.lookup', namely `o'
In the first argument of `(==)', namely `M.lookup "command" o'
In the expression: M.lookup "command" o == Just "get"

EDIT2:

在突然的预感中,我追踪了我之前收到的涉及“无序容器”的错误消息。这是Aeson使用的包。但我意识到我还安装了包hashmap,它被导入为Data.HashMap。来自无序容器的哈希映射将导入为Data.HashMap.Strict或Lazy!

将行import qualified Data.HashMap as M更改为import qualified Data.HashMap.Strict as M无论如何都要修复它。现在给出的答案有效!

1 个答案:

答案 0 :(得分:5)

aeson JSON object is a Hashmap起,您可以使用Hasmap interface,在这种情况下lookup

import qualified Data.HashMap.Strict as M

M.lookup "command" o == Just "get"