2

Here's my code:

import Data.Aeson
import Control.Applicative
import Control.Monad
import Data.Text
import GHC.Generics
import qualified Data.ByteString.Lazy as B

data JSON' = 
    JSON' { 
        foo :: !Text,
        int :: Int
        } deriving (Show, Generic)

instance FromJSON JSON'
instance ToJSON JSON'

jsonFile :: FilePath
jsonFile = "test.json"

getJSON :: IO B.ByteString
getJSON = B.readFile jsonFile

main :: IO ()
main = do
    -- Get JSON data and decode it
    d <- (eitherDecode <$> getJSON) :: IO (Either String [JSON'])
    -- If d is Left, the JSON was malformed.
    -- In that case, we report the error.
    -- Otherwise, we perform the operation of
    -- our choice. In this case, just print it.
    case d of
        Left err -> putStrLn err
        Right ps -> print ps

test.json looks liket his:

-- test.json
{
    "foo": "bar",
    "int": 1
}

When I run this code I get this error:

Can't make a derived instance of ‘Generic JSON'’:
  You need DeriveGeneric to derive an instance for this class
In the data declaration for ‘JSON'’

So far the documentation for Aeson, like all documentation on Hackage, is not helpful at all. I have no idea what I'm doing wrong. So far it seems like I'm reading a file into a bytestring, transforming it into a "tree" like data structure, and then printing one leaf per node. My code is straight from this link

What am I doing wrong?

UPDATE

After adding the language extension declaration to the top of the file

{-# LANGUAGE DeriveGeneric #-}

I'm getting this error:

Error in $: expected [a], encountered Object

Not sure what this means.

2
  • Your edited in error is just telling you the decode expected a JSON list, so test.json would look like [ ... ] and not { ... }. The reason your decoder expected a list is your explicit type [JSON'] instead of just JSON', which would work. Commented Mar 2, 2016 at 17:55
  • You ask for [JSON'], but provide a json file with a value of type JSON'. Perhaps you wanted more square brackets in your json file or fewer in your Haskell file. Commented Mar 2, 2016 at 17:55

2 Answers 2

3

you need to put {-# LANGUAGE DeriveGeneric #-} on the top of your file.

On a side note the documentation of aeson says

instance ToJSON Person where
    -- ...
    toEncoding = genericToEncoding defaultOptions

instance FromJSON Person
    -- No need to provide a parseJSON implementation.

I think you probably also forgot the toEncoding line in your ToJSON.

Sign up to request clarification or add additional context in comments.

3 Comments

This link is able to read the JSON without adding a toEncoding function
Event after adding the extension declaration Im getting an error. Please see edits.
Every example using deriving (Generic) in the Data.Aeson documentation includes this language pragma. @dopatraman What additional info should be present in the "not helpful at all" documentation to help here?
1

Add this to the top of the file

{-# LANGUAGE DeriveGeneric #-}

The ability to use derive (Generic) is a language extension, and you have to tell GHC that you want this to be turned on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.