How to parse array into tuple with aeson?

半世苍凉 提交于 2019-12-24 08:19:27

问题


If I have an array ["addTask", {"id": "1", "description": "d", "dependsOn": [], "dependentTasks": []}].

data Task = Task
    { id :: String
    , description :: String
    , dependsOn :: [String]
    , dependentTasks :: [String]
    } deriving (Eq, Show, Generic, ToJSON, FromJSON)
type Change = Storage -> Storage
addTask :: Task -> Change
addTask (Task id desc dep dept) = insert id (Task id desc dep dept)

How can I create a parser that would produce a addTask from that?

instance FromJSON (Storage -> Storage) where
    parseJSON (Array v) = do
        name <- parseJSON $ v V.! 0 :: Just String
        task <- parseJSON $ v V.! 1 :: Just Task
        return (addTask task)

This is my current attempt which doesn't work.


回答1:


I would go on and parse the Array into a Tuple (String,Task) and then use simple pattern matching, you might need to specify the type of "addTask" if you are using OverloadedStrings, which can be done with ScopedTypeVariables (just add it to the left hand side of the .. :: (String, Task) <- parse..).

instance FromJSON (Storage -> Storage) where
    parseJSON v = do ("addTask",t) <- parseJSON v
                     return $ addTask t

Note: I did not compile or test this code

Edit

Using alternative is really easy you just define two parsers and combine them

instance FromJSON (Storage -> Storage) where
    parseJSON v = let addP = do ("addTask",t) <- parseJSON v
                                return $ addTask t
                      rmP = do ("rmTask",n) <- parseJSON v
                               return $ rmTask n
                   in addP <|> rmP

I am assuming that rmTask :: Int -> Change otherwise cou can always case on the first element of the tuple.



来源:https://stackoverflow.com/questions/42707231/how-to-parse-array-into-tuple-with-aeson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!