I have some data that I have to return after the Ajax call. On my .cfc page I have the logic that converts the data. Each column is tab delimited and each row is separated with CRLR. I have my cffunction returnformat set to JSON. After I return the data I use JavaScript to separate columns and rows in desired format. While I was doing this I saw few blogs where people were talking about all kind of problems with JSON in ColdFusion 9. On my development site I use ColdFusion 10 and I haven't notice any problem in my data. But on the live site I have ColdFusion 9 and I was wondering if that will cause some problems. In my data I have fields like Address and this field allows all kind of characters. Return type JSON in coldfusion 9 has the problem if your data is like this for example 2131231D if combination of strings and integers ends with D JSON will not return the same data. So my question is what I should do in this situation? Is there any fix for this problem? Is JSON reliable and recommended to use in ColdFusion 9? I was looking also for some tool that will convert my data on .cfc page that way I can avoid cffunction returnformat='JSON'. Here is my code:
<cfcomponent>
<cffunction name="getData" access="remote" output="true" returnformat="JSON">
<cfset fncResults = StructNew()>
<cfif myFile = true>
<cfoutput query="myData">
//Here I organize my data
</cfoutput>
<cfset getList = arrayToList(buffer,crlf)/>
<cfset fncResults.status = "200">
<cfset fncResults.fileData = #getList#>
<cfelse>
<cfset fncResults.status = "400">
<cfset fncResults.message = "The file was not properly uploaded.">
</cfif>
<cfreturn fncResults>
</cffunction>
</cfcomponent>
Here is my JavaScript:
function getFile(){
$.ajax({
type: 'POST',
url: 'Components/test.cfc?method=getData',
data: new FormData($('#myForm')[0]),
cache: false,
dataType: 'json'
}).done(function(obj){
if(obj.STATUS === 200){
return obj.FILEDATA;
}else{
return false;
}
}).fail(function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
});
}
Keeping your development and production environments as similar as possible to each other is a best practice. It reduces these kinds of "gotchas" when you push code to Production.
ColdFusion 10's JSON serializer/deserializer is vastly improved over CF9's. If you're working with a lot of JSON, then upgrading to CF10 on Production is a good idea. Until that's a possibility, using a separate serializer can help.
I suspect the "json cfc" that @KevinB mentioned Ben Nadel's JsonSerializer.cfc. It's available on Github (forked version, with a few extra features) at https://github.com/kevindb/JsonSerializer.cfc