Categories
Flex

Flex ActionScript Object and Java Object Serialization

Few days back while working on Flex and Java Webservices, I came across an issue which eventually became worst nightmare. The issue got even worst when we couldn’t find any suitable solution to overcome it. The issue was related to ActionScript value object to Java Object Serialization.

Some Details about Issue
Using Action Script Value objects while passing data to Remote Object is considered to be best practice and provides much more flexibility over passing data using parameter binding style.  Lets have a look at the following sample code

var myobj:MyObject = new MyObject();
myobj.property1 = “hello”;
myobj.property2 = “world”;

…..

myRemoteObject.callSomeMethod(myobj); //Pretty easy huh.

Also when you get data from RemoteObject’s Result Event, you can easily typecast it to desired type and use it in code. This helps you to use OOPS style programming in ActionScript and deal with real time problems using Objects. Behind the scene, Flex requires the ActionScript Value objects to be tagged with [RemoteClass(alias=”…”)] tag to identify corresponding Java Object on server side.

While above example seems to be pretty easy, lets get into somewhat complex Value object having collection as its property.

[RemoteClass(alias=”com.pkg.java.JavaObject”)]
class MyComplexObject{
   private var _property1:String;
   private var _property2:ArrayCollection;

  //..setter and getter methods go here.
}

While above Class has one property with simple type as String and other has type of ArrayCollection. In our case, the Remote Object was returning an instance of  “JavaObject” with following structure.

class JavaObject{
   private String _property1;
   private List<MyClass> _property2;

  //..setter and getter methods go here.
}

The equvivalent ActionScript class for “MyClass” was created with following structure

[RemoteClass(alias=”com.pkg.java.MyClass”)]
class MyClass{
  //..property definition goes here.
}

It looks pretty much clear that all the ActionScript and Java Value objects are defined properly with correction metadata information. As I said the RemoteObject in our case was returning and instance of  “JavaObject”, we faced a weird issue. Somehow the _property2 of “MyComplexObject” was not able to bind collection using instance of “MyClass” and instead the data was displayed inside “Object” class.

It was real frustrating when similar objects were working in other modules. But after spending hours in finding the solution a small trick did the magic. (I am not sure whether to call it trick, but it worked.)

 

How we resolved the issue
At the time of serializing Java Object to ActionScript objects, Flex uses getClassByAlias(“aliasname”) to find out corresponding ActionScript Object. The default registration of Class is done when class is explicitly referenced either by declaring its type or by instantiating it.

In our case, there was neither an instance of MyClass was declared nor any variable was declared with type of MyClass in ActionScript. This caused the Class to remain not loaded in memory and hence the alias was not registered. Further causing no serialization of ActionScript object to Java.

To overcome this issue we created a dummy variable with type of MyClass in ActionScript and it worked as expected. The other option is to use registerClassAlias which does explicit registration of Class using alias.

It took hours to find out the real issue, hope this article will be useful for others.

By Carbon Rider

Hi this is Yogesh, welcome to my world. Being passionate about learning new technologies and building frameworks, I end up spending most of my time in front of computer. But over last few months, I realised apart from being good coder and designer (Yes I am) I have something hidden in me. And thats called ART.

5 replies on “Flex ActionScript Object and Java Object Serialization”

Hi Monika,

If you are facing the similar problem that we faced, there is nothing much you have to do. You have to just make sure that the Java Object which is not getting serialized to Custom Action Script class, is defined with dummy variable of the Action Script class before calling remote object method. Make sure that you have added correct meta data information in Action Script class. If you are still not able to get the code working, send me the code and problem details at [yogesh.jadhav [at] gmail [dot] com]

Hi Sankar,
I didn’t understand “With out explicit map…”. Are you talking about using metadata at class level like [RemoteAlias]. The class won’t serialize without this metadata tag.
Please provide more details about your requirement.

Leave a Reply