[ActionScript 3] Array subclasses cannot be deserialized, Error #1034
- by aaaidan
I've just found a strange error when deserializing from a ByteArray, where Vectors cannot contain types that extend Array: there is a TypeError when they are deserialized.
TypeError: Error #1034: Type Coercion failed: cannot convert []@4b8c42e1 to com.myapp.ArraySubclass.
at flash.utils::ByteArray/readObject()
at com.myapp::MyApplication()[/Users/aaaidan/MyApp/com/myapp/MyApplication.as:99]
Here's how:
public class Application extends Sprite {
public function Application() {
// register the custom class
registerClassAlias("MyArraySubclass", MyArraySubclass);
// write a vector containing an array subclass to a byte array
var vec:Vector.<MyArraySubclass> = new Vector.<MyArraySubclass>();
var arraySubclass:MyArraySubclass = new MyArraySubclass();
arraySubclass.customProperty = "foo";
vec.push(arraySubclass);
var ba:ByteArray = new ByteArray();
ba.writeObject(arraySubclass);
ba.position = 0;
// read it back
var arraySubclass2:MyArraySubclass = ba.readObject() as MyArraySubclass; // throws TypeError
}
}
public class MyArraySubclass extends Array {
public var customProperty:String = "default";
}
It's a pretty specific case, but it seems very odd to me. Anyone have any ideas what's causing it, or how it could be fixed?