Migrating Serialization Changes in AIR
Categories: air
Written By: sebi
This post in the Air Cookbook wins the O’Reilly contest for Adobe AIR Cook-off.
Migrating Serialization Changes in AIR
A beautiful example of using namespaces and the IExternalizable class during serialization in Adobe Air.
The idea is that to provide backward compatibility to your custom classes you want to store serialized in your application, you should add an incremental namespace to it.
namespace wd1_0 = "WD1.0"; namespace wd1_1 = "WD1.1";
In the case of reading your stored data, first you read the version information,
public function readExternal(input:IDataInput):void { version = input.readUTF(); var ns:Namespace = new Namespace(version); ns::parse(input); }
and after based on the extracted version number, you can use the relevant parsing method in that namespace version to avoid runtime serialization errors, when you try read non-existant class variables from the stored instance.
wd1_0 function parse(input:IDataInput):void { date = input.readObject() as Date; hi = input.readFloat(); } wd1_1 function parse(input:IDataInput):void { date = input.readObject() as Date; lo = input.readFloat(); hi = input.readFloat(); }
Simply, and brilliant. Make sure, you check Greg Jastrab’s original post and detailed explanation at Adobe AIR cookbook