在 AS3 的子类中序列化向量时出现问题

发布于 2024-11-06 04:22:57 字数 1963 浏览 2 评论 0原文

嘿!我正在尝试在 AS3 中序列化数据,但遇到了一个令人沮丧的问题。最初,我遇到了“myObjClass”不可转换的问题,但在我发现“registerClassAlias”之后,我的工作一切正常。

一段时间后,我将向量添加到 myObjClass 中。我之前遇到过向量字符串的麻烦,如下所示:

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

所以我知道解决方法是包括:

registerClassAlias("String", String);

我只是不确定如何为向量内的子向量(以及其他变量)注册别名。这是我的代码:

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();

问题#1:当这两行包含在我的编译中时,最后一行 (bytes.readObject()) 失败并出现错误:

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

这真的很奇怪。就好像前两个 registerClassAlias 正在撤销第三个。

问题#2:如果我注释掉两个“第一个问题”行(字符串/xml 类别名注册),它会将 myObj 转换为 myObjClass 就好;不会引发内部错误,并且应用程序不会停止。但是,它无法转换内部 String 和 XML 向量,应用程序输出中出现此错误(不停止):

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>.

所以我的问题是,如何注册一个类别名:

  • Vector.(myObjClass)
  • 字符串向量为Vector.(myObjClass) 的属性
  • XML 向量作为 Vector.(myObjClass) 的属性

全部同时?

Hey there! I'm trying to serialize data in AS3 but am running into a frustrating problem. Originally I had problems with "myObjClass" not being convertible, but after I discovered "registerClassAlias" I got things working allright.

Some time later, I added vectors to the myObjClass. I ran into trouble with Vector Strings before, as was reported here:

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

So I know the workaround is to include:

registerClassAlias("String", String);

I'm just not sure how to register an alias for a subvector within a vector (along with other variables). Here's 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 final line (bytes.readObject()) fails with the error:

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

This is really strange. It's as if the first two registerClassAlias's are undoing the third one.

Problem #2: If I comment out the two "first problem" lines (string/xml classalias registrations) it converts myObj to myObjClass just fine; no internal error is thrown and the application is not halted. However, it fails to convert the internal String and XML vectors, with this error appearing in the application Output (without halting):

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 property of Vector.(myObjClass)
  • A vector of XML as a property of Vector.(myObjClass)

All at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

破晓 2024-11-13 04:22:57

看起来像直接移动:

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

进入myObjClass就达到了目的,同时保留:

registerClassAlias("myObjClass", myObjClass);

在序列化类中。我不确定为什么将这三个存储在序列化类中不起作用 - 如果我更改代码的顺序,则会生成奇怪的错误,因此问题可能只是特定机器上的一些故障。

Looks like moving:

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

Into myObjClass directly did the trick, while retaining:

registerClassAlias("myObjClass", myObjClass);

In the serialization class. I'm not sure why storing the three together in the serialization class wasn't working - and was generating strange errors if I changed the order of the code, so the problem might just be some glitch on specific machine.

我的影子我的梦 2024-11-13 04:22:57

当您想使用复杂的矢量内容时,您必须注册每个子类(为您的 Vector.[String]),如下所示:

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

When you want to use a complex vector content, you have to register your each sub-class (for you Vector.[String]) as follow :

    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>;
}
み零 2024-11-13 04:22:57

测试您的代码 Andy,您需要注册您创建的类,以便 bytes.readObject() 返回带有自定义类的向量 - 在本例中为类 myObjClass。因此,如果您添加这行代码:

registerClassAlias("myObjClass", myObjClass);

因此,如果您将代码更改为如下所示,那么您应该返回所有数据都正确序列化的对象(我做了一个简单的测试,它似乎对我有效)。

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();

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

registerClassAlias("myObjClass", myObjClass);

So if you alter 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();
萌逼全场 2024-11-13 04:22:57
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);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文