DOJO/JSON requests in ColdFusion

Recently I was putting together a sample app for my presentation at AjaxWorld, and I decided to use the DOJO ajax library and ColdFusion together. However, there was one problem, DOJO does everything with JSON requests/response, instead of url/form variables and XML and it does it in such a way that you can't easily get to it from CF.

What I mean by that, DOJO will send the JSON request arguments in a string, in the body of the HTTP get request. Now this is perfectly valid in HTTP, however it is rare. And it means that the values don't end up in a FORM or URL variable either. So getting the JSON string can be tricky.

So I've put together a simple UDF that you can use to get this JSON request, technically anything in the http body of a request. Soon I'll put this on cflib, but for now, here it is. Hope it helps.

(Special thanks to Christian Cantrell, the byte array code is from his blog).

<cffunction name="getJSONRequest" access="private" returnType="string" output="no">
   <cfscript>
      var size=GetPageContext().getRequest().getInputStream().available();
      var emptyByteArray = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray();
      var byteClass = createObject("java", "java.lang.Byte").TYPE;
      var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, size);

      GetPageContext().getRequest().getInputStream().readLine(byteArray, 0, size);

      createObject('java', 'java.lang.System').out.println("{GetJSONRequest} ByteArray.ToString=" &ToString( byteArray    ) );   
      return ToString( byteArray );
   </cfscript>
</cffunction>

<cfset jsonstring = getJSONRequest()>

Comments
Andrea Veggiani's Gravatar Cool, thanks for the code.
# Posted By Andrea Veggiani | 10/2/06 12:51 PM
Andrew Powell's Gravatar Mike -

Here is the CFJSON CFC Implementation:

http://www.infoaccelerator.net/client/index.cfm/20...
# Posted By Andrew Powell | 10/2/06 6:22 PM
Rob Gonda's Gravatar Mike, could you benchmark your snippet against cfjson? Your code seems to rely in built in Java components, which should run faster, and in the other hand you're initializing a bunch of objects, which could slow it down... I would be interested to see performance comparisons.

Thanks!

~Rob
# Posted By Rob Gonda | 10/2/06 9:06 PM
Mike Nimer's Gravatar There is nothing to compare with CFJSON, this is a UDF to pull the json string out of the HTTP body. You still need something like CFJSON to decode the string into an object.

So the better example would be:
<code>
<cfset jsonobject = cfjsonDecode( getJSONRequest() )>
</code>

Now a json decoder writing in straight java would probably be faster. Just look at the CF7 function toScript(), when it's creating an "actionscript" string that is almost the same as a json string (curly brackets, etc..). So the speed of toScript() is a good comparison of a java udf vs a cfml udf. I did notice that it was a lot slower to encode a large result set with JSON, can't remember the library I used, then creating XML with cfsavecontent and using xml instead. So writing something native is probably worth the effort.

I wish I knew about JSON when I wrote toScript() in CF7 I would of structured the arguments a little differently and add json support too. Guess we'll have to convince the CF team to add native json support as a new function ;)
# Posted By Mike Nimer | 10/3/06 12:53 AM
Rob Gonda's Gravatar Thanks Mike; man, I need some sleep. It makes sense now.
# Posted By Rob Gonda | 10/3/06 7:42 AM
Dan G. Switzer, II's Gravatar @Mike:

You can also snag the content by using the native getHttpRequestData() function--which returns a struct. In the struct is a key called "content" which should contain any text delivered in the body of the HTTP request.

You really don't need Java at all--which some users may not be able to use due to Sandboxing.
# Posted By Dan G. Switzer, II | 10/3/06 10:22 AM
Jeff Knooren's Gravatar Adobe has a <a href="http://labs.adobe.com/wiki/index.php/ActionScript_...; of AS3 functions that serialize JSON.
# Posted By Jeff Knooren | 10/3/06 6:46 PM
# Posted By video indir | 7/27/08 8:51 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.001.