The Flex SerializationFilter Class
Posted in Flex on January 15th, 2010 by admin – 1 CommentThe other day while digging through the API docs for the HTTPService class, I noticed the addition of a new property called serializationFilter. This piqued my interest. I clicked the property name to get the more detailed explanation. Here’s what it says:
Provides an adapter which controls the process of converting the HTTP response body into ActionScript objects and/or turning the parameters or body into the contentType, URL, and and post body of the HTTP request. This can also be set indirectly by setting the resultFormat by registering a SerializationFilter using the static method: SerializationFilter.registerFilterForResultFormat(“formatName”, filter)
If you can’t already see how this could be useful let me give you an example. Have you ever had to work with a service that returned JSON? You probably did something like this.
<mx:HTTPService url="/path/to/json/data" result="resultHandler(event);" />
<mx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import mx.rpc.events.ResultEvent;
private function resutlHandler(event:ResultEvent):void
{
var result:Object = JSON.decode(event.result as String);
// Do something with the result
}
]]>
</mx:Script>
This method is fine and it works well for a single service. But what if you have multiple JSON services? You’ll end up having to decode the result string in every additional result handler you have. Not a big deal but wouldn’t it be nice if the HTTPService could deserialize the results for us like it already does for XML? This is where the SerializationFilter comes in. By subclassing the SerializationFilter and overriding the deserializeResult method we can encapsulate the JSON decoding process and free ourselves from the repetitive task. Here’s a simple JSONSerializationFilter class.
package org.tylerchesley.serialization
{
import com.adobe.serialization.json.JSON;
import mx.rpc.http.AbstractOperation;
import mx.rpc.http.SerializationFilter;
public class JSONSerializationFilter extends SerializationFilter
{
public function JSONSerializationFilter()
{
super();
}
/* Overriden Methods */
override public function deserializeResult(operation:AbstractOperation, result:Object):Object
{
return JSON.decode(result as String);
}
}
}
There’s two ways to use a custom SerializationFilter class. You can set the serializationFilter property of your service with an instance of your custom
SerializationFilter. For example:
<mx:HTTPService url="/path/to/some/json/data">
<mx:serializationFilter>
<tc:JSONSerializationFilter />
</mx:serializationFilter>
</mx:HTTPService />
Or you can use the static registerFilterForResultFormat method like this.
<mx:Script>
<![CDATA[
import org.tylerchesley.serialization.JSONSerializationFilter;
import mx.rpc.http.SerializationFilter;
private function init():void
{
SerializationFilter.registerFilterForResultFormat("application/json", new JSONSerializationFilter());
}
]]>
</mx:Script>
By using the latter method any HTTPService object with the content type set to “application/json” will automatically use our new JSONSerializationFilter.

