Serializing enums in FDS

Java 5.0 introduced typesafe enums which are a special type of class. enums in other languages are typically just an ordered set of properties, however, Java enums allow custom behaviors and logic to be added through constructors, fields and methods.

FDS is based on JDK 1.4.2 and does not natively support enums. ActionScript 3 does not have the concept of enums either. To FDS, an enum class definition looks like a normal class. As such, only public fields and public bean methods with get/set accessors are seen as properties.

An enum can be written to capture the selected value by making the class look like a normal value Object.


package food;

public enum FruitEnum
{
    APPLE("apple"),
    ORANGE("orange"),
    BANANA("banana");

    FruitEnum(String value)
    {
        this.value = value;
    }

    public String value;
}

While this doesn’t provide round trip support for enums, it does allow Java to ActionScript conversions of enums.

9 Responses to Serializing enums in FDS

  1. Anish Bansal says:

    Hi Pete,The example above has public visibility for enum member variables. In this case, the deserialization works perfectly fine. But if I keep the visibility as private with just the getters (or for that matter, keep the visibility as public but make the member variables final), deserialiation fails.Is there any solution/workaround to make the deserialiation work correctly with private variables and no setters?Thanks in advance,Anish

  2. Pete says:

    Note that because these types are treated like Objects – normal AMF3 serialization rules apply. That is, you can use bean accessors instead of member variables, but you need to have a public get and set method (as read-only properties are not sent by default).

  3. Vijay Ganesan says:

    I get an error from FDS when trying this with a RemoteObject invoking a Java method that takes an enum (com.foo.EnumTest):”flex.messaging.MessageException: Unable to create a new instance of type ‘com.foo.EnumTest’. Types cannot be instantiated without a public, no arguments constructor.at flex.messaging.util.ClassUtil.createDefaultInstance(ClassUtil.java:135)at flex.messaging.io.AbstractProxy.createInstance(AbstractProxy.java:84)Am I missing something?ThanksVijay

  4. Vijay Ganesan says:

    Correction to my previous post – I had my Java method taking an Enum arg and retuning an Enum. The error was in the client-to-server direction. The reverse direction works as Pete’s blog correctly states.Vijay

  5. Serge says:

    Hi,So is there another solution to create semi safe enums using normal classes?We tried something like the following but its not working either.public class MediaType {public static final MediaType IMAGE = new MediaType(“IMAGE”);public static final MediaType AUDIO = new MediaType(“AUDIO”);public static final MediaType VIDEO = new MediaType(“VIDEO”);private final String myName; // for debug onlyprivate MediaType(String name) {myName = name;}public String toString() {return myName;}Thanks,Serge

  6. Pete,Please take a look at our take over this problem:http://flexblog.faratasystems.com/?p=242I would like to know your opinion :)VS

  7. I think we’re looking to just return enums as Strings for now due to the lack of enums in AS3. This is a pretty simple solution and should solve 80%+ of user’s issues.

  8. It shouldn’t be necessary for [RemoteClass] metadata to be in because we don’t use it at runtime. We only use this metadata at compile-time to generate the calls to flash.net.registerClassAlias().If you’re trying to determine a Class’s alias at runtime then you can get this from the XML information returned by flash.utils.describeType() .Note that if you don’t want to deal with this XML directly, you can rely on Flex’s mx.utils.ObjectUtil.getClassInfo() method which will return the class alias in a property called “alias” on the info Object returned (although that is not documented in Flex 2).

  9. flexuletz says:

    I do have the same problem as Vijay. In the server-client direction everytihng works just fine, but not in the client-server one. I do have getters/setters for the value of the enumeration,and i even declared it public, does not work.Found a possible solution on Valery Silaev;s blog at http://flex.sys-con.com/read/429520_2.htm.But I do not know where to get the AMFEndpoint class from in order to extend it. I do have it specified in my services-config.xml, but I can’t find it in my granite.jar, as I’m using graniteDS.does any one have any solution?Correction to my previous post – I had my Java method taking an Enum arg and retuning an Enum. The error was in the client-to-server direction. The reverse direction works as Pete’s blog correctly states.Vijay