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!
Archived Comments
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.
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.
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();
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.
@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