Couldn't match expected type 'Data.ByteString.Lazy.Internal.ByteString' with actual type '[Char]'

StackOverflow https://stackoverflow.com/questions/21988831

  •  15-10-2022
  •  | 
  •  

Question

I'm trying to get a simple Json parser up and running in my Haskell code, I came across Data.Aeson which seemed like a viable solution to my problem

I followed the example code on the page, and with some minor modifications, here's what I got:

{-#LANGUAGE OverloadedStrings #-}

import Data.Aeson
import Data.Text
import Control.Applicative
import Control.Monad

data Person = 
    Person { firstName :: Text
           , lastName  :: Text
           , age       :: Int
           } deriving Show

instance FromJSON Person where
    parseJSON (Object v) =
        Person <$> v .: "f_name"
               <*> v .: "l_name"
               <*> v .: "age"
    parseJSON _ = mzero

Running the following in GHCi causes the nasty message in the title to appear:

decode "{\"f_name\":\"Haskell\", \"l_name\":\"Curry\",\"age\":114}" :: Maybe Person

So, does anyone here have an idea what went wrong? I followed the example code almost exactly as it was written, so why is it that it fails?

Was it helpful?

Solution

Before calling decode in ghci, you need to do :set -XOverloadedStrings, so the string literal is treated as a ByteString instead of a String. The pragma in the module only applies to the code in the module, not to what you do in ghci.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top