Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

aeson

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?

like image 694
Electric Coffee Avatar asked Feb 24 '14 13:02

Electric Coffee


1 Answers

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.

like image 137
fjh Avatar answered Oct 07 '22 07:10

fjh