Serializing File Reference Instances in Adobe AIR

mikechambers June 22nd, 2007

In one of the projects I am working one with the Adobe AIR bus tour, I had a need to serialize instance of File class to the file system. Normally you can do this with File.writeObject(), but as the player serializer / de-serializer does not know about the File class, this would not work for me.

I came up with a simple solution that I wanted to share. Basically, you need to extend the File class and provide the meta data for it to tell the player how to serialize and de-serialize the class. This will then allow it to be serialized to the file system.

Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

	<mx:Script>
		<![CDATA[
			import flash.filesystem.FileMode;
			import flash.filesystem.FileStream;
			import flash.filesystem.File;
			private function onSerialize():void
			{
				//file reference to serialize
				var f:MyFile = new MyFile("app-resource:/foo.txt");

				//file that we will write serialized object to
				var ser:File = new File("app-storage:/store.db");
				var fs:FileStream = new FileStream();
					fs.open(ser, FileMode.WRITE);
					fs.writeObject(f);
					fs.close();

			}

			private function onDeSerialize():void
			{
				var ser:File = new File("app-storage:/store.db");
				var fs:FileStream = new FileStream();
					fs.open(ser, FileMode.READ);
				var f:MyFile = fs.readObject() as MyFile;
					fs.close();
			}

		]]>
	</mx:Script>

	<mx:Button label="Serialize" left="10" top="10" click="onSerialize()"/>
	<mx:Button label="De-Serialize" left="92" top="10" click="onDeSerialize()"/>

</mx:WindowedApplication>

and the MyFile.as class:

package
{
	import flash.filesystem.File;

	[RemoteClass(alias="MyFile")]
	public class MyFile extends File
	{
		public function MyFile(path:String = null)
		{
			super(path);
		}
	}
}

The key line is the [RemoteClass(alias="MyFile")] which tells the player how to serialize the class.

Of course, this technique should work with any other built in class that you need to serialize.

8 Responses to “Serializing File Reference Instances in Adobe AIR”

  1. Chase Brammeron 22 Jun 2007 at 4:06 pm

    Just out of curiosity… why would you serialize a class into local storage?

  2. Diego Volpeon 22 Jun 2007 at 7:55 pm

    I’ve been using this technique to serialize/deserialize the state of the user and all “presentation” data generated by him in a application like Scrapblog.
    It is easier to save and retrieve complex information (hierarchical object tree) and keep the state (selected items, editing properties, preferences, context, etc).

  3. Doubleslashon 24 Jun 2007 at 4:50 am

    Hi Mike,

    just wanted to know, why you want to (de-)serialize a File-reference?

    Greetz

  4. [...] Check the article and code here. [...]

  5. [...] mikechambers‘blog June 22nd, 2007 [...]

  6. mikechamberson 25 Jun 2007 at 8:55 am

    >Just out of curiosity… why would you serialize a class into local storage?

    Hehe… I was waiting for someone to ask that.

    Basically, I needed to associated from meta data with an image that I had written to the file system. I could have created a value object that contained the file path and the meta data, but:

    1. seemed like overkill when I could just store the file reference
    2. I was worried about the value object getting out of sync with the file system

    Plus, it makes the code a little cleaner when I am serializing and de-serializing objects.

    You can view the code here:

    http://onairbustour.googlecode.com/svn/trunk/projects/AIRSnapshot/src/

    To be honest, im not sure if this is a “best practices” sort of thing, but regardless, the technique works for other classes also.

    mike chambers

    mesh@adobe.com

  7. Reinier Rossenon 12 Feb 2008 at 4:07 pm

    Hi Mike,

    Great post, it really helped me out but i ran into another problem i can not figure out so far. If you listen for the SELECT_MULTIPLE event you receive an event object with a property “files”, which is an array that in turn containts File instances…not MyFile objects, even though the original object firing that event was of the MyFile type.

    Am i running into a technical limitation here or just missing something? I fear the former, i hope the latter.

    Cheers from the Netherlands!

  8. [...] If serializing a FileReference is what’s necessary - would this work? http://www.mikechambers.com/blog/200…-in-adobe-air/ [...]

Trackback URI | Comments RSS

Leave a Reply