Posted By

narkisr on 05/29/08


Tagged

actionscript serialization Flex


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

taboularasa
THEPWN3R
goliatone


Action script to string serialization and de-serialization


 / Published in: ActionScript 3
 

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).

  1. package
  2. {
  3. import flash.utils.ByteArray;
  4. import mx.utils.Base64Encoder;
  5. import mx.utils.Base64Decoder;
  6.  
  7. public class SerializationUtils {
  8. public static function serializeToString(value:Object):String{
  9. if(value==null){
  10. throw new Error("null isn't a legal serialization candidate");
  11. }
  12. var bytes:ByteArray = new ByteArray();
  13. bytes.writeObject(value);
  14. bytes.position = 0;
  15. var be:Base64Encoder = new Base64Encoder();
  16. be.encode(bytes.readUTFBytes(bytes.length));
  17. return be.drain();
  18. }
  19.  
  20. public static function readObjectFromStringBytes(value:String):Object{
  21. var dec:Base64Decoder=new Base64Decoder();
  22. dec.decode(value);
  23. var result:ByteArray=dec.drain();
  24. result.position=0;
  25. return result.readObject();
  26. }
  27. }
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: etfd on January 21, 2010

Thanks for the post! Very useful! I did have to modify the code a bit for it to work however (I kept getting "end of file" errors when I got to the result.readObject() line. Hope this is helpful to others... -David

here's the code

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.encodeBytes( bytes );            
        return be.toString();           
    }

    public static function readObjectFromStringBytes(value:String):Object{          

        var dec:Base64Decoder=new Base64Decoder();          
        dec.decode(value);          
        var result:ByteArray= dec.toByteArray();
        result.position=0;          
        return result.readObject();         
    }
Posted By: hopewise on February 10, 2010

No matter what I do, I keep having this error:

RangeError: Error #2006: The supplied index is out of bounds. at flash.utils::ByteArray/readObject()

I am dispertly looking for a solution ..

Posted By: jschatz1 on November 5, 2010

Here is the correct code: This eliminates the end of file error you were getting: public static function serializeToString(value:Object):String { var bytes:ByteArray = new ByteArray(); bytes.writeObject(value);

            bytes.position = 0;
            var be:Base64Encoder = new Base64Encoder();
            be.encodeBytes(bytes)
            return be.drain();
        }

        public static function readObjectFromStringBytes(value:String):Object
        {
            var dec:Base64Decoder = new Base64Decoder();
            dec.decode(value);
            var result:ByteArray = dec.toByteArray();
            result.position = 0;
            return result.readObject();
        }

Posted By: jschatz1 on November 5, 2010

That got messed up but you get the idea. The drain grabs anything in the buffers which we don't have anything (but you might). Yeah that be.encode(bytes.readUTFBytes(bytes.length)); is the part that really kills it. because it's not UTF you want.. You want the actual bytes. And You really need dec.toByteArray() because you want a byteArray not a string. You never even wanna deal with strings because as soon as you do, the bytes in your ByteArray is no longer accurate because they have been converted to something else. And that's the whole point of Base64 anyway: to take what you can't read (the actual characters of the bytes (in 8 bits), with all sorts of none ASCII characters, and turn them into readable ASCII characters (6 bits) and slide the extra 2 bits over to the next byte.)

You need to login to post a comment.