开发者

Deseralize Vector.<String> from ByteArray

I got this situation:

var vector:Vector.<String> = new Vector.<String>();
vector.push("uno");
vector.push("dos");
vector.push("tres");

var serializer:ByteArray =开发者_运维知识库 new ByteArray();
serializer.writeObject(vector);
serializer.position = 0;

var vector2:Vector.<String> = serializer.readObject();
trace(vector2[0]);
trace(vector2[1]);
trace(vector2[2]);

When the code reach the trace(vector2[0]); sentence i got this error:

TypeError: Error #1034: Type Coercion failed: cannot convert __AS3__.vec::Vector.<Object>@16820d01 to __AS3__.vec.Vector.<String>.

Now, if i call registerClassAlias("com.some.alias", String) before declare the vector variable the code executes without problem.

Why the call to registerClassAlias() is needed in this case?


the writeObject() and readObject() methods respectively write(encode) and return a generic (untyped) Object typed objects.

so typing your vector2 as a Vector.< String > will cause the error you get while typing it as a Vector.< Object > or even not typing it at all should work ; in this case an Object will be returned:

var vector:Vector.<Object> = new Vector.<Object>();//Object
vector.push( 'abc' );
vector.push( 123 );
vector.push( { foo:"bar" } );

var serializer:ByteArray = new ByteArray();
serializer.writeObject(vector);
serializer.position = 0;

var vector2:Object = serializer.readObject();// NB: readObject returns an Object
trace( 'vector2 is a Object ?', vector2 is Object );//true
trace( 'vector2 is a Vector.<Object> ?', vector2 is Vector.<Object> );//true but see below

trace( vector2[0], 'is a String ?', vector2[0] is String );//abc is a String ? true
trace( vector2[1], 'is a Number ?', vector2[1] is Number );//123 is a Number ? true  ( int and uints are Number )
trace( vector2[2] );//[object Object] untyped object
trace( 'vector2[2].foo => ', vector2[2].foo );// vector2[2].foo =>  bar
trace( 'vector2[2].bar => ', vector2[2].bar );// vector2[2].bar =>  undefined

the funny thing is that Vector.< Number > are also typed back correctly when read back while all other types fail are casted as Objects comment / uncomment the vector declarations below to see how it is typed after decoding.

//correctly typed
var vector:Vector.<Object> = new Vector.<Object>();
vector.push( 'abc', 123, { foo:"bar" } );

//var vector:Vector.<Number> = new Vector.<Number>();
//vector.push( 123, 456, 789 );

//generic Objects instead
//var vector:Vector.<String> = new Vector.<String>();
//vector.push( "a", "b", "c" );

//var vector:Vector.<Function> = new Vector.<Function>();
//vector.push( function a():*{}, function b():*{}, function c():*{} );

//var vector:Vector.<Boolean> = new Vector.<Boolean>();
//vector.push( true, false, true );

var serializer:ByteArray = new ByteArray();
serializer.writeObject(vector);
serializer.position = 0;

var vector2:* = serializer.readObject();
trace( 'vector2 is a Object ?', vector2 is Object );//true
trace( 'vector2 is a Vector.<Object> ?', vector2 is Vector.<Object> );
trace( 'vector2 is a Vector.<Number> ?', vector2 is Vector.<Number> );
trace( 'vector2 is a Vector.<String> ?', vector2 is Vector.<String> );
trace( 'vector2 is a Vector.<Function> ?', vector2 is Vector.<Function> );
trace( 'vector2 is a Vector.<Boolean> ?', vector2 is Vector.<Boolean> );

I don't know exactly why Numbers also make it, I don't know much about serialization though...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜