user4201880
user4201880

Reputation:

Haskell JSON Issue

been trying to get this code working but the compiler is throwing out an error?

 {-# LANGUAGE OverloadedStrings, DeriveGeneric #-}

 import Data.Aeson
 import Data.Text
 import Control.Applicative
 import Control.Monad
 import qualified Data.ByteString.Lazy as B
 import Network.HTTP.Conduit (simpleHttp)
 import GHC.Generics


data Temperatures =
Temperatures { date  :: String
     , temperature   :: Int
     } deriving (Show,Generic)


instance FromJSON Temperatures
instance ToJSON Temperatures

jsonURL :: String
jsonURL = "A JSON URL"

getJSON :: IO B.ByteString
getJSON = simpleHttp jsonURL

main :: IO ()
main = do
d <- (eitherDecode <$> getJSON) :: IO (Either String [Temperatures])
case d of
Left err -> putStrLn err
Right ps -> print ps

And the error message i'm getting is

Main.hs:25:11:
Couldn't match type `bytestring-      0.10.0.2:Data.ByteString.Lazy.Internal.ByteString'
              with `B.ByteString'
Expected type: IO B.ByteString
  Actual type: IO
                 bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString
In the return type of a call of `simpleHttp'
In the expression: simpleHttp jsonURL
In an equation for `getJSON': getJSON = simpleHttp jsonURL

Any suggestions of what's causing this error and how to fix it would be much appreciated. Thanks.

Upvotes: 2

Views: 149

Answers (2)

Beerend Lauwers
Beerend Lauwers

Reputation: 922

Ah. This is known as Cabal Hell. Fixing it is an active area of research in the Haskell community. My suggestions would be as follows:

  • If you're using Windows:

    • Install MinGHC
    • Install Stackage (Stable Hackage). You can do this globally or on a per-project basis. Here's the global way:
    • Download the global cabal.config file.
    • Now open your global cabal config file. Mine was in C:\Users\blauwers\AppData\Roaming\cabal and was just called config.
    • Copy and paste the contents of the file your downloaded to the end of that file.
    • Your global cabal environment will now try to get packages from Stackage.
    • For extra safety, comment out the other lines in the file that start with remote-repo. In my case that was only remote-repo: hackage.haskell.org:http://hackage.haskell.org/packages/archive.
    • With the extra safety enabled, you'll have to manually download and install packages that are not on Stackage.
  • If you're using Linux: I don't know where the global cabal config file is, but the process is the same.

Upvotes: 0

danplubell
danplubell

Reputation: 131

It looks like there are multiple versions of the bytestring package installed. You can use the following command to find the bytestring packages:

ghc-pkg list bytestring

If there are more than one installed then the following could solve the problem:

ghc-pkg unregister

Or,

build with ghc --make --hide-package

Upvotes: 2

Related Questions