Question

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))))
Était-ce utile?

La solution

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.

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top