Return to Snippet

Revision: 6546
at May 29, 2008 12:28 by narkisr


Initial Code
package
{   
    import flash.utils.ByteArray;
    import mx.utils.Base64Encoder;
    import mx.utils.Base64Decoder;
   
    public class SerializationUtils {
        public static function serializeToString(value:Object):String{
            if(value==null){
                throw new Error("null isn't a legal serialization candidate");
            }
            var bytes:ByteArray = new ByteArray();
            bytes.writeObject(value);
            bytes.position = 0;
            var be:Base64Encoder = new Base64Encoder();
            be.encode(bytes.readUTFBytes(bytes.length));
            return be.drain();
        }
       
        public static function readObjectFromStringBytes(value:String):Object{
            var dec:Base64Decoder=new Base64Decoder();
            dec.decode(value);
            var result:ByteArray=dec.drain();
            result.position=0;
            return result.readObject();
        }      
    }
}

Initial URL

                                

Initial Description
This snippet shows how to serialize action script object to strings, the serialization method is AMF based, note also that each object must meet three basic rules in order to be serialized properly: 
   ¨1. The constructor must take no arguments
   2. Fields must be public or they won’t be saved
   3. You must register it with a class alias by calling flash.net.registerClassAlias(aliasString, class).¨
(this is based upon http://www.partlyhuman.com/blog/roger/technique-storing-arbitrary-objects-in-html-links).

Initial Title
Action script to string serialization and de-serialization

Initial Tags
actionscript, Flex

Initial Language
ActionScript 3