Coldfusion 11 & serializedJSON issue
Copy link to clipboard
Copied
Hi
I wanted to upgrade our Coldfusion 10 to 11. We are using MongoDB as additional database, so we use Java MongoDB driver. And until this point everything is fine & everything works.
All data to Mongo and from Mongo are in JSON format. Problem we have is that serialization changed in CF 11 what broke all queries.
Here is an example:
CF10: { "VAL1" : 0 , "VAL2" : { "$ne" : false} , "VAL3" : true , "VAL4" : { "$gt" : 5}}
CF11: { "VAL1" : "0" , "VAL2" : { "$ne" : "false"} , "VAL3" : "true" , "VAL4" : { "$gt" : "5"}}
Problem is that 0 != "0", true != "true", 5 != "5". Mongo can't compare string with int or bool (same issue with floats)
When i put data to database have same issue. Something that was before integer or bool now is string.
I found a way to make it work Javacast('int',value), Javacast('boolean',value) but changing it in every place in big project will take far too much time. Is there a way to make SerializeJson work like it worked before? Why it changes types now?
Copy link to clipboard
Copied
add nno Application.cfc <cfprocessingDirective pageencoding="utf-8"> | |
<cfset this.serialization.preservecaseforstructkey = true> | |
<cfset this.serialization.serializeQueryAs = "struct"> |
Eder Rocha
www.ersolution.com.br
Copy link to clipboard
Copied
Already tried that. Didn't help.
Copy link to clipboard
Copied
I'm running the latest hotfix of CF11 and I'm getting different results than you are. Maybe they fixed something? Here is my program and results:
<cfset variables.s = {} />
<cfset variables.s["val_0"] = "i'm a string" />
<cfset variables.s["val_1"] = 0 />
<cfset variables.s["val_2"] = "0" />
<cfset variables.s["val_3"] = true />
<cfset variables.s["val_4"] = {} />
<cfset variables.s["val_4"]["$ne"] = false />
<cfset variables.s["val_5"] = {} />
<cfset variables.s["val_5"]["$gt"] = 5 />
<cfset variables.test = {} />
<cfset variables.test["s"] = variables.s />
<cfset variables.test["json"] = serializeJSON(variables.s) />
<cfdump var="#variables.test#" />
json = {"val_0":"i'm a string","val_1":0,"val_4":{"$ne":false},"val_5":{"$gt":5},"val_2":0,"val_3":true}
It's treating 0 and "0" as a number, but from my experience CF has always had this issue. The Boolean values are not quoted as in your example.
I'm running cf11-hotfix007.
Copy link to clipboard
Copied
You might also look into using the custom serializer capabilities in ColdFusion 11, as the built-in serializer still isn't 100% fit for purpose (as you've seen). Here are a few useful links:
Adobe's docs on custom JSON serializers (particularly the very bottom of the page): ColdFusion Help | RESTful Web Services in ColdFusion‌
Adam Cameron's exploration of using the custom JSON serializer feature: Adam Cameron's Dev Blog: ColdFusion 11: custom serialisers. More questions than answers
Ben Nadel's alternative JSONSerializer CFC: JsonSerializer.cfc - A Data Serialization Utility For ColdFusion
That CFC on GitHub: bennadel/JsonSerializer.cfc · GitHub
Ray Camden's take on Ben Nadel's CFC: Implementing custom JSON serialization for your CFCs | Raymond Camden's Blog
HTH,
-Carl V.

