May 2018

          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
:: 05/05/2007 ::
AIR - Serializing and Deserializing Data
Return   Back To Top
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="" layout="vertical" backgroundColor="#FFFFFF" width="300" height="300">
		import flash.filesystem.FileMode;
		import flash.filesystem.FileStream;
		import flash.filesystem.File;
		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();, FileMode.WRITE);
			btnDeserialize.enabled = true;
		private function getMovieFromFS():void {
			var movieDict:movieDictionary;
			if (fMovie.exists) {
				var fs:FileStream = new FileStream();, FileMode.READ);
				movieDict = (fs.readObject() as movieDictionary);
				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 {"Error deserializing data", "I/O Error");
	<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"/>

Figure 5.1 shows a screenshot of the AIR application from the code in Listing 5.4.

Serializing and Deserializing Movie Example Screenshot

Serializing and Deserializing Movie Example.


canada goose femme pas cher Soldes Louboutin Chaussures Soldes Louboutin louis vuitton taschen outlet louboutin outlet uk billig canada goose canada goose tilbud goyard pas cher longchamp bags outlet Monlcer udsalg billige parajumpers YSL replica sac louis vuitton pas cher Canada Goose Pas Cher Canada Goose Outlet UK Moncler Outlet uk goyard replica bags polo Lacoste pas cher Bolsos Longchamp España Moncler Jakker tilbud Doudoune moncler pas cher Parajumpers Jakker tilbud Ralph Lauren Soldes Parajumpers Outlet louis vuitton replica Moncler Jas sale Billiga Canada Goose Jacka Canada Goose outlet Billiga Moncler Doudoune Canada Goose Pas Cher Canada Goose Pas Cher Louboutin Soldes Canada Goose Pas Cher Hemers replica Doudoune Canada Goose Pas Cher prada replica Canada Goose Pas Cher Canada Goose Soldes Doudoune Canada Goose Pas Cher Canada Goose Pas Cher Canada Goose outlet Canada Goose outlet Canada Goose outlet