【问题标题】:Deriving Aeson type classes with two different possible types in the same field在同一字段中派生具有两种不同可能类型的 Aeson 类型类
【发布时间】:2021-08-05 08:29:20
【问题描述】:

我有一个 API,它以以下形式返回 JSON 结果:

{
  "data": [1, 2, 3]
}

data 字段可以是两条不同记录的编码,如下所示:

newtype ResultsTypeA = ResultsTypeA [ResultTypeA]
newtype ResultsTypeB = ResultsTypeB [ResultTypeB]

当我从 Haskell 查询这个 API 时,我提前知道我是在处理 ResultsTypeA 还是 ResultsTypeB,因为我在查询中明确要求它。

我苦苦挣扎的部分是 Aeson ToJSON 和 FromJSON 实例。由于A 和B 两种结果类型最终都是Int 的列表,我不能在FromJSON 中使用模式匹配器,因为在这两种情况下我只能匹配[Int]。

这就是为什么我想到做以下事情:

newType ApiResponse a =
    ApiResponse {
        data :: a
    }

newtype ResultsTypeA = ResultsTypeA [ResultTypeA]
newtype ResultsTypeB = ResultsTypeB [ResultTypeB]

但是我不知道如何为上述编写 ToJSON 和 FromJSON 实例,因为现在 ApiResponse 有一个类型参数,而在 Aeson 文档中似乎没有一个地方可以放置它解释了如何通过涉及类型参数来派生这些实例。

另一种选择,避免使用类型参数,如下所示:

newtype Results =
    ResultsTypeA [ResultTypeA]
  | ResultsTypeB [ResultTypeB]

newtype ApiResponse =
    ApiResponse {
        data :: Results
    }

在这种情况下,ToJSON 很简单:

instance ToJSON ApiResponse where
    toJSON = genericToJSON $ defaultOptions

但是FromJSON 让我们回到了无法在结果类型A 和B 之间做出决定的问题...

也有可能我完全做错了,还有第三个选项我看不到。

  • FromJSON / ToJSON 实例在 ApiResponse 上带有类型参数会是什么样子?
  • 有没有更好的替代方案来解决这个问题?

【问题讨论】:

  • 我不相信将任何 Haskell 类型用于此专用目的 根本。有时最好只在接口处使用Values,然后将相关数据提取到不需要以任何方式近似 JSON 结构的 Haskell 类型中。
  • 我认为你应该能够为参数化类型派生 FromJSON 和 ToJSON 就好了。生成的实例将采用FromJSON a => FromJSON (ApiResponse a) 的形式。您还应该为ResultsTypeA、ResultTypeA 等派生实例。 ResultTypeA 的那些应该搭载Int 的底层实例。这可以通过GeneralizedNewtypeDeriving 和DerivingStrategies 来完成:deriving newtype (FromJSON, ToJSON)
  • 帧挑战:只使用[Int] 有什么问题? 特别是“当我从 Haskell 查询这个 API 时,我提前知道我是在处理 ResultsTypeA 还是 ResultsTypeB,因为我在查询中明确要求它。” .如果您绝对必须有两种不同的类型,则查询器可以应用适当的 newtype 构造函数,但即便如此,在大多数情况下,也不需要。

标签: haskell type-parameter aeson to-json fromjson


【解决方案1】:

由于结果类型 A 和 B 最终都是 Int 列表,我不能在 FromJSON 中使用模式匹配器,因为在这两种情况下我只能匹配 [Int]。

如果您有参数化类型,并且您正在手动编写FromJSON 实例,则可以设置参数本身必须具有FromJSON 实例的前提条件。

然后,当您编写解析器时,您可以将类型参数的解析器用作定义的一部分。像这样:

{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson

data ApiResponse a =
    ApiResponse {
        _data :: a,
        other :: Bool
    } 

instance FromJSON a => FromJSON (ApiResponse a) where
    parseJSON = withObject "" $ \o -> 
          ApiResponse <$> o .: "data" -- we are using the parameter's FromJSON 
                      <*> o .: "other"

现在,让我们使用GeneralizedNewtypeDeriving 定义两个新类型,它们从Int 借用它们各自的FromJSON 实例:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
-- Make the instances for the newtypes exactly equal to that of Int
newtype ResultTypeA = ResultTypeA Int deriving newtype FromJSON
newtype ResultTypeB = ResultTypeB Int deriving newtype FromJSON

如果我们在 ghci 中加载文件,我们可以将类型参数提供给 ApiResponse 和 interrogate the available instances:

ghci> :instances ApiResponse [ResultTypeA]
instance FromJSON (ApiResponse [ResultTypeA])

如果您还派生了Generic,您还可以为ApiResponse 自动派生FromJSON:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
import Data.Aeson
import GHC.Generics

data ApiResponse a =
    ApiResponse {
        _data :: a,
        other :: Bool
    } 
    deriving stock Generic
    deriving anyclass FromJSON

deriving stock Generic 使 GHC 生成数据类型结构的表示,该结构可用于派生其他类型类的实现——此处为 FromJSON。对于那些通过Generic 机制进行的推导,他们需要使用anyclass 方法。

生成的实例将采用FromJSON a =&gt; FromJSON (ApiResponse a) 的形式,就像手写的一样。我们可以在 ghci 中再次查看:

ghci> :set -XPartialTypeSignatures
ghci> :set -Wno-partial-type-signatures
ghci> :instances ApiResponse _
instance FromJSON w => FromJSON (ApiResponse w)
instance Generic (ApiResponse w) 

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2015-10-28
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多