对于这个使用 Aeson 的示例程序:
module Main where
import Data.Maybe
import Data.Aeson
import Data.Map as Map
import Data.Functor
import qualified Data.ByteString.Lazy as LBS
main = do
jsonContent <- LBS.readFile "templates/test.json"
print (decode jsonContent :: Maybe TemplateConfig)
newtype TemplateConfig = TemplateConfig (Map String String)
deriving Show
instance FromJSON TemplateConfig where
parseJSON val = TemplateConfig <$> parseJSON val
我收到一个错误,抱怨缺少实例:
$ ghc test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
test.hs:17:45:
No instance for (FromJSON (Map String String))
arising from a use of `parseJSON'
Possible fix:
add an instance declaration for (FromJSON (Map String String))
In the second argument of `(<$>)', namely `parseJSON val'
In the expression: TemplateConfig <$> parseJSON val
In an equation for `parseJSON':
parseJSON val = TemplateConfig <$> parseJSON val
我知道我需要一个 FromJSON 实例来解析 JSON,并且 Aeson 中还包含很多常用的实例。根据文档,有一个实例FromJSON v => FromJSON (Map String v)
,我认为应该在这种情况下使用它。
我错过了什么?