输入别名和“实例......在哪里”

时间:2015-07-08 14:19:59

标签: haskell compiler-errors typeclass

我有一个类型别名:

type MyId = UUID

数据类型:

data MyData = MyData { myId :: MyId, .... }

然后我为FromJSON创建了ToJSONMyData的实例

instance FromJSON MyData where
  parseJSON (Object v) = ....

instance ToJSON MyData where
  toJSON (Object v) = ....

我想知道,为什么我有错误:

1) No instance for (`FromJSON MyId`) arising from a use of ‘.:’
2) No instance for (ToJSON MyId) arising from a use of ‘.=’

instance FromJSONToJSON UUID已经实施,MyId是否已自动拥有它们?如果没有,我将如何实施它们?

instance FromJSON MyId where
  parseJSON (Object v) = --- ? It's already UUID for which FromJSON is implemented

instance ToJSON MyId where
  toJSON (Object v) = --- ? It's already UUID for which FromJSON is implemented

1 个答案:

答案 0 :(得分:4)

您是否导入Data.UUID.Aeson(来自uuid-aeson包)?

这对我有用:

import Data.UUID
import Data.UUID.Aeson
import Data.Aeson

type MyUUID = UUID

data U2 = U2 MyUUID Int

instance ToJSON U2 where
  toJSON(U2 a b) = toJSON a