[Haskell-cafe] JSON serialization/deserialization

ntupel at googlemail.com ntupel at googlemail.com
Fri Sep 12 10:31:26 EDT 2008


Dear list members,

I try to use Text.JSON
(http://hackage.haskell.org/cgi-bin/hackage-scripts/package/json) to
serialize and deserialize record types. As I understand it, the data
types need to be instances of class JSON. However I have difficulties
to come up with a nice implementation of showJSON and readJSON. For
example:


module Test where

import Text.JSON

data Record =
    One {
        field1 :: String,
        field2 :: String }
  | Two {
        field :: String } deriving (Eq, Ord, Show, Read)

showJSON (One x y) = toJSObject [("field1", x), ("field2", y)]
showJSON (Two x)   = toJSObject [("field", x)]

readJSON x = -- ???

This lacks the instance declaration itself but my problem is more
fundamental. How can I write the readJSON method? In general how would
one approach the problem of serialization/deserialization? I am
puzzled that the type classes Show and Read can be derived
automatically. I can not think of any way to do this.

Thanks for your help!


More information about the Haskell-Cafe mailing list