17.6.序列化类型对象

17.6.1. 问题
我想要能去保存自定数据类型的对象到一个共享对象中。
17.6.2. 解决办法
使用registerClassAlias()方法来注册类型化的对象到Flash 运行时,然后存贮这个对象实例到一个共享对象。
17.6.3. 讨论
所有的共享对象都包含了一个叫作objectEncoding 的属性,用来标示在这个共享对象中使用的AMF 版本。默认情况,objectEncoding 被设为AMF3—标准的as3 格式。你也可以设定一个共享对象的编码来使用as1 和as2 格式,通过设定objectEncoding 为AMF0。

当存贮类型化数据到一个共享对象是,确保已经在运行时注册了类型化对象,这样在特殊的情况发生时候你的应用程序才能确切地指导如何序列化和反序列化这个对象。呼叫registerClassAlias 方法来注册类。

registerClassAlias 方法需要2 个参数。第一个必须是目标对象的完整限定类名以字符串的形式,通常就是类的别名。第二个参是你想要注册到第一个参所提供的类名的对象。
+展开
-ActionScript
registerClassAlias( "package.ClassType", ClassType );

下面的例子是一个自定数据类型通过registerClassAlias()方法来注册,确保属性值被序列化之后存到共享对象中,以及被同一个会话或其他会话读取出来并且反序列化。
+展开
-ActionScript
// the strongly typed object to use for this example
package oreilly.cookbook{
[Bindable]
public classAutomobile
{
private var _make : String;
public function get make() : String { return _make; }
public function set make( value : String ) : void {
_make = value;
}
private var _model : String;
public function get model() : String { return _model; }
public function set model( value : String ) : void {
_model = value;
}
private var _year : Number;
public function get year() : Number { return _year; }
public function set year( value : Number ) : void {
_year = value;
}
public functionAutomobile()
{
super();
}
public function toString() : String
{
return "make: " + _make + "\nmodel: " + _model +
"\nyear: " + _year;
}
}
}

Automoible 类拥有与制造一个汽车相关的基础属性。属性值可以被分别地存贮存储到共享对象实例中,但是你也需要注册这些自定的数据类型,确保当数据被再次从共享对象中读取到应用程序时,数据反序列化操作上可以序列化以及保留属性值。下面的例子注册了一个Automoible 类的类名并且读写自定义对象到本地共享对象:
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxmllayout="absolute"
initialize="onInit()">

<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import oreilly.cookbook.Automobile;
import flash.net.registerClassAlias;
private var lso : SharedObject;
private function onInit() : void
{
// register the class alias to be able to later correctly
// deserialize and serialize this class type
registerClassAlias( "oreilly.cookbook.Automobile", Automobile );
// initialize the shared object
lso = SharedObject.getLocal( "automobile" );
}
private function save() : void
{
var automobile : Automobile = newAutomobile();
automobile.make = make.text;
automobile.model = model.text;
automobile.year = parseFloat( year.text );
lso.data.automobile = automobile;
lso.flush();
status.text = "your automobile has been saved";
}
private function retrieve() : void
{
if ( lso.data.automobile != undefined )
{
var objectInfo : String = ObjectUtil.toString(lso.data.automobile);
status.text = "retrieving type information about the currently" +
" stored object\n\n";
status.text += lso.data.automobile.toString() + "\n\n";
status.text += objectInfo;
}
else
{
status.text = "nothing has been stored into the shared object";
}
}

]]>
</mx:Script>
<mx:VBox width="100%height="100%">
<mx:Form>
<mx:FormItem label="makewidth="100%">
<mx:TextInput id="makewidth="100%"/>
</mx:FormItem>
<mx:FormItem label="modelwidth="100%">
<mx:TextInput id="modelwidth="100%"/>
</mx:FormItem>
<mx:FormItem label="yearwidth="100%">
<mx:TextInput id="yearwidth="100%"/>
</mx:FormItem>
<mx:FormItem direction="horizontalwidth="100%">
<mx:Button label="save lso datawidth="100%"
click="{ save() }"/>

<mx:Button label="retrieve lso datawidth="100%"
click="{ retrieve()}"/>

</mx:FormItem>
</mx:Form>
<mx:TextArea id="statuseditable="falsewidth="100%height="100%" />
</mx:VBox>
</mx:Application>

当你需要存贮指定的数据到用户的本地机器上时,上面的方法非常有效,特别是大的强类型值对象。通过注册类和初始化一个对象,你可以避免处理XML 或者文本数据的运算开销,还能很好的维护对象的属性和类型。

加支付宝好友偷能量挖...


评论(0)网络
阅读(142)喜欢(0)flash/flex/fcs/AIR