Calendar

««Nov 2009»»
SMTWTFS
1234567
89
10
11121314
15
16
17
18192021
2223
24
25262728
2930

Alert Email

Get a short email alert whenever a new entry is published.

Confidential, secure it's piece of cake to keep uptodate.

CFFUNCTION gets JSON support

Published: 10:02 AM GMT, Friday, 13 February 2009

In addition to creating new features to add to the richness of CFML, we've also been working hard to plug some of the incompatibility holes that exist. One such hole that has been released into the nightly builds is the ability for remote CFC's to return back different format types.

The most common role that people are using remote CFC's is within Javascript client side applications with the JSON format being king in terms of moving data around.

We've made the necessary additions to the engine to support this access method to your CFC's.

JSON

This includes the ability for a CFC to return data back in either JSON, PLAIN, WDDX or XML formats. If you opt for JSON then you can secure your JSON by inserting a prefix to it that can be set using the standard attributes from the CFAPPLICATION/Application.cfc mechanisms.

This feature will be part of the 1.0.2 release, but you can play with it now by downloading the nightly builds.

We've now got all the pieces in place for us to release CFAJAXPROXY very soon. The additions that came part of CFJAVASCRIPT allows us to keep the amount of Javascript code we implant into the body of the HTML page to a complete minimum.

Comments (4)

Sorry about that--some of the code output got truncated. Hope that makes sense though. Best way to test things is just grab OpenBD and play around with it a bit. Let me know if you need further assistance.

left by Matt Woodward . Thursday, 23 July 2009 2:30 PM

Hi Allen--JSON with OpenBD is pretty straightforward. Assuming you have a query called "myQuery", you can serialize it to JSON using the SerializeJSON() function:

<cfset myJSON = SerializeJSON(myQuery) />

You would then return the myJSON variable from your function to return the JSON version of the query to the caller.

You can also control whether or not you want the query serialized by its columns, which by default it is not. To enable this, just pass a boolean of true as the second function argument: <cfset myJSON = SerializeJSON(myQuery, true) />

This will change the JSON output from having full records in each element like so (made-up output here ...): {"COLUMNS":,"DATA":[[1,"matt@mattwoodward.com"],]}

And instead will have all the values for each column as the elements: {"ROWCOUNT":2,"COLUMNS":,"DATA":{"ID":,"EMAIL":}}

You can also force everything to lowercase using the final argument of SerializeJSON: <cfset myJSON = SerializeJSON(myQuery, false, true) />

Which would result in: {"columns":,"data":[[1,"matt@mattwoodward.com"],]}

Also note that numeric data is NOT converted to a float (e.g. 1 does not become 1.0) as it is in ColdFusion, which in my opinion is a good thing. ;-)

You can read more here: http://wiki.openbluedragon.org/wiki/index.php/SerializeJSON()

Hope that helps!

left by Matt Woodward . Thursday, 23 July 2009 2:29 PM

Hi there,

Would it be possible to post a working example of how to return a query with JSON? Does this work the same way as with CF8?

Thanks, Allen

left by Allen Souliere . Monday, 20 July 2009 8:58 AM

Hi There,

Would it be possible to post an example of the usage of returning a JSON packet? I am eager to use ajaxCFC with JSON for transporting data from my CFC's back to my clients, but I have no idea where to start.

Thanks, Allen

left by Allen Souliere . Monday, 20 July 2009 8:14 AM
Add Comment