views:

747

answers:

1

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.

+4  A: 

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.

Jason Maskell
That probably deserves an upmod :) I've had some real problems with serialisation in actionscript, mostly due to not having the appropriate class compiled into the SWF (just using a cast doesn't include it), it got very confusing for a while.
inferis