سؤال

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