Marshall and unmarshall arrays in a standard way.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4336 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-08-21 23:31:13 +00:00
parent d57d213904
commit 8996d8da20
@@ -7,17 +7,28 @@ import flash.system.ApplicationDomain;
import flash.utils.ByteArray;
import flash.utils.Endian;
import com.threerings.io.TypedArray;
/**
* Utility methods for transferring flash properties via
* the presents dobj system.
*/
public class FlashObjectMarshaller
{
public static function encode (obj :Object) :ByteArray
public static function encode (
obj :Object, keepArrays :Boolean = false) :Object
{
if (obj == null) {
return null;
}
if (!keepArrays && obj is Array) {
var src :Array = (obj as Array);
var dest :TypedArray = TypedArray.create(ByteArray);
for each (var o :Object in src) {
dest.push(encode(o, true));
}
return dest;
}
// TODO: Our own encoding, that takes into account
// the ApplicationDomain
@@ -28,11 +39,20 @@ public class FlashObjectMarshaller
return bytes;
}
public static function decode (bytes :ByteArray) :Object
public static function decode (encoded :Object) :Object
{
if (bytes == null) {
if (encoded == null) {
return null;
}
if (encoded is TypedArray) {
var src :TypedArray = (encoded as TypedArray);
var dest :Array = [];
for each (var b :ByteArray in src) {
dest.push(decode(b));
}
return dest;
}
var bytes :ByteArray = (encoded as ByteArray);
// TODO: Our own decoding, that takes into account
// the ApplicationDomain