views:

192

answers:

9

What I would like to do is capture an object that's in memory to disk for testing purposes. Since it takes many steps to get to this state, I would like to capture it once and skip the steps.

I realize that I could mock these objects up manually but I'd rather "record" and "replay" real objects because I think this would be faster.

Edit: The question is regarding this entire process, not just the serialization of the object (also file operations) and my hope that a tool exists to do this process on standard objects.

I am interested in Actionscript specifically for this is application but...

  • Are there examples of this in other programming languages?

  • What is this process commonly called?

  • How would this be done in Actionscript?

Edit:

  • Are there tools that make serialization and file operations automatic (i.e. no special interfaces)?

  • Would anybody else find the proposed tool useful (if it doesn't exist)?

Use case of what I am thinking of:

ObjectSaver.save(objZombie,"zombie"); //save the object
var zombieClone:Zombie = ObjectSaver.get("zombie"); // get the object

and the disk location being configurable somewhere.

+2  A: 

Converting objects to bytes (so that they can be saved to disk or transmitted over network etc.) is called serialization.

But in your case, I don't think that serialization is that useful for testing purposes. When the test creates all its test data every time that the test is run, then you can always trust that the test data is what you expect it to be, and that there are no side-effect leaking from previous test runs.

Esko Luontola
Serialization I thought would be the path I would take but serialization is only half then it needs to be save to disk. So, the question is regarding this entire process, not just the serialization of the object and my hope that a tool exists to do this process on standard objects.
Brandon
+1  A: 

I think you are talking about "object serialization".

Alexander
+1  A: 

What is this process commonly called?

  • Serializing / deserializing
  • Marshalling / unmarshalling
  • Deflating / inflating
Patrick McElhaney
+1  A: 

It's called Serialization

Perl uses the Storable module to do this, I'm not sure about Actionscript.

denkfaul
+2  A: 

I asked the same question for Flex a few days ago. ActionScript specifically doesn't have much support for serialization, though the JSON libraries mentioned in one of the responses looked promising. Serialize Flex Objects to Save Restore Application State

bedwyr
Cool, thanks I'll try the serialization package out.
Brandon
I had a few problems working with the JSON package (kept getting SO errors), so I went ahead with storing/retrieving objects via XML. I would recommend using XML to store objects if you can't get the JSON package to work--it's pretty intuitive.
bedwyr
+1  A: 

This used to be called "checkpointing" (although that usually means saving the state of the entire system). Have you considered serializing your object to some intermediate format, and then creating a constructor that can accept an object in that format and re-create the object based on that? That might be a more straightforward way to go.

TMN
A: 

Check out the flash.utils.IExternalizable interface. It can be used to serialize ActionScript objects into a ByteArray. The resulting data could easily be written to disk or used to clone objects.

Note that this is not "automatic". You have to manually implement the interface and write the readExternal() and writeExternal() functions for each class you want to serialize. You'll be hard pressed to find a way to serialize custom classes "automatically" because private members are only accessible within the class itself. You'll need to make everything that you need serialized public if you want to create an external serialization method.

joshtynjala
A: 

The closest I've come to this is using the appcorelib ClassUtil to create XML objects from existing objects (saving the xml manually) and create objects from this xml. For objects with arrays of custom types it takes configuring ArrayElementType Metadata tags and compiler options correctly as described in the docs.

ClassUtil.createXMLfromObject(obj);
CreateClassFromXMLObject(obj,targetClass);
Brandon
A: 

If you're using AIR, you can store Objects in the included local database.

Here's a simple example using local SQLite database on the Adobe site, and more info on how data is stored in the database.

Niko Nyman