1. How to Use bytearray to copy objects:
/** <Br/> * copyobject <br/> * @ Param * ob <br/> * @ return * <br/> ***/< br/> Public static function copyobject (OB: *): * <br/>{< br/> var bytedata: bytearray = new bytearray (); </P> <p> bytedata. writeobject (OB); <br/> bytedata. position = 0; </P> <p> return bytedata. readobject (); <br/>}
2. How to store and extract more complex types through bytearray:
(That is
Custom object for serialization/deserialization
)
Package <br/> {<br/> public class user <br/> {<br/> private VaR _ firstname: string; <br/> private VaR _ lastname: string; </P> <p> public function set firstname (firstname: string): void <br/>{< br/> _ firstname = firstname; <br/>}</P> <p> public function set lastname (lastname: string): void <br/>{< br/> _ lastname = lastname; <br/>}</P> <p> Public Function get firstname (): String <br/>{< br/> return _ firstname; <br/>}</P> <p> Public Function get lastname (): String <br/>{< br/> return _ lastname; <br/>}< br/>}
Store the user instance in binary format to the server or to the local mongodobject. if you try to store the user instance in bytearray and extract it again later, when you read it, flash player will internally retrieve whether the user type has been registered, if no player exists
The object is deserialized according to the object type.
VaR User: User = new user (); <br/> User. name = "Dante"; <br/> User. age = 25; <br/> // out put: false <br/> trace (commonutil. copyobject (User) is user );
In the preceding example, false is output.
We need to use "registerclassalias" to inform Flash Player and register the user type so that flash player can automatically deserialize the user type instances.
Registerclassalias ("usertypealias", user); <br/> var User: User = new user (); <br/> User. name = "Dante"; <br/> User. age = 25; <br/> var item: User = commonutil. copyobject (User); <br/> trace (item. name, item. age );
PS:
Note that some basic types cannot be serialized or deserialized into AMF. For example, displayobject. Therefore, if you want to serialize A movieclip instance, it will not work.