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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With