如何为具有依赖映射的类型派生 JSON 个实例?
How to derive JSON instances for a type with dependent-map?
我使用 dependent-map
to create a map of GADT values. My goal is to derive JSON instances for them. The aeson-gadt-th
库可以为 GADT 本身派生 JSON 个实例(下面的 P
类型);然而,自动派生 GADT 值映射(下面的 DMap P Identity
类型)失败,因为缺少此类型的 Generic
实例:• No instance for (Generic (DMap P Identity))
。
如何为这种类型生成 Generic
?
import Data.Dependent.Map
data P a where
P_Title :: P Text
deriveGEq ''P
deriveGCompare ''P
deriveJSONGADT ''P
deriving instance ToJSON (DMap P Identity)
编辑: 根据下面 Cale 的回答使用 dependent-sum-aeson-orphans
库后:
import Data.Dependent.Sum.Orphans ()
data MyType = MyType (DMap P Identity)
deriving Generic
deriving instance ToJSON MyType
我看到一个不同的错误:
• Could not deduce: Data.Constraint.Extras.ConstraintsFor'
P ToJSON Identity
如果 DMap P Identity
已经有 JSON 个实例(根据孤儿导入),为什么简单类型包装器没有?
编辑 2:好的,在对引用库的代码和示例进行一些探索后,我意识到我需要派生 ArgDict
,之后一切正常!
import Data.Constraint.Extras.TH
deriveArgDict ''P
这里有一个库,其中包含用于 DSum 和 DMap 的 ToJSON 和 FromJSON 实例。
https://github.com/obsidiansystems/dependent-sum-aeson-orphans
一定要看看它们是如何工作的,这是 constraints-extras 可以做什么的一个很好的例子。
我使用 dependent-map
to create a map of GADT values. My goal is to derive JSON instances for them. The aeson-gadt-th
库可以为 GADT 本身派生 JSON 个实例(下面的 P
类型);然而,自动派生 GADT 值映射(下面的 DMap P Identity
类型)失败,因为缺少此类型的 Generic
实例:• No instance for (Generic (DMap P Identity))
。
如何为这种类型生成 Generic
?
import Data.Dependent.Map
data P a where
P_Title :: P Text
deriveGEq ''P
deriveGCompare ''P
deriveJSONGADT ''P
deriving instance ToJSON (DMap P Identity)
编辑: 根据下面 Cale 的回答使用 dependent-sum-aeson-orphans
库后:
import Data.Dependent.Sum.Orphans ()
data MyType = MyType (DMap P Identity)
deriving Generic
deriving instance ToJSON MyType
我看到一个不同的错误:
• Could not deduce: Data.Constraint.Extras.ConstraintsFor'
P ToJSON Identity
如果 DMap P Identity
已经有 JSON 个实例(根据孤儿导入),为什么简单类型包装器没有?
编辑 2:好的,在对引用库的代码和示例进行一些探索后,我意识到我需要派生 ArgDict
,之后一切正常!
import Data.Constraint.Extras.TH
deriveArgDict ''P
这里有一个库,其中包含用于 DSum 和 DMap 的 ToJSON 和 FromJSON 实例。
https://github.com/obsidiansystems/dependent-sum-aeson-orphans
一定要看看它们是如何工作的,这是 constraints-extras 可以做什么的一个很好的例子。