数组子类无法反序列化,错误#1034子类、数组、错误、序列化

2023-09-08 14:22:07 作者:眼淚、錯覺

我从反序列化时,只发现了一个奇怪的错误的ByteArray ,其中向量 s不能包含类型延长阵列:有他们反序列化时,一个TypeError

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]

下面是如何:

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";
}

这是一个pretty的具体情况,但似乎很奇怪我。任何人有任何想法是什么原因引起的,还是如何能解决吗?

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?

推荐答案

嗯,看来数组序列很难codeD。你一定要发布一个bug报告。

well, it seems array serialization is hardcoded. you should definitely post a bug report.

其实你贴,因为 ba.readObject不抛出一个错误()作为MyArraySubclass 简直就是空。 MyArraySubclass(ba.readObject())但是会。

actually the code you posted doesn't throw an error since ba.readObject() as MyArraySubclass is simply null. MyArraySubclass(ba.readObject()) however would.

你可以尝试修复它,将是实现 IExternalizable ,altough我不知道它会工作的更好。

what you could try to fix it, would be to implement IExternalizable, altough I'm not sure it'll work any better.

我想问题是,阵列在ActionScript中一个非常非常特殊的类(在这个意义上,在某些方面它比任何其他动态类罢了,但在其他方面,它不是在所有),这导致了很多在VM阵列特异code的。此外,一个问题是,为什么你需要继承阵列

I guess the problem is that Array is a very very special class in ActionScript (in the sense that in some way it is nothing more than any other dynamic class, but in other ways it's not at all) which leads to a lot of Array-specific code in the VM. Also, a question is, why do you need to subclass Array?