1
votes

For this example programm using 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

i get an error complaining about a missing instance:

$ 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

I understand I need a FromJSON instance to parse JSON and there are also a lot of commonly used instances included in Aeson. According to the documentation there is an instance FromJSON v => FromJSON (Map String v) and I thought it should get used in this case.

What am I missing?

1
Thats works fine on my systemSatvik
Hm. Someone on #haskell also has this code working. I am using aeson-0.6.2.1 on ghc 7.4.2. I guess I will try a clean setup of cabal and check back.matthias krull
It's not working for me on GHC 7.6.3 with aeson 0.6.1.0.bheklilr
With moving my .cabal and .ghc directories and doing a fresh install of aeson it also works for me now. I still do not see why it did not with my other environment.matthias krull

1 Answers

1
votes

There has been an FromJSON v => FromJSON (Map String v) instance in aeson since version 0.2, which was released in February 2011. This was missing in version 0.1. So I guess you have an old version of aeson installed, which probably got pulled in as a dependency with upper bounds.