I am trying to parse JSON representation of a RoseTree. Here is a snapshot I have:
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"
But I am getting following error on fileload:
RoseTree2.hs:10:10:
No instance for (GToJSON (GHC.Generics.Rep RoseTree2))
arising from a use of `aeson-0.7.0.6:Data.Aeson.Types.Class.$gdmtoJSON'
Possible fix:
add an instance declaration for
(GToJSON (GHC.Generics.Rep RoseTree2))
In the expression:
(aeson-0.7.0.6:Data.Aeson.Types.Class.$gdmtoJSON)
In an equation for `toJSON':
toJSON = (aeson-0.7.0.6:Data.Aeson.Types.Class.$gdmtoJSON)
In the instance declaration for `ToJSON RoseTree2'
Failed, modules loaded: none.
Could please someone tell me what's wrong with my definition of a JSON parser and how could I fix it? Thanks!
You need to indent the definition of toJSON
instance ToJSON RoseTree2 where
toJSON (RoseNode n cs) =
object [T.pack "value" .= show n
, T.pack "children".= show cs]
You forgot to indent the line after instance ToJSON RoseTree2
so the instance block is closed, and it defaults to
default toJSON :: (Generic a, GToJSON (Rep a)) => a -> Value
toJSON = genericToJSON defaultOptions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.