AIR Tip 8 – Serializing Objects

AIR Tip 8: This tutorial will walk you through the process of serializing Actionscript 3 classes. There are several uses for this in AIR such as custom clipboard data types and the encrypted local store. By understanding the serialization process, you can master the preservation of class instances.

For this example we will convert a simple value object class (in Code Example 1) into a class that can be serialized. This basic class will be a value object for an application that stores information about Flex developers. It will have three properties (as you can see below): lastName which will be a String, firstName which will be a String, and skillLevel which will be a uint. Assume (for the sake of the example) that we will be building multiple AIR applications that will use this class, and we will want to be able to paste this data (as an actual class instance) between these different AIR applications.

ACTIONSCRIPT:
  1. package vo {
  2.  
  3.     [Bindable] 
  4.     public class FlexDeveloper {
  5.  
  6.         public var lastName:String;
  7.         public var firstName:String;
  8.         public var skillLevel:uint;
  9.  
  10.         public function FlexDeveloper() {
  11.         }
  12.  
  13.     }
  14.  
  15. }

Code Example 1 - Basic Value Object Class

To clone clipboard data (which is the only way that multiple AIR applications can share custom clipboard data types), your data must be serializable. So, how is a class made serializable? For a class to be serializable, it must implement the interface IExternalizable [Reference]. Some classes within Flex (such as ArrayCollection) already implement this interface (and because of this, they are serializable). To implement the IExternalizable interface, your class must have two methods: writeExternal and readExternal. These classes serialize class data into a ByteArray and unserialize a ByteArray back into an instance of the class respectively.

The writeExternal method takes one argument: output. This argument implements IDataOutput [Reference]. For serialization this will be a ByteArray (just one of the classes that implements IDataOutput). In this method you need to take each property and write it into this output object. For this example only the writeUTF and writeUnsignedInt methods were needed. To see a full list of methods available - click here.

The readInternal method also accepts one argument: input. This argument implements IDataInput [Reference]. To unserialize this object, you only need to read the values back from the ByteArray. It is important here that you read the values back in the same order that you wrote them. To view a full list of the methods available to read values back - click here.

By adding these two methods and importing the needed interfaces (IDataInput,IDataOutput,IExternalizable) our class should be serializable.

ACTIONSCRIPT:
  1. package vo {
  2.  
  3.     import flash.utils.IDataInput;
  4.     import flash.utils.IDataOutput;
  5.     import flash.utils.IExternalizable;
  6.     import flash.net.registerClassAlias;
  7.  
  8.     [Bindable] 
  9.     public class FlexDeveloper implements IExternalizable {
  10.  
  11.         public var lastName:String;
  12.         public var firstName:String;
  13.         public var skillLevel:uint;
  14.  
  15.         public function FlexDeveloper() {
  16.             registerClassAlias(
  17.                 "vo.FlexDeveloper", FlexDeveloper );
  18.         }
  19.  
  20.         public function readExternal(input:IDataInput):void {
  21.             this.firstName = input.readUTF();
  22.             this.lastName = input.readUTF();
  23.             this.skillLevel = input.readUnsignedInt();
  24.         }
  25.  
  26.         public function writeExternal(output:IDataOutput):void {
  27.             output.writeUTF(this.firstName);
  28.             output.writeUTF(this.lastName);
  29.             output.writeUnsignedInt(this.skillLevel);
  30.         }
  31.  
  32.     }
  33.  
  34. }

Code Example 2 - Custom Class Implementing the IExternalizable Interface

Note: I received errors when I did not have the registerClassAlias method call. I am not sure of this is a bug or not. However, once I added it, the serialization process worked seamlessly.

In the next AIR tip I will demonstrate how this technique can be used with the Clipboard functionality inside of AIR.




4 Responses to “AIR Tip 8 – Serializing Objects”

  1. [...] an Application from the Browser (AIR Beta 3) AIR Tip 7: Using Command Line Arguments (AIR Beta 3) AIR Tip 8: Serializing Objects (AIR Beta 3) To assist you in your AIR Development, I also wanted to make available a list of [...]

  2. [...] AIR Tip 8: Serializing Objects (AIR 1.0) [...]

  3. air apps and techniques…

    I’ve been trawling the web for cool AIR apps and techniques for AIR development. Here’s what I’ve seen thus far.
    David Tucker has ten handy AIR dev tips (like this one for serialising objects).
    There’s a “top 100 music vid…

  4. Toby says:

    Absolutely the best description on how to do this, and I’ve spent a lot of today looking for info on it. Thanks!!

Leave a Reply