views:

2190

answers:

3

Is it possible to serialize a hierarchy of objects in Flex, send the binary data to a URL for storage/retrieval on/from a server, and deserialize the data to restore the objects' original state?

I know it's possible to convert the objects into an XML format (haven't tried it yet), but I'm hoping to avoid parsing XML and rebuilding the objects manually. It would be nice to have functionality which can serialize/deserialize objects to a simple binary format (I did something similar in the past in Java, though not quite as easily as I would have liked). The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.

In pseudo-code, here's what I would like to do:

private var contentToSave:HBox = new HBox();

private function saveState(event:Event):void {
    var toSave:HBox = this.contentToSave;
    var data:? = /* serialize 'toSave' ActionScript classes to binary data*/;
    sendDataToServer(data, filename);
}
private function restoreState(filename):void {
    var data:? = getDataFromServer(filename);
    var savedData:HBox = /* deserialize binary 'data' to ActionScript classes */;
    this.contentToSave = savedData;
}
+1  A: 

Try the JSON based serialization package in ascorelib.

[...]but I'm hoping to avoid parsing XML and rebuilding the objects manually

AS handles XML just like any other native type. Rest assured. XML is the preferred way of dealing with data you will be pulling off and putting back on a server. Of course, the ascorelib gives you a JSON class -- so you may want to look at that as well.

The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.

IIRC, eval is part of the ECMAScript specification (and you will find it in Javascript). But not in AS3.0. It was there to a certain extent in some previous version(s?) but is no longer supported.

dirkgently
thanks for the response! Just to make sure I understand: if I convert the HBox (from the example) to XML and save this on a server, I can retrieve the XML and restore the HBox (and all children) as an object without parsing or doing any manual object creation?
bedwyr
No, you cannot just convert a HBox to XML. You'll have to implement a function for that. What I meant was XML handling is painless in AS3.
dirkgently
Ahh, I see. Thanks for your help.
bedwyr
It looks like the JSON libraries in ascorelib are what I was looking for. Thanks again!
bedwyr
+2  A: 

Take a look at ByteArray.writeObject(). which saves the passed object in AMF format into the byte array. I have not used this function too much, I don't exactly know what kind of objects it can serialize, but definitely not all.

David Hanak
A: 

I have the same situation how did you serialize the HBox or any other mxml component...? any help would be appreciated...

Rexo
I actually ended up going with XML. E4X handling in ActionScript is really quite painless and it makes creating factories to generate objects pretty straightforward. If you need to serialize, I would recommend it.
bedwyr