在Haskell中解析RoseTree JSON

在Haskell中解析RoseTree JSON,json,parsing,haskell,Json,Parsing,Haskell,我试图解析RoseTree的JSON表示。这是我的一张快照: module RoseTree2 where import Data.Tree import Data.Aeson import qualified Data.Text as T import Control.Applicative data RoseTree2 = RoseNode Int [RoseTree2] deriving (Show) instance ToJSON RoseTree2 where toJSON (R

我试图解析RoseTree的JSON表示。这是我的一张快照:

module RoseTree2 where

import Data.Tree
import Data.Aeson
import qualified Data.Text as T
import Control.Applicative

data RoseTree2 = RoseNode Int [RoseTree2] deriving (Show)

instance ToJSON RoseTree2 where
toJSON (RoseNode n cs) =
    object [T.pack "value" .= show n
    , T.pack "children".= show cs]

instance FromJSON RoseTree2 where
    parseJSON (Object o) =
        RoseNode <$> o.: T.pack "value"
        <*> o.: T.pack "children"

有人能告诉我JSON解析器的定义有什么问题吗?我该如何修复它?谢谢

您忘了在
instance to json RoseTree2
之后缩进行,因此实例块被关闭,默认为

default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
toJSON = genericToJSON defaultOptions

您需要缩进
toJSON

instance ToJSON RoseTree2 where
  toJSON (RoseNode n cs) =
    object [T.pack "value" .= show n
    , T.pack "children".= show cs]
instance ToJSON RoseTree2 where
  toJSON (RoseNode n cs) =
    object [T.pack "value" .= show n
    , T.pack "children".= show cs]