Properly serializing flash.utils.Dictionary to a SharedObject

五迷三道 提交于 2019-12-23 15:44:16

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/850466/properly-serializing-flash-utils-dictionary-to-a-sharedobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!