Modern programming

I’m having a geek moment.  I just learned to use .Net serialization.  I am absolutely amazed at the point to which modern programming languages have progressed.   It is so interesting and powerful that I must talk about it- even if many of you reading already know what it is.

Imagine you have a complicated data structure.  You have an object that contains many other objects, and then the first object belongs to a list.  For example, in my Eye Tracker Analysis tool I have an object “AOI” (area of interest) that contains various other data structures such as a string “Name,” an array of integers to determine where it is the screen “origin”, a list of integers for “control points” to control its shape and so forth.  Then, I have a list of these AOI structures to use when defining more than one area of interest.

Once created, I need my program to save these areas of interest to a file so that I can later re-load them without having to manually create them again.   In the old days, I would have had to write a routine that manually exported each element in an AOI to a file, and then do the opposite to read them back in.  It would have been a lot of really tedious and un-interesting coding.

Enter XLM serialization. 

To save my list of AOIs, all that was required was to use the XmlSerializer class, and create one that serializes my AOI datatype.  That was really hard…

XmlSerializer serialfile = .XmlSerializer(typeof(AOI_to_Serialize) );

…where (AOI_to_Serialize) is my name for a class that holds the list of my AOI structures.

Then, open a file.

TextWriter tofile = new StreamWriter(filename);

“serialize” your datastructure to that file.

    serialfile.Serialize(tofile,aoi_to_serialize);

And close the file.

        tofile.Close();

You get your data structure outputted to an XML file.  Then, if you want to read that saved structure back into the program, it’s a process parallel to the one just described, but you’ll call  .DeSerialize instead. 

Tons and tons of code reduced to a few lines of .Net wonder.

Advertisements
This entry was posted in Programming in general. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s