[Hs-Generics] xml deserialization using generics?
Hugh Perkins
hughperkins at gmail.com
Tue Jun 26 15:39:49 EDT 2007
Cant decide whether this list is appropriate for questions related to
generics usage, or only to generics design? So let me know if I should use
haskell at haskell.org instead, but on the other hand I figure you guys are the
experts :-)
Trying to write an xmlserialization function for arbitrary (Data) objects,
and not requiring a DTD or a code generation step.
It's possible in C# so it really ought to be possible in Haskell ;-)
xml serialization is pretty easy, see section 9.1.1 of
http://www.haskell.org/haskellwiki/HXT
On the other hand deserialization is still a work in progress. At least I'm
not smart enough to figure it out, and nothing comes up in Google.
The xml parsing bit is done, see section 9.1.2 of
http://www.haskell.org/haskellwiki/HXT
So, we can get a list of pairs/triples etc containing the data of our
choice, such as field names, field values (in string format), data types,
constructors, etc.
What's remaining is to take makeConstrM, our final data type, and the list
of field values, and to create the final object.
Here's my non-working attempt at this bit so far. Most of the working bits
come from a demonstration by kpreid in irc yesterday night of using
makeConstrM with a pair of strings. The rest of the code is my feeble
attempt to get this working for the Config custom data type.
runM' :: (MonadState [String] m, Monad m, Data a) => m a
runM' = do
value <- gets head
modify tail
-- then one of: (pick the non-working function of your choice ;-) :
-- return read (fromJust value)
-- return (fromJust $ cast value )
-- return (fst $ head $ gread( "(" ++ value ++ ")" ) )
-- return (fromConstrM runM' constr)
-- return (fromConstr contr)
testConstrM' :: (Read a, Data a, Read c, Read b, Data b, Data c) => [String]
-> a -> (b,c)
testConstrM' fieldvalues object = evalState( fromConstrM runM' (toConstr
object) ) fieldvalues
data Config = Config{ name :: String, age :: Int }
deriving( Data, Show, Typeable, Ord, Eq, Read )
createConfig = Config "blah" 3
test = testConstrM' ["qsdfqsdf", "7"] createConfig
Note that whilst I'm a total Haskell newbie (this is week 2), I've used
Reflection extensively in other languages, eg wrote a fast async rpc layer
over guaranteed udp for C#
http://metaverse.svn.sourceforge.net/viewvc/metaverse/Trunk/Source/Metaverse.Networking/,
so I know more or less what I'm aiming for, just not exactly how to do
it
;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/generics/attachments/20070626/5e24bb8a/attachment.htm
More information about the Generics
mailing list