PDA

View Full Version : Problems with ByteArray.writeObject()


inspiratielozer
08-20-2009, 09:01 PM
Hi all,

Why is it that you can't write out an arraycollection consisting of instances of classes that contain objects as private datamembers?

my code looks like this:


var file:File = new File( destPath );
var ba:ByteArray = new ByteArray();
ba.objectEncoding = ObjectEncoding.DEFAULT;
ba.writeObject( projects.source ); => gives error
ba.position = 0;

var fs:FileStream = new FileStream();
fs.open( file, FileMode.WRITE );
fs.writeBytes( ba, 0, ba.length );
fs.close();

** projects= ArrayCollection
projects contains another ArrayCollection "relations"
in "relations" instances of the class Relation.as get saved
Relation.as contains a private var object:Canvas;

wvxvw
08-20-2009, 09:11 PM
1. Not every object may be serialized at all.
2. Those that may be serialized require that you:
a. register their class for serialization by using registerClassAlias or, in Flex you can use [RemoteClass] meta, which basically does the same thing.
b. if they happen to have constructor arguments, they need defaults for them.
c. alternatively, they should implement IExternalizable.

However, all prime types are serializable (they don't need to implement IExternalizable).
Some flash.* classes are also good for serialization (ByteArray, BitmapData).

inspiratielozer
08-20-2009, 09:32 PM
It's actually like this:

Relation.as contains a private var object:ObjectCanvas;

ObjectCanvas extends ObjectHandles
ObjectHandles extends Canvas implements Selectable

If i do like this it still won't work:

[RemoteClass]
public class ObjectCanvas extends ObjectHandles

(every parameter in the constructor has default values)

Do i also need to implement IExternalizable? or is it just another way than [RemoteClass]

Btw i use Flex 3

Thanks for your answers

wvxvw
08-20-2009, 09:37 PM
Visual objects cannot be serialized.
By implementing IExternalizable you let the player know how to reconstruct the object, if not through the common procedure.
You may want to look in how ArrayCollection implements this interface to get a bit better understanding of it.