Best JSON option for folks not running ColdFusion 9

This post is more than 2 years old.

For folks who have done a lot of AJAX work in ColdFusion, the ability to natively generate JSON from a CFC request has been a godsend. However, there have been many issues with the serialization of certain values. A good example of this is values like "011" turn into "11.0", which is unacceptable. There have been changes (both good and bad) in this area since 8 was released, with the very latest version of ColdFusion getting it near perfect. (And I don't mean ColdFusion 901, but ColdFusion 901 with the hot fix applied.) But what about folks not using ColdFusion 9 yet?

Chris emailed me earlier this week with exactly that issue. I recommended two options:

In the past, I've made use of cfjson by Jehiah Czebotar and Thomas Messier. I believe more than one of my open source projects make use of it. However, it hasn't been updated since February of 2008. Because of that, I also suggested Chris take a look at jsonutil by Nathan Mische. Nathan is a darn smart guy (he runs ColdFire) and his code was a good year more fresh. I asked Chris to let me know which he used and how it worked. He ended up using using jsonutil and reported that it worked great.

Not sure how many other people out there are on ColdFusion servers prior to 8 but if you have any alternate suggestions, speak up!

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Mark Andrachek posted on 11/3/2010 at 10:41 PM

I work on an ajax heavy cf8 application. We needed json compatible with extjs, we needed it to be very fast, and we needed it to not munge our data.

The problem with most of these approaches is that there is generally a performance tradeoff.

Enter Jackson (http://http://jackson.codehaus.org/).

Jackson is a java json encoding/decoding library. It's very fast. With about 500 lines of custom java code, we have a query converter that rivals the native json encoding in terms of performance, doesn't munge our data, and allows us to flexibly alter additional metadata, so we can implement things like paging easier.

Comment 2 by Gary F posted on 11/3/2010 at 10:51 PM

I craft the json response by hand. If you know the response will always consist of a query with 3 columns, for example, then I code this:
<cfsavecontent variable="json">
{"myData": [
<cfoutput query="result"> ["#firstname#","#lastname#",#id#], </cfoutput>
]}
</cfsavecontent>
<!--- output saved content and remove the final unnecessary comma --->
<cfoutput>#Reverse(Replace(Reverse(json),",","","one"))#</cfoutput>

It's minimalistic and I have to customise it to each ajax request, but it works very well. Of course I add a bit of code to handle errors and empty queries, but it's compatible with older versions of CF.

Comment 3 by johans posted on 11/3/2010 at 10:54 PM

One mentioned by Barney Boisvert from:

http://www.json.org/java/in...

myStruct = {
a = "to serialize",
b = 4,
c = [ 1, "two", "3" ]
};
json = createObject("java", "org.json.JSONObject").init(myStruct).toString();

Comment 4 by Gary F posted on 11/3/2010 at 11:01 PM

Mark touched on two good points. I add JSStringFormat() around data that could contain non-alphanumeric characters and do a replace() on quotes to escape them. I never get munged data.

I add another column in the json response to carry the total number of records in the query so my favourite jquery data table plugin (called DataTables funnily enough) can format it automatically in a table and page through the results. Nothing else is needed in the json response to achieve that.

Comment 5 by Dave Merrill posted on 4/27/2011 at 9:21 PM

@Mark Andrachek: Mark, is your Jackson-based serializer an available thing that other people can use? I assume it also handles other data types, yes? I've modified jsonutil to do what we need, but a java solution is likely to perform better.

Thanks