Using Custom Type Marshallers in BlazeDS

In my previous post I pointed to the BlazeDS classes that need to be replaced in order to prevent ActionScript Number.NaN from turning into Long or Integer zeroes on the MessageBroker side. The recommendation boiled down to re-jaring flex-messaging-core.jar or placing the modified classes somewhere earlier in the application server’s classpath. If neither option is allowed, you may configure your endpoint with the custom type marshaller, like the one below:


        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
           <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" 
           class="flex.messaging.endpoints.AMFEndpoint">
           </endpoint>
           <properties>    
               <serialization>
                   <type-marshaller>clear.messaging.io.amf.translator.NullkeepingASTranslator</type-marshaller>
               </serialization>
           </properties>    
       </channel-definition>

In BlazeDS 4 you will base your type marshaller on flex.messaging.io.ASTranslator:
public class NullkeepingASTranslator extends
    flex.messaging.io.amf.translator.ASTranslator
{
    private static final NumberDecoder numberDecoder = new NumberDecoder();  
    public Object convert(Object source, Class desiredClass)
    {
      if ( DecoderFactory.isNumber(desiredClass)){
        return numberDecoder.decodeObject(source, desiredClass);
      }else{
        return super.convert(source, desiredClass);
      }
    }
}

This route does not require neither server-level deployment nor modification of the flex-messaging-core.jar.

I’d like to use this opportunity and invite Flex developers living in Europe to attend our Advanced Flex Master Class in Brussels, Belgium on March 1 and 2, 2010.

Victor Rasputnis