Recipe 20.15 Receiving Typed Objects with ColdFusion

20.15.1 Problem

You want to be able to handle typed object parameters (i.e., properties of an object that store data of a custom type) sent to a ColdFusion back end (a CFC function or a CFM page).

20.15.2 Solution

Work with the parameter as an ASObject. Use the get( ) method to get the values for the properties, and use the getType( ) method to get the registered class name.

20.15.3 Discussion

Typed objects are automatically converted to ASObject datatypes in ColdFusion. ASObject is a Java class that extends the java.util.HashMap class (meaning all the properties and methods of a HashMap will also work for an ASObject) that also adds the getType( ) and setType( ) methods. If you are not yet familiar with how to work with Java objects in ColdFusion, you can read more about this in the ColdFusion documentation here:

Here is an example of a snippet of ActionScript code that creates a typed object and sends it to a ColdFusion service function:

function MyClass(a, b) {
  this.a = a;
  this.b = b;

Object.registerClass("MyClass", MyClass);

// Send a new MyClass object to a ColdFusion service function.
myCFService.serviceFunction(myResponseObj, {param: new MyClass("eh", "bee")});

Here is an example of a ColdFusion snippet that extracts the values from properties named a and b from an ASObject named param and also retrieves the object's type:

<!--- aVal = eh, bVal = bee, objTypeName = MyClass --->
<cfset aVal = param.get("a")>
<cfset bVal = param.get("b")>
<cfset objTypeName = param.getType(  )>

20.15.4 See Also

Recipe 20.14 and Recipe 20.18

    Part I: Local Recipes
    Part II: Remote Recipes