正确地将flash.utils.Dictionary序列化到SharedObject [英] Properly serializing flash.utils.Dictionary to a SharedObject

查看:149
本文介绍了正确地将flash.utils.Dictionary序列化到SharedObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Flex项目中有一个名为HashMap的便捷集合类,它本质上是一个包含flash.utils.Dictionary的包装器,有一堆便捷方法和一个添加的(同步的)ArrayCollection,这样我就可以将HashMap传递给可绑定的需要ArrayCollection的控件。这一切工作正常。

什么不好,我刚才发现,把HashMap放在一个本地的SharedObject。



注册这个类会导致它被存储并返回正确的类型,ArrayCollection成员返回就好了,但Dictionary不存储它的数据。 b
$ b

下面是一个片段:

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

//保持一个数组的集合,所以我们可以给这个数据绑定控制

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

这就是相关的东西。什么是让该字典存储正确的过程?我实际上必须做ArrayCollection瞬态,就像现在每当SO被刷新,我得到(在字典中唯一键入的)数据的另一个副本。

{
var hashCount:int = input.readInt( ); ($ i

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

putEntry(prop,val);


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

(var prop:哈希中的对象)
{
output.writeObject(prop);

output.writeObject(hash [prop]);






$ b

所有东西都被储存起来,并且正确输入。存储的对象必须是本地类(如String),或者具有[RemoteClass]元数据标记/ registerClassAlias()调用。但除此之外,它的工作。


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.

解决方案

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.

这篇关于正确地将flash.utils.Dictionary序列化到SharedObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆