Tech Walkthrough Pt. 1: Object Serialization

As promised for all my developers out there, the 1st part of a 5 part tech walkthrough.  I am no expert, just sharing information.  If anything stands out as incorrect, please leave a comment and we can have a discussion.

Object serialization is a method of persisting (ie. saving) objects to files.  We desire to keep these objects in their current form even after the program has been closed/exited/terminated.  Object serialization is a very easy to way to this.  The closest non-technical example of this I can come up with is setting up a drum set.  You desire to play the drums one day.  Let’s say you have an area where you can set up drums for the long term in your house.  Well, in this instance, you just set them up once (we “serialize” the drum set).  You can then use them whenever you like (opening the program and deserializing the drumset).  When you are done, you don’t put the drumset up, you just leave it setup in its current condition (serialize) so you don’t have to set it up again.

This is exactly how Speeed Reader caching works.  I create some custom classes in Speeed Reader that hold information for each tag, subscription, and article.  I am able to save each subscription to a file and reload it when necessary (ex. starting Speeed Reader in offline mode).  It was reasonably simple transition to object serialization to persist article data between Speeed Reader program executions. 

Let’s get into an example.  This code is written in C# .NET.  There are some imports missing so I could keep the code concise, but those should be easy to find with a quick Google search.  Here is my simplified article object.

public class Article
{
 public string title = "";
 public string content = "";
}

We have our very simple article class with a title and contents (would be the text of the article).  Let’s define our serialization (ie. saving the class to a file) and deserialization (ie. loading the class from a file) functions.

public void SerializeArticle(Article article, string file_name)
{
 XmlSerializer serializer = null; //serializer object which takes care of serializing/deserialzing our object
 try
 {
  using (TextWriter writer = new StreamWriter(file_name)) //we use a TextWriter to write the contents of the serialized class to disk
  {
   serializer = new XmlSerializer(typeof(Article)); //create a serializer to serialize objects of the Article type
   serializer.Serialize(writer, article); //serializes the object
  }
 }
 catch (Exception e)
 {
 }
}

public Article DeserializeArticle(string file_name)
{
 Article ret_val = null; //our return value, the article we are loading from memory
 XmlSerializer serializer = null; //serializer object which takes care of serializing/deserialzing our object
 try
 {
  using (TextReader reader = new StreamReader(file_name)) //use a TextReader to read the file contents
  {
   serializer = new XmlSerializer(typeof(Article)); //create a serializer to read in objects of the Article type
   ret_val = (Article)serializer.Deserialize(reader); //deserialize the object
  }
 }
 catch (Exception e)
 {
 }
 return ret_val;
}

I hope its apparent how one could create a function to serialize and deserialize objects. I commented the example code. I also want to point out that since I use a XmlSerializer, my output of the serialized article will be an XML formatted file. There are other serializer types available in the .NET framework, such as the binary serializer which serializes objects to bytes instead of readable XML. Finally, a quick demonstration.

public static void main()
{
 Article sample_article = new Article();
 sample_article.title = "Just a quick sample";
 sample_article.content = "This is just a quick sample to demonstrate article serialization";
 SerializeArticle(sample_article, "sample_article.xml");
 Article deserialized_article = DeserializeArticle("sample_article.xml");
}

The contents of sample_article.xml would look like.  I had to upload it, WordPress was acting funny with embedded XML.  Download

That about sums it up, let me know if you have any questions.


 

New Volkswagen Jetta?

Picture of the Volkswagen Compact Coupe concept at the Detroit Auto Show.

Now I am a big fan of Volkswagen.  My last three cars: ’98 Jetta, ’95 Golf GTI VR6,  ’06 VW GLI DSG (current), and this may as well be my next car.  All I need is a V6 + AWD and I’m sold.

Has a very “Audi” look to it.

Technology Walkthrough Series Intro

Over the next week or two I will be posting some very informative items regarding the technologies that make Speeed Reader work.  It may or may not be knowledge you already possess depending on your experience in software engineering, however, I feel it may be beneficial for me and for others looking for this information.  Posts will include some snippets (generalized) from actual Speeed Reader code.  I am thinking about the following topics:

  • object serialization
  • http web communication
  • multi-threading
  • encryption
  • C#/C++ integration

I’m also looking for ideas for my next project.  A Twitter client was suggested and a media player would be a good idea.