ColdFire and a CF to JSON Gotcha

A new version of ColdFire is up on RIAForge. This is an extension only update that fixes a bug related to JSON serialization/deserialization of queryparam values.

The problem was reported and fixed by Shane Bradly. He noticed that he was not seeing the SQL for some of his queries in the ColdFire Query tab. His investigation found that passing a numeric value for a text queryparam type such as cf_sql_varchar or cf_sql_char caused the following JavaScript in the ColdFire extension to fail:

tmpVal = "'" + value.replace(/'/, "''") + "'";

The problem is a result of ColdFusion's implicit type conversion; a variable with a numeric value will get serialized to JSON as a Number, even if the variable should be a String. Numbers in JavaScript do not have a replace method so the above JavaScript would fail. The solution Shane came up with was to cast the value to a string for all text queryparam types. I modified his patch slightly and came up with the following fix:

tmpVal = "'" + value.toString().replace(/'/, "''") + "'";

It would be great if you could cast variables on the ColdFusion side using JavaCast(). As it stands now SerializeJSON does not respected the Java type of a variable as the following code demonstrates:

<cfset valToSerialize = JavaCast("String",123) />
<cfset class = valToSerialize.getClass() />
<cfset json = SerializeJSON(valToSerialize) />
Value: #valToSerialize#<br/>
IsNumeric: #IsNumeric(valToSerialize)#<br/>
IsSimpleVaue: #IsSimpleValue(valToSerialize)#<br/>
Class: #class#<br/>
JSON :#json#

This may not be possible as all simple values, numeric, string, or boolean, are stored as Strings in CF, so you generally want some type of implicit conversion to take place. Maybe SerializeJSON could have a strict parameter which forced it to honor the Java type of the variable?

Well, enough thinking out loud. This is just something else to look out for when using JSON with CF.

shag's Gravatar Thanks Nathan. I just installed your updated version, and it works as expected. Thanks for putting up with my e-mails.
# Posted By shag | 10/22/08 1:09 PM
Raymond Camden's Gravatar I've run into this myself a few times as well. You should log an ER for this (
# Posted By Raymond Camden | 10/22/08 2:27 PM
Nathan Mische's Gravatar @shag - Glad we got it working as expected, and thanks for tracking this one down!

@Raymond - I'll log an ER.
# Posted By Nathan Mische | 10/23/08 11:22 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.