正确地将 flash.utils.Dictionary 序列化为 SharedObject

发布于 2024-07-19 07:18:12 字数 738 浏览 6 评论 0原文

我的 Flex 项目中有一个名为 HashMap 的便利集合类,它本质上是 flash.utils.Dictionary 的包装器,带有一堆便利方法和添加的(同步的)ArrayCollection,以便我可以将 HashMap 传递给需要的可绑定控件。一个数组集合。 一切都很好。

我现在发现,将 HashMap 放在本地 SharedObject 中是行不通的。

注册该类会导致它被存储并以正确的类型返回,并且 ArrayCollection 成员返回得很好,但字典不存储其数据。

这是一个片段:

[RemoteClass(alias="com.tamedtornado.collections.HashMap")]
public class HashMap extends Proxy
{
    public var hash:Dictionary = new Dictionary();

    // Keeps an array collection as well so we can give this to a data bound control

    [Bindable]
    public var collection:ArrayCollection = new ArrayCollection();

这就是相关的内容。 让字典正确存储自身的过程是什么? 实际上,我必须使 ArrayCollection 成为瞬态,因为现在每次刷新 SO 时,我都会获得(在字典中唯一键入的)数据的另一个副本。

I have a convenience collection class in my Flex project called HashMap, which is essentially a wrapper around the flash.utils.Dictionary with a bunch of convenience methods and an added (synced) ArrayCollection so that I can pass the HashMap to bindable controls that want an ArrayCollection. That all works fine.

What doesn't work fine, I'm finding out just now, is putting that HashMap in a local SharedObject.

Registering the class causes it to be stored and come back as the proper type, and the ArrayCollection member comes back just fine, but the Dictionary doesn't store its data..

Here's a snippet:

[RemoteClass(alias="com.tamedtornado.collections.HashMap")]
public class HashMap extends Proxy
{
    public var hash:Dictionary = new Dictionary();

    // Keeps an array collection as well so we can give this to a data bound control

    [Bindable]
    public var collection:ArrayCollection = new ArrayCollection();

So that's the relevant stuff. What's the procedure for getting that Dictionary to store itself correctly? I actually have to make the ArrayCollection transient, as right now every time the SO is flushed, I'm getting another copy of the (uniquely keyed in the Dictionary) data.

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

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

发布评论

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

评论(1

不必了 2024-07-26 07:18:12

我对此进行了更多修改,并得到了很多愚蠢的结果,试图让序列化“正常工作”,所以我最终只实现了 IExternalized 接口,并修复了它。

    public function readExternal(input:IDataInput):void
    {
        var hashCount:int = input.readInt();

        for (var i:int = 0;i<hashCount;i++)
        {
            var prop:Object = input.readObject();
            var val:Object = input.readObject();

            putEntry(prop,val);
        }
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeInt(collection.length);

        for (var prop:Object in hash)
        {
            output.writeObject(prop);

            output.writeObject(hash[prop]);
        }
    }

所有内容都会被存储并正确键入。 存储的对象必须是本机类(如 String),或者具有 [RemoteClass] 元数据标记/registerClassAlias() 调用。 但除此之外,它还有效。

I tinkered with this some more, and got lots of goofy results trying to let the serialization "just work", so I finally just implemented the IExternalizable interface, and that fixed it.

    public function readExternal(input:IDataInput):void
    {
        var hashCount:int = input.readInt();

        for (var i:int = 0;i<hashCount;i++)
        {
            var prop:Object = input.readObject();
            var val:Object = input.readObject();

            putEntry(prop,val);
        }
    }

    public function writeExternal(output:IDataOutput):void
    {
        output.writeInt(collection.length);

        for (var prop:Object in hash)
        {
            output.writeObject(prop);

            output.writeObject(hash[prop]);
        }
    }

Everything gets stored and comes across properly typed. The objects stored have to either be native classes (like String), or have a [RemoteClass] metadata tag/registerClassAlias() call. But other than that, it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文