Returning ColdFusion 9 ORM Objects with JSON

Last week I was working on a client’s project that makes heavy use of ColdFusion 9’s ORM features. Everything we’d done with ORM up to that point had been going really well and I continue to be impressed by the amount of time ORM saves me in development. I had gotten to a point in the project where I needed to be able to use AJAX calls from jQuery to manage some of the data in the database. Based on my experiences using a RemoteFacade.cfc to feed data to a Flex app, I thought it should be pretty easy. Several hours later, I realized that my particular use case for this was anything but easy.

What I eventually found was, when trying to load an object using an AJAX call to RemoteFacade.cfc, any object that had a property that was an array of other objects was not returned to Javascript. It wasn’t even that it was sent across as an empty property. The property absolutely didn’t exist in the JSON returned by the CFC. I used the ColdFusion debugger to verify that the array existing in the object immediately prior to the <cfreturn>; statement and then examined the JSON return using Charles Proxy only to find out that those properties weren’t in the JSON.  All the normal string, numeric, date, etc type properties were returned just fine, but any properties composed of other objects were missing.

As near as I can tell, the culprit is the serializeJSON() method that ColdFusion uses to convert complex objects into JSON notation. It doesn’t seem to work correctly on ORM objects. To test my theory, I manually built a structure and inserted each simple property from the ORM object. I then created a key in the structure that was an array and manually populated several items in the array each with a structure of data. When I returned that from the CFC as JSON, the array and all the structures it contained came across the wire just fine.

I’m not sure if this is a bug in the serializeJSON() method or if serializeJSON() wasn’t meant to handle objects. Whatever the case, this issue leads to quite a bit of extra work if you need to return an object via JSON and include its properties that are composed of other objects. I’d definitely be interested in hearing from anyone who has successfully done this with ORM and AJAX to see if I’ve done something incorrectly in my code.

Leave a Comment