开发者

Flash/Flex: Is it possible to encode Dictionary using AMF?

开发者 https://www.devze.com 2022-12-27 16:35 出处:网络
As the title suggests, is it possible to use AMF to encode/decode Dictionaries (without subclassing, that is)?

As the title suggests, is it possible to use AMF to encode/decode Dictionaries (without subclassing, that is)?

For example, here's a test case:

function serializeAndReload(obj:*):* {
    var serialized:ByteArray = new ByteArray();
    serialized.writeObje开发者_开发技巧ct(obj);
    serialized.position = 0;
    return serialized.readObject();
}

function test():void {
    var d:Dictionary = new Dictionary();
    d[{}] = 42;
    d[d] = true;
    var x:* = serializeAndReload(d); // <<< x is an instance of Object
    trace(x['[object Object]']); // <<< traces '42'
}


You may be over-thinking. I use Object instead of Dictionary and it is automatically encoded using AMF. I use pyamf all the time to pass Objects/dicts around and its always worked without any mental effort on my part. Never have I needed to manually serialize/deserialize anything


The keys in the Dictionary need to be serializable, too.

[RemoteClass(alias="Foo")]
public class Foo
{
}

Test:

var d:Dictionary = new Dictionary();
var f:Foo = new Foo();
d[f] = "Hello";
var ba:ByteArray = new ByteArray();
ba.writeObject(d);
ba.position = 0;
var d2:Dictionary = Dictionary(ba.readObject());
for (var key:* in d2)
{
    trace(getQualifiedClassName(key));
    trace(d2[key]);
}

Output:

Foo
Hello
0

精彩评论

暂无评论...
验证码 换一张
取 消