I need to serialize a data type to disk that uses Data.Text
, here's an example:
{-# LANGUAGE DeriveGeneric #-}
import Data.Serialize (Serialize)
import Data.Text (Text)
import GHC.Generics
data Foo = Foo Text deriving (Read, Show, Eq, Ord, Generic)
instance Serialize Foo
-- instance Serialize Text
As written, this generates the error:
No instance for (Serialize Text)
arising from a use of `Data.Serialize.$gdmput'
Possible fix: add an instance declaration for (Serialize Text)
In the expression: (Data.Serialize.$gdmput)
In an equation for `put': put = (Data.Serialize.$gdmput)
In the instance declaration for `Serialize Foo'
If I uncomment the instance Serialize Text
line, then this more cryptic error comes about:
No instance for (Data.Serialize.GSerialize (Rep Text))
arising from a use of `Data.Serialize.$gdmput'
Possible fix:
add an instance declaration for
(Data.Serialize.GSerialize (Rep Text))
In the expression: (Data.Serialize.$gdmput)
In an equation for `put': put = (Data.Serialize.$gdmput)
In the instance declaration for `Serialize Text'
I could implement the Serialize
instance manually, but this seems like a situation where orphaned instances would be a real problem, and furthermore, I don't think I know enough about Data.Text
to serialize / deserialize it quickly and correctly.
Is there a standard solution to this problem? (I'm also not wedded to using cereal's Serialize instance, but I have been having some version issues relating to using the binary package; binary-0.5.1.1 doesn't seem to support Generics well, and I'd like to avoid writing boilerplate.)
Here's the instance I ended up using, based on Joachim Britner's advice:
instance Serialize Text where
put txt = put $ encodeUtf8 txt
get = fmap decodeUtf8 get
As pointed out, you may need different encode / decode functions, but the structure should be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With