0

I've created my own parseJSON function for my Valuation type. Unfortunately, I get compile errors about there not being a "Generic" version of Valuation and I'm not sure what to make of it. I've read and re-read as many aeson tutorials as I can and none of them seem to mention this. I've made my Valuation type an instance of the FromJSON class and provided my own implemention of the parseJSON function, but for some reason, the compiler seems to think that it's an implementation of the other parseJSON function, and I don't know why or how to fix it. Any help would be very much appreciated.

C:\Users\John\GitHub\haskell_projects\learning\src\Lib.hs:17:10: error: * No instance for (GHC.Generics.Generic Valuation) arising from a use of aeson-0.11.2.1:Data.Aeson.Types.Class.$dmparseJSON' * In the expression: aeson-0.11.2.1:Data.Aeson.Types.Class.$dmparseJSON In an equation forparseJSON': parseJSON = aeson-0.11.2.1:Data.Aeson.Types.Class.$dmparseJSON In the instance declaration for `FromJSON Valuation'

-- While building package learning-0.1.0.0 using: C:\Users\John\AppData\Roaming\stack\setup-exe-cache\x86_64-windows\setup-Simple-Cabal-1.24.0.0-ghc-8.0.1.exe --builddir=.stack-work\dist\b7fec021 build lib:learning exe:learning-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1

Here's the code:

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{- The DuplicateRecordFields language extension allows records to use the same name for field labels. Without it, all the records in this module would need to have unique names for all their fields.
-}
module Lib
    ( someFunc,
    ) where
import Data.Time.Calendar

import Control.Lens
import Data.Aeson.Lens (_String, _Object, key)
import Network.Wreq
import Data.Aeson (Value(..), FromJSON, (.:), (.=), withObject)

data Valuation = Valuation {valued_on :: Day, price :: Double}

instance FromJSON Valuation where
parseJSON = withObject "valuation" $ \o -> do
    query <- o .: "query"
    results <- query .: "results"
    quote <- results .: "quote"
    price <- quote .: "Open"
    return Valuation{valued_on=today, price=price}
torrlane
  • 23
  • 1
  • Is there a chance that you didn't indent `parseJSON` and its body further than the line with `instance FromJSON Valuation where`? If so, the error message would make sense since the default signature of `parseJSON` relies on `Generic`... – Alec Oct 16 '16 at 20:58
  • 1
    I double-checked and my hunch appears to be right: you just need to indent `parseJSON` and its body further in. Also, you need to import the methods of `FromJSON` too by adding a `(..)` after the `FromJSON` in the import statement. Finally, you have a problem because there is no such function as `today :: Day`. – Alec Oct 16 '16 at 21:25
  • Wow. Thankyou so much for this. You were absolutely right. I indented the parseJSON function and that sorted the compile error. I forgot to paste the today function in. – torrlane Oct 18 '16 at 20:31
  • 2
    As a helper for others treading the same path. I also needed to change the FromJSON import to: import Data.Aeson (Value(..), FromJSON(..), (.:), (.=), withObject) Without that change, I get the following error: `parseJSON' is not a (visible) method of class `FromJSON' – torrlane Oct 18 '16 at 20:32
  • Well thanks @torrlane - I needed apparently `ToJSON (..)` – voneiden Oct 12 '21 at 18:32

0 Answers0