Version 0.2

Corrected the escape function. Unlike XML, JSON does not have a CDATA capability. This makes escaping certain characters a requirement. Just like the toXML.QueryToXML() function there is an argument called 'cDataCols', which is a list of columns that are to use the CDATA element. In toJSON, this argument is used to escape those characters for the columns listed.

Related Blog Entries

Comments
Chris H's Gravatar Hey Doug!
first of all thanks for creating this CFC, saved me the troube of looking at how to create JSON data by myself or using cfjson. i was using toxml before for my Spry results, but switched to toJSON now.
i have a problem though, it seems Spry doesn't like quotes around numbers, as described here:
http://www.adobe.com/cfusion/webforums/forum/messa...
any chance for a fix for this? ;D
thanks for your hard work
# Posted By Chris H | 4/18/07 6:36 AM
Chris H's Gravatar oh, another thing:
how does one return the json results from a CFC? no matter what returntype is specify, i always get the "<wddxPacket version='1.0'><header/><data><string>" etc. in front of the actual content, which breaks Spry of course =\
# Posted By Chris H | 4/18/07 8:44 AM
Doug's Gravatar Hi Chris,
Thank you for trying toJSON. Actually, JavaScript in general doesn't like quote marks in data. To work around this convert the quote marks to HTML &quot;, then add this Spry script to the page to decode the column:

   <!--// decode columns -->
   function DecodeHTMLColumn(notificationType, notifier, data)
   {
      if (notificationType != "onPostLoad" || dsPO.getRowCount() < 1)
         return;
         
      var rows = ds.getData();
      var numRows = rows.length;
      for (var i = 0; i < numRows; i++)
      {
         var row = rows[i];
         if (row.MYCOLUMN)
            row.MYCOLUMN = Spry.Utils.decodeEntities(row.MYCOLUMN);
      }
   }
   dsPO.addObserver(DecodeHTMLColumn);

Be careful where you use this code. Because it is decoding encoded HTML and JavaScript, any field that is processed by it, could be subject to cross site script injection.

As for WDDX output; Change the returntype to a string and return the variable to a display page with this code:
<cfsetting showdebugoutput="false" />
<cfcontent type="text/html" ><cfoutput>#trim(request.json)#</cfoutput>

Or change the output type of the function to output="True" and use the above code in your function.

Hope this helps you.
Doug
# Posted By Doug | 4/18/07 9:13 AM
Chris H's Gravatar thanks for your response!
i got rid of the WDDX output using your suggestion, all i had to change was set the returntype to "any" instead of "string", since string still added the "<wddx etc." at the end of the return, while any does not!
i can't get the quotes problems working though. isn't there a way to build in isNumeric() into the cfc to decide whether to add quotes to the output or not?
thanks again!
# Posted By Chris H | 4/20/07 8:27 AM
Doug's Gravatar Hi Chris,
Sorry for not it explaining that better.
Please see my comments on your thread in the forum.
The code above is intended to keep quote marks and other HTML usable or displayed in the values. Not to make a string into a number. Also, to use it you need to change the MYCOLUMN to the name of your column.
# Posted By Doug | 4/20/07 10:07 AM
Chris H's Gravatar ah yes, works great now! thanks again Doug!
# Posted By Chris H | 4/23/07 3:37 AM
Dominic Watson's Gravatar Great work Doug thanks. Had me up and running in 2 minutes without having to learn JSON! maybe one day...
# Posted By Dominic Watson | 12/31/07 12:50 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006.