11 Replies Latest reply on Mar 16, 2016 11:02 AM by Zolotoj

    How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]

    Phoenix_Guru Level 1

      Hi,

       

      I am calling a web service with the resultFormat set to e4x. When i get the result, it is in XML format. What I need is to convert this to a custom type Value Object (which is actually a class generated from WSDL). I was not finding a way to do this. So I used a SimpleXMLDecoder to decode it to a generic Object. Now my problem is that all the nested objects inside this generic Object are of type "ObjectProxy". So i cannot convert them to my nested value objects.

       

      So now what I finally need is either of the following 2 things:

       

      1. A way to convert the ObjectProxy to my custom type Value Object.

      Or

      2. A way to directly parse the result XML to my Custom type Value Object.

       

      Please let me know if I need to provide some more clarification.

       

      Please help.. This one is really killing me now....

       

      Thanks in advance!!

        • 2. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
          neoX286 Level 1

          I know the original poster for this question probably won't get this, but that's not the intention of this post...I just spent the last 6 hours trying to find the answer to the question: how do I convert an ObjectProxy to my Object?

           

          Well here's the answer, for any other googlers that find this post (as it's on the first page of results for 'cast objectproxy to object')

           

          Let's say you have a service call that returns a generic Object type.  In SOAP, you might see the resulting xml tag as '<AnyType/>'.  In flex though, it will pretty much always convert this to an ObjectProxy class.  You'll find that getting the object out isn't exactly straightforward, and casting it back to the type you want it to be also isn't either.

           

          Here's how you do it, and it's actually REALLY simple once you know what you have to do:

           

          First, you have to convert the ObjectProxy to an Object type.  To do so, you must use the 'object_proxy' namespace itself.  Here's the code to do that

           

                    import mx.utils.ObjectProxy;

                    import mx.utils.object_proxy;

           

            var myObjectProxy:ObjectProxy = new ObjectProxy(myStronglyTypedObject);

            var obj:Object = myObjectProxy.object_proxy::object;

           

          That code right there will take 'myObjectProxy' and pull out the original object into a general Object variable (NOTE: the object reference is read only according to the docs.)  That's only half of the battle though, as then you still need to cast your object back.  I have read in serveral sources that you can use an 'as MyType' method to convert the object from there, but I could not get that to work.  Traditional casting (i.e. MyType(obj)) only resulted in a runtime error as well.  These may work for you if you proxied your object from an existing flex object, but they will NOT work if you pulled it from a service call response's ObjectProxy.  Either way though, the solution is just as short to type in the case that you pulled it from a service call response:

           

                    import com.adobe.serializers.utility.TypeUtility;

           

                                                            obj = com.adobe.serializers.utility.TypeUtility.convertToStrongType(obj,MyType);

           

          (if you can't access that method you probably need to add the serializers.swc from the framework to your build path:

          Adobe Flash Builder 4.5 Root Directory\eclipse\plugins\com.adobe.flexbuilder.project_4.5.1.313231\dcradSwcs\4.5\libs\s erializers.swc

           

          This will convert your object to the type you want it to be.

          AND YOUR DONE.  No more searching for the full answer folks, and no more converting your service calls to e4x instead.

           

           

          Now, if you want to know why this works but casting doesn't stick around...if you don't care, don't waste your time reading the rest of this:

           

          Why is it that your other service calls can parse the xml data from a SOAP response to any predefined type from the SOAP service, but casting the proxied version to that type does not work?

          Your service calls are dynamically generated from the SOAP data, so obviously flex doesn't just 'know' how to do that...and your SOAP objects do extend the Object class...so what gives?

           

          If you look at the dynamically generated web service code from flex, you will see that each service call is told what the result type should be.  Then the entire AbstractOperation (your client-side web service class) is given a convertResultHandler function that grabs the data from the result and converts it to the type defined by the service.  You won't get very far just looking at the generated code though, it's far to abstracted.  But if you look at the serializers.swc compiled code (you'll have to decompile it) you will get an idea of what it is that that handler function does.  That handler function basically takes it and checks if it's an ArrayCollection/Array or an Object and then defers conversion of the object/array to separate functions called 'convertToStrongType' and 'convertListToStrongType'.  These functions take in your object, a class to convert it to, and return the passed in conversion class.  Bingo.

           

          Now, you will see that you can take that object and use the convertToStrongType and convertListToStrongType functions from the serializers swc to perform the conversions you need.  The benefit of this is that these functions are the EXACT SAME FUNCTIONS that handle converting your objects from the webservice result, so you can pretty well bet you won't loose any data like you would with single level reflection utilities and such.

           

           

          As a side note of the usefullness of this.  If you have a C# back end web service that returns a List of Object type, meaning that you can toss in all kinds of different objects into the same list, then you can use this in conjuction with an object interrogation function to determine the objects type to perform your conversions.

           

          I REALLY hope this helps some other folks save themselves from pulling out their hair or wasting entire days trying to figure out the answer before switching to some off the wall setup that'll make your code more complex!

          • 3. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
            Omnipitence

            Hi I like the solution above but I thought I should point out that you can also get strongly typed conversion by adding the [RemoteClass(alias="my.class.package.Name")] metadata onto the class you want serialised/de-serialised.

            • 4. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
              Phoenix_Guru Level 1

              neo,

               

              Thanks a ton for your time. This is surely going to help people who are looking for this answer!

               

              Cheers,

              Guru

              • 5. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
                neoX286 Level 1

                Omnipitence wrote:

                 

                Hi I like the solution above but I thought I should point out that you can also get strongly typed conversion by adding the [RemoteClass(alias="my.class.package.Name")] metadata onto the class you want serialised/de-serialised.

                 

                Yea that's the other solution I had read about from a lot of people.  I had heard mixed responses with it though, but I didn't test it myself.  I would assume it should work, but the reason I didn't examine this too far is because I have had a lot of bugs with the Flash Builder code generator for WSDLs, and I didn't want to have to add that in every time I ran into a bug with it and had to delete my service and rebuild it again.  The main bug I have found is that, when refreshing your web service, Flash Builder often times takes some of your object classes and tosses them into a package named the same as your project in the root of your src directory rather than placing them were you originally placed them.  I.E. My objects are usually stored in Services.Objects, or Services.MyService.Objects, and each service is stored in Services.MyService.  When I refresh it some of the objects often get tossed in MyProject instead of Services.Objects.  If this weren't a bug I run into a lot (since I code both the front end and back end on a large flex based project), this would definitely be my solution.  Hopefully the next version of flash builder fixes this, as I rely heavily on the web service generator haha.

                 

                Thanks for adding that though, should work just fine for most people that aren't updating their web services 20x + a day haha.

                Phoenix_Guru wrote:

                 

                neo,

                 

                Thanks a ton for your time. This is surely going to help people who are looking for this answer!

                 

                Cheers,

                Guru

                 

                 

                Not a problem!

                • 6. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
                  MrHjsm1

                  I do really love you Neo! It works Awesome!, Thank you, you saved me a lot of time , just registered to thank you..

                  • 7. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
                    neoX286 Level 1

                    Haha, no problem, glad it helped out!

                    • 8. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
                      MichaelKarnerfors

                      Perfect answer Neo! This killed two problematic birds with one stone for me. +1 Like.

                       

                      /Michael

                      • 11. Re: How to convert ObjectProxy to Object [Nobody can answer this question??? :( ]
                        Zolotoj Level 3

                        Hey, hope you get this message.

                        In your code:

                        var myObjectProxy:ObjectProxy = new ObjectProxy(myStronglyTypedObject);

                        var obj:Object = myObjectProxy.object_proxy::object;

                        obj = com.adobe.serializers.utility.TypeUtility.convertToStrongType(obj,MyType);


                        Where is my actual Proxy class that I need to cast to a strong type?