In an effort to learn AJAX and Javascript Object Notation (JSON), I am converting the CFJSON encoder into something that will fit into my architecture and standards; that's not meant to be snooty, I just have certain coding standards (such as capitalization and white-space utilization) that I like to follow. One of the things that I ended up changing was the conversion of the ColdFusion query to Javascript. The conversion is very interesting. Inherently, CFJSON converts the ColdFusion query into a mirrored Javascript object of arrays. In ColdFusion, if you refer to a query via structure notation, you have to put the column name first, then the row:
Launch code in new window » Download code as text file »
This seems unnatural to me. I never liked it in ColdFusion and I don't care for it in Javascript. And so, I have converted my ColdFusion queries into Javascript arrays of structures:
Launch code in new window » Download code as text file »
This seems a much more natural representation of Javascript data types; however, when I dumped out the results, I can see that not only do they perform differently, they look very different. Take of example the following query:
Launch code in new window » Download code as text file »
When converted via CFJSON, you get:
Launch code in new window » Download code as text file »
When converted through my ColdFusion ToJavascript() method, you get:
Launch code in new window » Download code as text file »
My version, while more natural, must transport much more data since the column names are repeated for every "row" of the query. I don't like all the additional transfer, but I do like the way I handle the query conversion. For the moment, I am going to keep my method. I am at peace since I will most likely not be handling such large amounts of data and the size difference should be insignificant. But I can certainly see that with larger data sets, this will quickly add up and become very large.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Print Page
Ben,
Did you modify the CFJSON file. I tend to not want to modify the core files to any framework I use (if I didn't write it).
Joe
Posted by Joe Simes on Apr 10, 2007 at 1:46 PM
@Joe,
With something like this, I tend to write things from the ground up. I am HUGE fan of reinventing the wheel. Plus, I have very specific formatting rules that I like to follow so modifying someone else's file tends to irk me a bit.
Of course, writing from the ground up or any modifications for that matter are much more prone to error :)
Posted by Ben Nadel on Apr 10, 2007 at 11:12 PM