matthias krull
matthias krull

Reputation: 4429

Why is the existing FromJSON instance for (Map String v) not used on (Map String String)?

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?

Upvotes: 1

Views: 489

Answers (1)

Waldheinz
Waldheinz

Reputation: 10487

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.

Upvotes: 1

Related Questions