手动parseJSON函数

时间:2016-10-16 20:54:14

标签: json haskell aeson

我为我的Valuation类型创建了自己的parseJSON函数。不幸的是,我得到的编译错误是关于没有" Generic"估价的版本,我不知道该怎么做。我尽可能多地阅读和重读了许多aeson教程,但似乎没有人提到这一点。我已经将我的Valuation类型作为FromJSON类的一个实例,并提供了我自己的parseJSON函数实现,但由于某种原因,编译器似乎认为它是另一个parseJSON函数的实现,并且我不知道为什么或如何解决它。非常感谢任何帮助。

  

C:\用户\约翰\ GitHub的\ haskell_projects \学习\ SRC \ Lib.hs:17:10:   错误:       *没有实例(GHC.Generics.Generic Valuation)           使用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         在“FromJSON Valuation'

的实例声明中      

- 使用以下方法构建包学习-0.1.0.0:         C:\用户\约翰\应用程序数据\漫游\栈\ SETUP-EXE缓存\ x86_64的窗口\ SETUP-简单惊天动地-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"       进程退出代码:ExitFailure 1

以下是代码:

{-# 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}

0 个答案:

没有答案