views:

206

answers:

5

I have a data struct being stored in JSON format, converted using the serializeJSON function. The problem I am running into is that strings that can be boolean in CF such as Yes,No,True,and False are converted into JSON as boolean values. Below is example code. Any ideas on how to prevent this?

Code:

<cfset test = {str='Yes'}>
<cfset json = serializeJSON(test)>
<cfset fromJSON = deserializeJSON(json)>

<cfoutput>
    #test.str#<br>
    #json#<br>
    #fromJSON.str#
</cfoutput>

Result:

Yes
{"STR":true}
YES
+3  A: 

I believe that your or any similar "string forcing" workaround is the only possible way to prevent such behavior in Adobe CF for now.

BTW, Railo works as expected with your example. Here is the output:

Yes
{"STR":"Yes"}
Yes

It is also works same way for the numbers with trailing zeros.

Sergii
We are on CF8 at the moment. Do you know how it is converted in CF9?
Dan Roberts
@Dan Knowing the background of legacy code support by Adobe, I could bet that default behavior not changed yet. But I've checked it for you: no changes. Also manual shows that there are no new arguments can overridde it yet:http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-79fa.html
Sergii
In CF9,0,0,251028 it is {"STR":true}
Leigh
@Sergii. Whoops. I did not see your response.
Leigh
Thanks for confirming in CF9
Dan Roberts
A: 

Append a space to the end. It sounds weird but it works.

<cfset test = {str='Yes }>
<cfset json = serializeJSON(test)>
<cfset fromJSON = deserializeJSON(json)>
Henry
Actually this _is_ the workaround mentioned by author.
Sergii
Ya, I realized after I posted.
Henry
A: 

I'd try javacasting it: key = javacast("string", "yes"). That should force CF to recognize it as a string rather than as a boolean.

Bialecki
didn't change result in my testing
Dan Roberts
A: 

Thanks- Prefixing with space is an easy solution and it worked.

Jim