意外的Haskell Aeson警告:没有明确实施' toJSON"

时间:2015-10-09 18:57:45

标签: haskell aeson

我正在尝试使用aeson库进行json解析,我正在关注文档。这是我现在的代码:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

import Data.Aeson as Ae
import Data.Text  as T
import qualified Data.ByteString.Lazy as BS
import GHC.Generics

data Episode = Episode { season :: Int
                       , epNum  :: Int
                       } deriving (Show, Generic)

data Series = Series { title      :: !T.Text
                     , curEpisode :: Episode
                     } deriving (Show, Generic)

instance FromJSON Episode
instance ToJSON Episode          -- Warning here
instance FromJSON Main.Series
instance ToJSON Main.Series      -- Warning here

问题是我得到了这两个警告:

src\Main.hs:21:10: Warning:
    No explicit implementation for
      `toJSON'
    In the instance declaration for `ToJSON Episode'

src\Main.hs:22:10: Warning:
    No explicit implementation for
      `toJSON'
    In the instance declaration for `ToJSON Main.Series'

我无法弄清楚为什么会这样。

修改

GHC版本:7.10.2

aeson版本:0.10.0.0(最新)

1 个答案:

答案 0 :(得分:5)

我可以通过这样做解决警告:

instance FromJSON Episode
instance ToJSON Episode where
  toJSON = genericToJSON defaultOptions
instance FromJSON Main.Series
instance ToJSON Main.Series where
  toJSON = genericToJSON defaultOptions

我仍然不知道为什么会有警告,但我看到已经有错误报告on github