If you are using json.cfc ... [solution to a possible error]
If you are using the Coldfusion component json.cfc here is a little patch for it to fix the following problem.
json.cfc is at http://www.epiphantastic.com/cfjson/
I am pulling data from a database in response to an Ajax request and using json.cfc to encode it before returning it to Ext. I convert the data from a query to an array of structures then pass that to the encode() method of json.cfc.
Now, it is important for my Ext application that numeric data base fields come through as numerics and strings as strings etc ie that data type is maintained. So, I am using json.cfc.encode() in it's default state ie no quotes around numbers so Ext stores them with the correct data type when they arrive in the json from the server.
Here is the "bug" in json.cfc:
It is legal for users to enter numerics in one of my text database fields ie "1111" is a valid entry. Json.cfc is returning that as a number instead of a string. ie without the quotes. In my Ext app I am doing an indexOf() on that field (looking for specific data) and of course since it was returned as 1111 not "1111", Ext (javascript) thinks it is a number and gives an error when I do the indexOf() since that is a String method that I am applying to a numeric field.
So.... I have modified json.cfc to check for that situation. Here are my changes:
First, I added the following as a generic function in case it is of use in the future:
// Return the underlying java field type
function getJavaDataType(_data){
var jtyp = _data.getClass().GetName();
if (listLen(jtyp,'.') eq 3) {
jtyp = ListGetAt(jtyp,3,'.');
}
return jtyp;
}
Then I changed the encode method as follows:
{etc...}
In this way, a string field that just happens to contain a valid number will be returned with quotes in the json so Ext will know it is a string, not a number.
Cheers,
Murray
#If you have any other info about this subject , Please add it free.# |