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.

14 thoughts on “Returning ColdFusion 9 ORM objects with JSON

  1. In this case a query really wouldn’t work as I really needed the parent object as well as the associated items in the array (in order to pre-fill select boxes and things without having to make additional AJAX calls for something like getUserRoles( userID ) ).

  2. Henry Ho

    Maybe it is the lazy loading that gets in your way? Try using HQL to fetch all properties, or see doc on how to disable lazy loading of your one-to-many property.

  3. I thought about the lazy loading issue, but the main property that I needed was already set to disable lazy loading in the persistent CFC. I even did a manual getRoles() on the object right before I returned it out of my RemoteFacade.cfc method to make sure those related objects had been grabbed. On top of that, the CF debugger clearly showed that they existed in the object one line before I returned it.

  4. Erik

    This was actually working fine for me in 9.0

    After upgrading to 9.0.1 it broke. But it’s not entirely broken. It returns only the first 2 of the persistent objects, the rest in my array of results are empty strings.

  5. @Bob That’s a handy thing to know for Flash Remoting type connections. However, in my case, I was returning the objects via JSON to an AJAX call from the page. Since they specifically list Flash Remoting as the connection type this applies to, I’m not sure this would solve the issue with JSON.

  6. Thanks Bob. That’s great to know. Unfortunately, I’ve moved on from the project that inspired this post and am not working for that particular client any longer. I’ll definitely keep that option in mind though the next time I need that specific functionality again.

  7. Jared Shields

    I found the same issue…. What’s happening is that Adobe fixed a prior bug that caused infinite recursion when serializing persistent entities. This was due to the bi-directional nature of common persistent entities. The way they did this was by creating a list of class names for the objects that have been serialized (as it’s serializing)… So when an object is in the list more than twice, it stops serializing it…. and returns an empty object instead…. This is a HUGE bug in CF.

    Im not sure if I’ll be making my fix for it directly in the Java source (albeit, not the legal way of doing it), or unfortunately having to do it all in CF (certainly a MUCH slower way of going about it)….


  8. Henry Ho

    @Honey No it is NOT fixed.

    CF 9.0.2:


    CC is MISSING!

Leave a Reply

Your email address will not be published. Required fields are marked *