我有一个类型别名:
type MyId = UUID
数据类型:
data MyData = MyData { myId :: MyId, .... }
然后我为FromJSON
创建了ToJSON
和MyData
的实例
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 FromJSON
和ToJSON
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
答案 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