Twitter: raymondcamden


Address: Lafayette, LA, USA

Best JSON option for folks not running ColdFusion 9

11-03-2010 6,061 views ColdFusion 5 Comments

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!

Related Blog Entries

5 Comments

  • Mark Andrachek #
    Commented on 11-03-2010 at 1: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.
  • Gary F #
    Commented on 11-03-2010 at 1: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.
  • johans #
    Commented on 11-03-2010 at 1:54 PM
    One mentioned by Barney Boisvert from:

    http://www.json.org/java/index.html

    myStruct = {
    a = "to serialize",
    b = 4,
    c = [ 1, "two", "3" ]
    };
    json = createObject("java", "org.json.JSONObject").init(myStruct).toString();
  • Gary F #
    Commented on 11-03-2010 at 2: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.
  • Dave Merrill #
    Commented on 04-27-2011 at 12: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

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty