Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize instance for Data.Text?

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.)

like image 935
rcreswick Avatar asked Oct 14 '13 17:10

rcreswick


1 Answers

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.

like image 159
rcreswick Avatar answered Oct 09 '22 04:10

rcreswick