
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!

The trouble: module A data ABC = A { a :: String } | B { a :: String } | C { a :: String } module B data ABC = A { a :: String } | B { a :: String } | C { a :: String } is valid haskell. so how should you know having the serialized String { a : "abc" } wether to use the constructor A,B,C ? or wether to use ABC from module A or from B? There are many ways to solve this: One is to serialize it this way: { "datatype": "A.ABC", constructor : "C" , a :"text" } or such.. Maybe also read about Data.Typable, have a look at derive or DrIft (or the lib itself, I'm not sure wethere there is one which has choosen one way to do this).. Basically JavaScript doesn't have real typing (such as classes). But there exist some libraries to emulate it such as mootools or scriptaculous.. I hope you'll find a solution which works for you. Sincerly Marc Weber
participants (2)
-
Marc Weber
-
ntupel@googlemail.com