AMF is crucial for all types of serialization and communications.
All native data serialization is customarily handled by the class ByteArray
. When serialized, the data type
information is marked out by the name included in the metadata tag
RemoteClass
.
Example 6-1 is a
small example from the Flash Builder’s NetworkingSamples project that comes with the
book. It includes an application RegisteredClassvsUnregistered.mxml and two
classes: RegisteredClass
and Unregistered
.
Example 6-1. Serialization with and without the RemoteObject meta tag
package { [RemoteClass(alias="com.RegisteredClass")] public classRegisteredClass
{ } } package { public classUnregisteredClass
{ } } <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="test()"> <mx:Script> <![CDATA[ import flash.utils.ByteArray private function serializeDeserialize(a:Object) : void { var ba : ByteArray = new ByteArray(); ba.writeObject(a); ba.position = 0; var aa:Object = ba.readObject(); trace( aa ); } private function test():void { serializeDeserialize( new RegisteredClass()); serializeDeserialize( new UnregisteredClass()); } ]]> </mx:Script> </mx:Application>
In Example 6-1, the
function serializeDeserialize()
serializes the object passed as an argument into a ByteArray
, and then reads it back into a
variable aa
of type Object
. The application makes two calls to this
function. During the first call, it passes an object that contains the
metadata tag, marking the object with a data type RegisteredClass
; the second call passes
the object that does not use this metadata tag. Running this program
through a debugger displays the following output in the console:
[SWF] /NetworkingSamples/NetworkingSamples.swf - 798,429 bytes after decompression [object RegisteredClass] [object Object]
Annotating a class with the RemoteClass
metadata tag allows Flash Player to
store, send, and restore information in the predictable, strongly typed
format. If you need to persist this class, say in AIR disconnected mode,
or communicate with another .swf
locally via the class LocalConnection
,
following the rules of AMF communications is crucial. In the example,
RemoteClass
ensures that during
serialization, the information about the class will be preserved.
18.227.46.229