Skip to main content
April 27, 2011
Question

Save an object to file for later use.

  • April 27, 2011
  • 1 reply
  • 580 views

Here is what I want to do: Take an object that has a bunch of properties set and possibly holds other objects in it, and serialize it or somehow save it to a file using the File class. Reversing the process is also necessary. I know about SharedObject, but I do not get the ability to dynamically change the save/load location as in the File class. Is this possible with FMS, and if so, what is the procedure? If it is not possible, then what options do I have to achieve the same goals? I have read the SSLR and searched for a solution but do not seem to see any means to do this. Please help?

    This topic has been closed for replies.

    1 reply

    April 27, 2011

    Shortly after I posted I found the ByteArray class was the key ingredient I had been missing. I did not know what I was looking at when I first saw it! To serialize/deserialize an object, you do something like this:

    var my_obj = new Object();

    my_obj.name = "Serialize me";

    var ba = new ByteArray()

    ba.writeObject(my_obj);               // serialized - you may now store this in a file...

    ba.position = 0;                           // for the example this is necessary because the pointer is currently at EOF.

    var new_obj = new Object();

    new_obj = ba.readObject();          // deserialized.

    trace(new_obj.name);                  // "Serialize me"

    My questions now are: do I get to control what encoding scheme is used when using read/writeObject(), and how does registering classes relate to recovering an object as a class when deserializing something, should I want the object of a certain class 'resurrected' as if it never happened?

    April 28, 2011

    Alright... I will answer my own questions for the benefit of other newbies like myself.

    Regarding control over the object encoding - there is a property on the ByteArray class called objectEncoding. For AMF0 you would set it to 0 and for AMF3 you would use 3. I prefer to use more suggestive labels in my code so I set these labels as constants with the appropriate values. As I understand it, since I am using FMS 4, AMF3 is the default (correct me if I am wrong) and if you are communicating with another FMS of version 2 or lower, the object encoding automatically falls back to AMF0 and ignores your settings.

    Regarding application.registerClass() - This is very important to you if you want to preserve a class state and be able to use the methods in it as well. It should be noted it is used whether you are locally de/serializing or trasferring objects of a particular class across a netconnection; you still need to register your classes if you wish to utilize the methods inside that class. If you do not do this you may access the properties, but the FMS will return undefined if you call any methods you know are supposed to be there. This makes sense, of course... you know what is there, but the FMS doesn't until you tell it by using application.registerClass()!