XML patterns

Lex Spoon lex at cc.gatech.edu
Mon Jul 4 00:30:07 UTC 2005


> I have an object that I want to serialize in XML now this object  
> contains a date object
> of course I can save it explicitly as a string and do a readFrom....
> But I wanted to know if there is a more advance serialization process.

My usual pattern for this is to put xmlForExport methods on every class
I want to be able to save, and then have decodeXML: methods on the class
side of these same classes.  Finally, I add a master decodeXML: method
somewhere in my system that looks at the tag and then calls decodeXML:
on the proper class.  At first, the master decodeXML: method has a long
list of ifTrue:'s in it to select the class to restore, but if the
system lasts long enough I'll end up making a Dictionary that maps XML
tag names into class objects.

Actually, usually I don't use XML per se if I am just doing Squeak work.
 I convert to trees of arrays, analagous to s-expressions, and I don't
convert small objects such as integers, floats, and points -- classes
that are extremely unlikely to change for the duration of time that my
external files should last.  I then export and import the array trees
using SmartRefStream.  This is Squeak-specific, but it is a little more
convenient and it does not cause problems with cross-version
compatibility.


There is a meme going around that you should use an automatic serializer
for this kind of thing, but if you do so then in practice you sacrifice
cross-version compatibility.  That is, if you use an automatic
serializer -- be it SIXX, SmartRefStream, or ImageSegment -- then files
you create today will probably not be loadable after your software has
evolved for several months.  The problem is that you will tend to
refactor your code as it evolves, creating new classes and eradicating
old ones, and thus the serialized files will specify classes that no
longer exist.


There is substantial room for improvement in this pattern, but it is
somewhere to start.  I yearn for the day that serialization is treated
more seriously, the day when software folks lose their fanatacism with
low-level file formats (XML) and silver bullets (automatic
serialization).


-Lex



More information about the Squeak-dev mailing list