我想用酸来存储aeson的Value类型

时间:2013-04-22 23:58:53

标签: haskell acid aeson

我想存储aeson值usig acid-store。我采用了最小的酸实现,并试图天真地将类型转换为Value。这些是我对deriveSafeCopy的调用:

$(deriveSafeCopy 0 'base ''Object)
$(deriveSafeCopy 0 'base ''Array)
$(deriveSafeCopy 0 'base ''Number)
$(deriveSafeCopy 0 'base ''Value)
$(deriveSafeCopy 0 'base ''JSONState)
$(deriveSafeCopy 0 'base ''JSONStateStore)

JSONState和JSONStateStore是我自己的类型。我收到这个错误:

Can't derive SafeCopy instance for: (Data.Aeson.Types.Internal.Object,TyConI (TySynD Data.Aeson.Types.Internal.Object [] (AppT (AppT (ConT Data.HashMap.Base.HashMap) (ConT Data.Text.Internal.Text)) (ConT Data.Aeson.Types.Internal.Value))))

2 个答案:

答案 0 :(得分:3)

显然,你已达到deriveSafeCopy模板Haskell功能可以为你做的限制。

您可以通过手动提供实例来解决问题。 SafeCopy API包含有关如何执行此操作的全面文档。有关其他示例,您可以查看how the default instances are declared

答案 1 :(得分:3)

以下是我对仍然感兴趣的人的实施:

-- | ACID

$(deriveSafeCopy 0 'base ''JSONStateStore)
$(deriveSafeCopy 0 'base ''JSONState)
$(deriveSafeCopy 0 'base ''Value)
$(deriveSafeCopy 0 'base ''Number)

-- | An instance of SafeCopy for the Array Value.
instance SafeCopy a => SafeCopy (V.Vector a) where
    getCopy = contain $ fmap V.fromList safeGet 
    putCopy = putCopy . V.toList

-- | An instance of SafeCopy for the Object Value.
instance (SafeCopy a, Eq a, Hashable a, SafeCopy b) => SafeCopy (H.HashMap a b) where
    getCopy = contain $ fmap H.fromList safeGet 
    putCopy = contain . safePut . H.toList