views:

418

answers:

2

I need to serialize and unserialize (is that even a word?) an array in AS3, so it can be sent as a string.

The only problem is that it doesnt just contain text, it contains objects.

Is it possible to serialize and unserialize arrays in AS3 like you can in PHP? How can I do so?

Once this bug is fixed, all will be well with my program.

EDIT: I need to be able to sync an array across several flash files, which are connecting via TCP and can send simple things like strings and integers, so I need to be able to transmit data about this in one go, in string form.

A: 

If your goal is to communicate between clients over TCP, then simply serialize the objects with AMF using writeObject method on your socket. I'm not sure if this is at odds with your "in string form" requirement, as it is more of a binary format for efficiency.

Edit: Write the object to a ByteArray and then Base64 encode it to produce a string. This would be similar to how some web services deal with binary data, since it has to be places into a string format that places nicely with xml, and base64 contains all printable characters.

You can even compress the bytearray before you encode it, to make is smaller for sending across the wire.

ByteArray: http://livedocs.adobe.com/flex/3/html/help.html?content=ByteArrays_2.html

There is a Base64 library attached in this forum thread, you can find other libraries on the internet: http://www.flexdeveloper.eu/forums/actionscript-3-0/compress-and-uncompress-strings-using-bytearray

AaronLS
Perhaps I need to clarify further...I am using a multiplayer API which uses TCP. I can only send strings and integers...
Cyclone
See edit, writeobject to bytearray, optionally compress if you want, then encode as base64 and you can work with it as a string.
AaronLS
A: 

maybe you should look at this page, its kind of similar with what you are trying to do

http://www.actionscriptdeveloper.co.uk/serializing-and-unserializing-svg-objects-to-arrays-in-actionscript-3/

ropox