문제

I'd like to store aeson Values usig acid-store. I've taken a minimal acid implementation and tried to naively switch the types over to Value. These are my calls to 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 and JSONStateStore are my own types. I get this error:

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))))
도움이 되었습니까?

해결책

Evidently you've reached a limit of what the deriveSafeCopy Template Haskell function can do for you.

You can solve the issue by providing the instances manually. SafeCopy API contains comprehensive docs on how to do that. For additional examples you can check out how the default instances are declared.

다른 팁

Here is my implementation for those who are still interested:

-- | 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top