[Haskell-cafe] serialize an unknown type

Iustin Pop iusty at k1024.org
Sun Oct 21 20:00:51 CEST 2012

On Sun, Oct 21, 2012 at 07:20:10PM +0200, Corentin Dupont wrote:
> Hi,
> Sorry if it was not enough explicit.
> I want to write functions like this:
> serialize :: (Show a) => Event a -> IO ()
> deserialize :: (Read a) => IO () -> Event a
> The functions would write and read the data in a file, storing/retrieving
> also the type "a" I suppose...

Can't you simply, when defining the type event, add a "deriving (Show,
Read)"? Then the standard (but slow) read/show serialisation would work
for you.

If you're asking to de-serialise an unknown type (i.e. you don't know
what type it should restore a-priori, but you want to do that based on
the contents of the file), things become a little more complex. Unless
you can further restrict the type 'a', really complex.

Maybe stating your actual problem, rather than the implementation
question, would be better?


More information about the Haskell-Cafe mailing list