开发者

Problem reading an byte array into an object after being decoded from a string.

I'm trying to serialize and deserialize a byte array to a string using Base64 for as3.

Here is my code

public function Serialize(vector:Vector.<Action>):String
    {
        var bytes:ByteArray = new ByteArray();
        var serialized:String = "";

        registerClassAlias("Action", Action);

        try { bytes.writeObject(vector); trace("Unserialized bytes:\n" + bytes + "\n"); }
      开发者_运维知识库  catch (e:Error) { trace("Writing object Failed!!\n" + e); }

        bytes.position = 0;
        bytes.compress();
        trace("Compressed bytes:\n" + bytes + "\n");

        return Base64.encode(bytes);
    }

public function Deserialize(serializedString:String):Vector.<Action>
    {
        var deserialized:ByteArray = new ByteArray();
        var deserializedObj:Object = new Object();
        var newVector:Vector.<Action> = new Vector.<Action>();

        try { deserialized = Base64.decode(serializedString); trace("Deserialized bytes:\n" + deserialized + "\n"); }
        catch (e:Error) { trace("Decoding Failed!!\n" + e); }

        deserialized.position = 0;
        deserialized.uncompress();
        trace("Uncompressed bytes:\n" + deserialized + "\n");

        try { deserializedObj = deserialized.readObject(); trace("Moving bytes into an object" + deserializedObj); }
        catch (e:Error) { trace("Reading Object Failed!!\n" + e); }

        for each(var a:Action in deserializedObj)
        {
            trace(a);
            newVector.push(a);
        }

        return newVector;
    }

I tested this and serializing works fine, but

Reading Object Failed!!
ArgumentError: Error #1063: Argument count mismatch on System::Action(). Expected 2, got 0.

is the error I get after calling deserializedObj = deserialized.readObject(); Should I be get that error if I'm just trying to put this into an object? I'm not trying to put it into an action class object yet, but if it is then the action it's getting doesn't have parameters that were originally inside.


It looks like Action has a constructor that takes two parameters (without defaults). readObject apparently can't handle constructing objects with non-default constructors.

Change Action so that its constructor parameters have defaults and see if that helps.


I have never seen that error System:Action The only thing that I see that could be an issue.

try moving

deserialized.uncompress();

above

deserialized.position = 0;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜