Interpreting vector selection in a subclass in AS3

Hey there! I am trying to serialize data in AS3, but I ran into a frustrating problem. Initially, I had problems with "myObjClass" that were not convertible, but after I discovered "registerClassAlias", everything worked out fine for me.

After some time, I added vectors to myObjClass. I used to run into problems with Vector Strings, as reported here:

https://bugs.adobe.com/jira/browse/FP-6693

Therefore, I know that a workaround is to enable:

registerClassAlias("String", String); 

I just don't know how to register an alias for a subvector inside a vector (along with other variables). Here is my code:

 var newObj:myObjClass = new myObjClass(); newObj.mySubXML = new Vector.<XML>(); newObj.mySubString = new Vector.<String>(); var myObj:Vector.<myObjClass> = new Vector.<myObjClass>(); myObj.push(newObj); registerClassAlias("String", String); // Problem #1 registerClassAlias("XML", XML); // Problem #1 registerClassAlias("VecMyObjClass", Vector.<myObjClass> as Class); // Problem #2 // registerClassAlias("myObjClass", myObjClass); // This and the above are interchangable (same errors) var bytes:ByteArray = new ByteArray(); bytes.writeObject(myObj); bytes.position = 0; myObj = bytes.readObject(); 

Problem # 1: When these two lines are included in my compilation, the last line (bytes.readObject ()) fails with an error:

 Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<Object>@42edb21 to __AS3__.vec.Vector.<myObjClass>. 

This is really weird. It is as if the first two registerClassAlias ​​have canceled the third.

Problem # 2: If I comment on two lines of the first problem (registering string / xml classes), it will perfectly convert myObj to myObjClass; an internal error does not occur and the application does not stop. However, it cannot convert the internal String and XML vectors, and this error appears in the Output application (without stopping):

 TypeError: Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<Object>@3aabc11 to __AS3__.vec.Vector.<XML>. TypeError: Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<Object>@3aabc41 to __AS3__.vec.Vector.<String>. 

So my question is: how can I register a class alias for:

  • Vector. (MyObjClass)
  • A vector of strings as a Vector property. (myObjClass)
  • An XML vector as a Vector property. (myObjClass)

In the same time?

+4
source share
4 answers

Like moving:

 registerClassAlias("XML", XML); registerClassAlias("String", String); 

In myObjClass directly did the trick, keeping:

 registerClassAlias("myObjClass", myObjClass); 

In the serialization class. I'm not sure why storing the three together in the serialization class didn’t work - and was creating strange errors if I changed the order of the code, so the problem might just be a crash on a particular machine.

+6
source

If you want to use complex vector content, you must register your each subclass (for you Vector. [String]) as follows:

  package { import flash.display.Sprite; import flash.net.registerClassAlias; import flash.utils.ByteArray; public class StackOverFlow extends Sprite { public function StackOverFlow() { var newObj:myObjClass = new myObjClass(); newObj.mySubXML = new Vector.<XML>(); newObj.mySubString = new Vector.<String>(); var myObj:Vector.<myObjClass> = new Vector.<myObjClass>(); myObj.push(newObj); registerClassAlias("String", String); registerClassAlias("XML", XML); registerClassAlias("myObjClass", myObjClass); registerClassAlias("VecMyObjClass", Vector.<myObjClass> as Class); var bytes:ByteArray = new ByteArray(); bytes.writeObject(myObj); bytes.position = 0; myObj = bytes.readObject(); } } } internal class myObjClass{ public var mySubXML : Vector.<XML>; public var mySubString : Vector.<String>; } 
+4
source

To test your Andy code, you need to register the class you created, so bytes.readObject() returns a vector with custom classes - in this case the class myObjClass. Therefore, if you add this line of code:

 registerClassAlias("myObjClass", myObjClass); 

So, if you change the code to look like this, then you should return the object with all its data correctly serialized (I did a simple test and it seems to work on my end).

 var newObj:myObjClass = new myObjClass(); newObj.mySubXML = new Vector.<XML>(); newObj.mySubString = new Vector.<String>(); var myObj:Vector.<myObjClass> = new Vector.<myObjClass>(); myObj.push(newObj); registerClassAlias("String", String); registerClassAlias("XML", XML); registerClassAlias("myObjClass", myObjClass); var bytes:ByteArray = new ByteArray(); bytes.writeObject(myObj); bytes.position = 0; myObj = bytes.readObject(); 
+2
source
 registerClassAlias("String", String); registerClassAlias("VecString", Vector.<String> as Class); registerClassAlias("XML", XML); registerClassAlias("VecXML", Vector.<XML> as Class); registerClassAlias("myObjClass", myObjClass); registerClassAlias("VecMyObjClass", Vector.<myObjClass> as Class); 
+1
source