从代码in this Answer开始,我有一个快速工作的东西,它解析yaml文件中的待办事项任务项列表。
但如果我添加第二个文档:Data.Yaml.decode不再解码任何内容,只返回Nothing
。
像这样:
---
- name: >
this is a test task
state: finished
- name: >
this is a second test task
state: todo
---
- name: noname
state: nostate
所以我想知道在使用Data.Yaml解析时是否可以使用Yaml的文档功能?
或者我做错了什么?
这是代码:
{-# LANGUAGE OverloadedStrings #-}
import Data.Yaml
import Control.Applicative -- <$>, <*>
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as BS
data Task = Task { name :: String,
state :: String }
deriving (Show)
instance FromJSON Task where
parseJSON (Object v) = Task <$>
v .: "name" <*>
v .: "state"
-- A non-Object value is of the wrong type, so fail.
parseJSON _ = error "Can't parse Task from YAML/JSON"
main = do
ymlData <- BS.readFile "/tmp/test2.yaml"
let users = Data.Yaml.decode ymlData :: Maybe [Task]
-- Print it, just for show
print $ fromJust users
答案 0 :(得分:1)
您需要...
来分隔文档,例如:
---
- name: >
this is a test task
state: finished
- name: >
this is a second test task
state: todo
...
---
- name: noname
state: nostate