Serializing and deserializing ActionScript objects to the file system is an important capability that AIR brings to the table.
By serializing the data that an AIR application needs to persist, and storing it on the client's file system, you can
leverage a cookie-like behavior common in web applications. Although similar in concept to cookies, the serialized data that
AIR works with is not subject to size limitations as cookies are. Some possible uses are as a file-based storage facility
- a file-based database. You could store client preferences permanently or temporarily store data to the user's file system
while in offline mode. Once online, the data is transferred to a server-side data access component and ultimately pushed to a
relational database. Use the writeObject() [to serialize] or readObject() [to deserialize] methods of the FileStream class to
read and write the ActionScript objects you create in your applications.
The serialized data is encoded in AMF (ActionScript Message Format), a lightweight format with a low network bandwidth requirement.
An example demonstrates the type of code you write - see Listing 5.4.
You will need the AIR Runtime
|
Listing 5.4 Writing/Reading an ActionScript Object
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="#FFFFFF" width="300" height="300">
<mx:Script>
<![CDATA[
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.net.registerClassAlias;
private var fMovie:File;
private function setMovieToFS():void {
// register a Dictionary
registerClassAlias("flash.utils.Dictionary", Dictionary);
// build the ActionScript Object of Data
var oMovie:Object = new Object();
oMovie.Title = "A Beautiful Mind";
oMovie.Genre = "Drama";
oMovie.MPAARating = "R";
oMovie.MovieReleaseDate = "12/21/2001";
oMovie.RunningTime = "136 minutes";
// create a file instance that points to the storage location
fMovie = File.applicationStorageDirectory.resolve("movie.db");
var movieDict:movieDictionary = new movieDictionary();
movieDict["Title"] = oMovie.Title;
movieDict["Genre"] = oMovie.Genre;
movieDict["MPAARating"] = oMovie.MPAARating;
movieDict["MovieReleaseDate"] = oMovie.MovieReleaseDate;
movieDict["RunningTime"] = oMovie.RunningTime;
var fs:FileStream = new FileStream();
fs.open(fMovie, FileMode.WRITE);
fs.writeObject(movieDict);
fs.close();
btnDeserialize.enabled = true;
}
private function getMovieFromFS():void {
var movieDict:movieDictionary;
if (fMovie.exists) {
var fs:FileStream = new FileStream();
fs.open(fMovie, FileMode.READ);
movieDict = (fs.readObject() as movieDictionary);
fs.close();
taMovieInfo.text = "Title: " + movieDict.Title + "\n"
+ "Genre: " + movieDict.Genre + "\n"
+ "Rating: " + movieDict.MPAARating + "\n"
+ "Release Date: " + movieDict.MovieReleaseDate + "\n"
+ "Running Time: " + movieDict.RunningTime + "\n";
} else {
mx.controls.Alert.show("Error deserializing data", "I/O Error");
}
}
]]>
</mx:Script>
<mx:Button label="Serialize Movie Info" click="setMovieToFS()"/>
<mx:Button id="btnDeserialize" label="Deserialize Movie Info" click="getMovieFromFS()" enabled="false"/>
<mx:TextArea id="taMovieInfo" height="120" width="180"/>
</mx:WindowedApplication>
Figure 5.1 shows a screenshot of the AIR application from the code in Listing 5.4.
Serializing and Deserializing Movie Example.
|