ColdFusion 10 serializeJSON turning yes/no strings to boolean values--how to stop it? ColdFusion 10 serializeJSON turning yes/no strings to boolean values--how to stop it? json json

ColdFusion 10 serializeJSON turning yes/no strings to boolean values--how to stop it?


To answer your bottom line question (the last sentence of your post), the answer is: no.

You can mess around with the data so as to trick ColdFusion into thinking it's a string not a boolean, but that is not a very good approach.

You basically need to use something other than ColdFusion to create your JSON strings. ColdFusion has been rife with JSON bugs, pretty much asking its JSON offering not fit for purpose. I think most of the bugs found have been fixed in CF11 though, as you have noticed.

I've not used it in prod., but I had a reasonable amount of proof-of-concept success using Google's GSON API for building JSON out of CFML data.


JsonSerializer.cfc on github can process your "this is a string and treat it like a blasted string, dangit!" request.

http://www.bennadel.com/blog/2505-jsonserializer-cfc-a-data-serialization-utility-for-coldfusion.htm

Example code:

serializer = new lib.JsonSerializer()    .asInteger( "age" )    .asAny( "createdAt" )    .asDate( "dateOfBirth" )    .asString( "favoriteColor" )    .asInteger( "favoriteNumbers" )    .asString( "firstName" )    .asString( "lastName" )    .asString( "nickName" )    .exclude( "password" );tricia = {    FIRSTNAME = "Tricia",    LASTNAME = "Smith",    DATEOFBIRTH = dateConvert( "local2utc", "1975/01/01" ),    NICKNAME = "Trish",    FAVORITECOLOR = "333333",    FAVORITENUMBERS = [ true, 4.0, 137, false ],    AGE = 38,    CREATEDAT = now(),    PASSWORD = "I<3ColdFusion&Cookies"};writeDump(serializer.serialize( tricia ));


I ran into the same issue and decided to modify my SQL statement that was pulling the "No" string values. Something along the lines of this:

SELECT CASE WHEN <colname> = 'NO' THEN 'NON' ELSE <colname> AS <colname>, <colname2>, etc...

Then I handle the string value "NON" from the JSON as I would handle a "NO" case.

Not the most elegant solution, but it seemed to work in my application.