views:

804

answers:

2

Hello

what i'm trying to do is walk an object that is also a complex tree of objects and output the actionscript 3 code it took (or takes) to create instantiate and populate that object and all its children.

so for instance if you saw something like this in your debugger

myObjectToParse (ParseMe@173e239)
----------[0]someBlob (SomeBlob@173e322)|null
----------[1]someChildren (Array@173e239)
--------------------[0]childFoo (ChildFoo@123e239)
------------------------------someProperty (Number@173e239) | 45
------------------------------someOtherProperty (Number@173e239) | 45
--------------------[1]childBar (ChildFoo@123e239)
------------------------------someStringProperty(String@173e239) | "hello world"

The exporter would spit out:

public class MySerialized extends ParseMe
{
    public var someBlob:SomeBlob;
    public var someChildren:Array;

    public function MySerialized()
    {
        //populateSomeBlob(); //its null so the export doesn't write it out    
        populateSomeChildren();    
    }

    private function populateSomeChildren()
    {
        someChildren=new Array(2);
        var childFoo:ChildFoo=new ChildFoo();
        childFoo.someProperty=45;
        childFoo.someOtherProperty=45;
        childFoo.someProperty=45;
        childFoo.someOtherProperty=45;

        var childBar=new ChildBar();
        childBar.someStringProperty="hello world";

        someChildren[0]=childFoo;
        someChildren[1]=childBar;
    }
}

Any tips? Ideas? Thanks!

Edit: I think i may end up just trying to serialize to an amf ByteArray...sounds simpler, but i haven't played with and don't know how many levels deep it can go and what types of references it supports.

+1  A: 

flash.utils.describeType() might help ;)

Cay
+3  A: 

in general, this is not possible, i am afraid ... for example, you have readonly properties ... also, some classes' constructor expects initialization values (although i personally think this is not very clever in most cases, but ok) ... also, some objects may be instances of private/internal classes, so the generated instantiation code will throw errors ...

you can however make a solution for objects, that do not have these problems ...

ok, basically, flash.utils.describeType will be of a lot of help ... that'll give you all the fields of an object (also tell you, whether it has read only fields etc.), as well as the class name ...

this will help you iterating through an objects properties ... for dynamic objects (describeType will tell you), you will additionaly have to loop over the properties using for-in loops ... use * as type for the iteration variable, that way it'll also work with Dictionarys for example ...

then you need to treat specific built-in types specially ...

  • XML: use XML::toXMLString ... this'll give you the base for an XML literal ...
  • Array: loop through it, and build an array literal
  • Number,int,uint,Boolean: use the standard string representation for a literal
  • String: remember to escape the string (newlines, backslashes etc.) ... easiest would be "unescape('"+escape(yourStringValue)+"');" ... you can of course create a normal String literal with escape sequences ... simplest way would be com.adobe.serialization.JSON.serialize(yourStringValue) (using as3corelib)
  • Date: use something like "new Date("+yourDateValue.getTime()+");" ... although it is questionable, whether it really makes sense to harcode dates ...
  • DisplayObject and subclasses: this will not be trivial, if you really want that ... you will need to find a way to properly rebuild the display list hierarchy ...
  • IEventDispatcher and implementors: this is quite impossible ... there is no way to find out the handlers registered to a IEventDispatcher, plus you'll be having a hard time generating ActionScript from function objects (quite impossible) ... you will have to choose a recursive approach ... please note that if you want to support circular references, this'll be more difficult ... you might wanna take that decision before you design your algorithm ...

hope this helps ...

back2dos
Note that shapes drawn in the Flash IDE can't be retrieved by AS3... :(
Cay