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 for
parseJSON': 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}