Converting boolean YES to true

390 views
Skip to first unread message

Steve Onnis

unread,
Aug 4, 2011, 2:39:59 AM8/4/11
to cfau...@googlegroups.com

I have been coming across this issue more and more when dealing with JavaScript and JSON which is ColdFusion changing true|false to YES|NO values.

 

For example:-

 

<cfset foo = true />

<cfoutput>#foo#</cfoutput> = true

 

<cfset foo = !foo />

<cfoutput>#foo#</cfoutput> = NO

 

Now take this JSON string for example

 

{“foo”:#foo#} would then become {“foo”:NO} which then screws everything up

 

I posted a feature request on the adobe site to have a toBoolean() function which would return only true or false based on the argument

 

So then we would be able to have...

 

{“foo”:#toBollean(foo)#} which would output as {“foo”:false}

 

What are the thoughts here.  I was very surprised to see the lack of this function in the library actually.

 

Also can anyone one recommend a way to do this now? I tried JavaCast(“Boolean”, foo) but that didn’t work.

 

Steve

Zac Spitzer

unread,
Aug 4, 2011, 2:55:41 AM8/4/11
to cfau...@googlegroups.com
It's an annoying bug in Adobe CF, Railo works as expected

http://stackoverflow.com/questions/1867728/how-can-i-prevent-serializejson-from-changing-yes-no-true-false-strings-to-boolea/6661460#6661460

z

> --
> You received this message because you are subscribed to the Google Groups
> "cfaussie" group.
> To post to this group, send email to cfau...@googlegroups.com.
> To unsubscribe from this group, send email to
> cfaussie+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cfaussie?hl=en.
>

--
Zac Spitzer
Solution Architect / Director
Ennoble Consultancy Australia
http://www.ennoble.com.au
http://zacster.blogspot.com
+61 405 847 168

Barry Beattie

unread,
Aug 4, 2011, 3:01:47 AM8/4/11
to cfau...@googlegroups.com
"Also can anyone one recommend a way to do this now? I tried
JavaCast(“Boolean”, foo) but that didn’t work."

have you seen this?

http://www.coldfusionmuse.com/index.cfm/2010/2/5/Booleans.and.Coldfusion

Also, have you confirmed you have a true boolean? What I'm getting at
is the difference between

True/False/Null (tri-state)
True/Absence of True (boolean)

The last one is important because you just have to check to see if the
value is there.

these two CFLib functions may help

http://cflib.org/udf/booleanize
http://cflib.org/udf/forceBoolean

Steve Onnis

unread,
Aug 4, 2011, 3:02:25 AM8/4/11
to cfau...@googlegroups.com
Wrong way

I want it to be true|false

Steve Onnis

unread,
Aug 4, 2011, 3:04:03 AM8/4/11
to cfau...@googlegroups.com
And that is dealing with the structures itself. I am dealing with single
variables as per my example. No json conversion or anything.

I want !true to give me false, not NO

-----Original Message-----
From: Zac Spitzer [mailto:zac.s...@gmail.com]
Sent: Thursday, 4 August 2011 4:56 PM
To: cfau...@googlegroups.com
Subject: Re: [cfaussie] Converting boolean YES to true

Paul Kukiel

unread,
Aug 4, 2011, 3:12:36 AM8/4/11
to cfau...@googlegroups.com
Its odd that !true is NO but cant you just use if/else to accomplish this till(if) Adobe fix this?

Paul
Paul Kukiel

Steve Onnis

unread,
Aug 4, 2011, 3:13:38 AM8/4/11
to cfau...@googlegroups.com
I can write a UDF so i don't need to download them from elsewhere. In any
case im dealing with cfmodule and i don’t want to have to keep including
stuff all over the place. It really should be built in.

The calculation is like this

<cfset somevar = false />
<cfset foo = (!somevar) AND true />

The above will give me "YES" as the result where i want it to give me
"true". This is the issue


I have gotten around it with
<cfset foo = IIF((!somevar) AND true, "true", "false") />

It is a bit of a hack but i don’t really like it.


-----Original Message-----
From: Barry Beattie [mailto:barry....@gmail.com]
Sent: Thursday, 4 August 2011 5:02 PM
To: cfau...@googlegroups.com
Subject: Re: [cfaussie] Converting boolean YES to true

Steve Onnis

unread,
Aug 4, 2011, 3:14:52 AM8/4/11
to cfau...@googlegroups.com

Yeah it can be fixed with if/else but should it be?  We want to write “less” code, not more...true ? or maybe it is YES J

Paul Kukiel

unread,
Aug 4, 2011, 3:18:42 AM8/4/11
to cfau...@googlegroups.com
I agree Steve !true should be false but I'm not sure if there is an easier way until ( if ) this ever gets addressed by Adobe.

Paul

Chong

unread,
Aug 4, 2011, 3:30:35 AM8/4/11
to cfau...@googlegroups.com
TBH you would have to fix it yourself, I can't see Adobe fixing it soon as it is likely a legacy issue, I would imagine if they change how it works to the way we expect it should, any code using specifically the old behaviour would error ... so they are probably stuck till they can agree on an elegant solution or give clients enough warning...

Barry Beattie

unread,
Aug 4, 2011, 3:33:24 AM8/4/11
to cfau...@googlegroups.com
so, what's !YES ... is it NO?

is anything that can be construed as a YES (True, 1, etc) considered a
YES and therefore the opposite is NO?

> --
> You received this message because you are subscribed to the Google Groups
> "cfaussie" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/cfaussie/-/-amD6_MZprcJ.

Mark Mandel

unread,
Aug 4, 2011, 3:40:52 AM8/4/11
to cfau...@googlegroups.com
#(value ? "true" : "false" )#

There you go.

CF is dynamic, so true is YES is 1... if you want static values, use a static language. There are pros and cons both ways.

Mark
--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

cf.Objective(ANZ) + Flex - Nov 17, 18 - Melbourne Australia
http://www.cfobjective.com.au

AJ Mercer

unread,
Aug 4, 2011, 3:49:49 AM8/4/11
to cfau...@googlegroups.com
heads up - this is CF9 ( Railo 3.3 code)

On 4 August 2011 15:40, Mark Mandel <mark....@gmail.com> wrote:
#(value ? "true" : "false" )#

There you go.

CF is dynamic, so true is YES is 1... if you want static values, use a static language. There are pros and cons both ways.

Mark
-- 

AJ Mercer
<webonix:net strength="Industrial" /> | <webonix:org community="Open" />
http://twitter.com/webonix

Steve Onnis

unread,
Aug 4, 2011, 3:52:47 AM8/4/11
to cfau...@googlegroups.com

They can leave it as it is.  If they add a toBoolean() function then the existing functionality can remain but we can use this function to actually give a proper Boolean value

 

From: Chong [mailto:kck...@gmail.com]
Sent: Thursday, 4 August 2011 5:31 PM
To: cfau...@googlegroups.com
Subject: Re: [cfaussie] Converting boolean YES to true

 

TBH you would have to fix it yourself, I can't see Adobe fixing it soon as it is likely a legacy issue, I would imagine if they change how it works to the way we expect it should, any code using specifically the old behaviour would error ... so they are probably stuck till they can agree on an elegant solution or give clients enough warning...

--

You received this message because you are subscribed to the Google Groups "cfaussie" group.

To view this discussion on the web visit https://groups.google.com/d/msg/cfaussie/-/-amD6_MZprcJ.

Dennis Clark

unread,
Aug 4, 2011, 6:24:57 AM8/4/11
to cfau...@googlegroups.com
The issue here is not one of static vs. dynamic languages. JavaScript is dynamic but doesn't have this difficulty with booleans that Adobe CF has.

The problem is due to two characteristics of Adobe CFML:
  1. Boolean is not a true primitive type.
  2. Boolean operators return the string "YES" for true and "NO" for false.
Most people who encounter problems with booleans in conversions between CF and other representations blame (2) because it is immediately noticeable, but the true root cause of the problem is (1). The lack of a primitive boolean type means that there will always be some values that will be handled incorrectly in conversions between CFML and representations with real booleans like JSON.

Adobe doesn't make it obvious that boolean is not a primitive type in CFML. CFML has standard boolean operators and constants, but if you play with them enough you will find that there is no true "true" value in CF.

Code:

<cfscript>
foo = true;
bar = True;
baz = TRUE;
</cfscript>
<cfoutput>
foo = #foo#<br>
bar = #bar#<br>
baz = #baz#<br>
</cfoutput>

Output under Adobe CF 9.0.1:

foo = true
bar = True
baz = TRUE

Languages with boolean primitives treat 'true' as a single literal value. In the above example the values of the 3 ColdFusion variables were converted to 3 different strings, so they could not have been set to the same literal value even though they are all supposed to be "true". It looks like CF implicitly converts the barewords "true" and "false" to strings and preserves their original case.

Adding a new primitive type (even one as simple as boolean) to a programming language is no small thing. If it ever arrives in Adobe CF it would almost certainly be part of a major release. Then and *ONLY* then should Adobe add a toBoolean() function.

In the meantime, I've written myself a simple UDF to handle issue (2):

string function trueFalseFormat(boolean booleanValue required, string trueString="true", string falseString="false") {
return arguments.booleanValue ? arguments.trueString : arguments.falseString;
}

With this I can use #trueFalseFormat(val)# to get a JSON boolean or #trueFalseFormat(val,1,0)# to get a SQL boolean (which is rare but sometimes necessary).

Note that none of this should be a problem when writing ColdFusion code that does not interface to other systems. If your code accepts a boolean value as an argument or return value, you should not write your code to expect the value to be exactly "TRUE" or "FALSE" or "YES" or "NO". If you need the value for conditional logic, use it as is. If you need to output the value, then you should convert it to the desired format first.

-- Dennis
Reply all
Reply to author
Forward
0 new messages