Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a Stored Procedure, MS SQL Server, one of the columns returned is a string, "yes" or "no." So far, so good. I'm creating a JSON string in ColdFusion 10 and will eventually be kicking that out to jQuery/Bootstrap to put in a table. If I call writeOutput("SP suitable text: " & spResults.rg_suitable_text[i]); on the output of the SP, (and this is a computed value, not an actual column with an actual datatype in TSQL,) it writes what it should, ie yes or no. However, in constructing an array to be serialized as JSON, rg_suitable_text=spResults.rg_suitable_text[i] and so on, if I use a REST client for Google or Firefox and view just the raw JSON output from ColdFusion, it shows true/false. I even tried creating a new variable and hard-coding:

var solicit="No";
if(spResults.rg_suitable_text[i] EQ true OR trim(spResults.rg_suitable_text[i]) EQ "true")
{
    solicit="Yes";
}

and tagging that onto my array, but the same thing happens. It looks like ColdFusion 11 supports a way to get around this, but this is a work project, so that's not an option. Is there an edit I can make to the above block that would say, "ColdFusion 10, I don't care what you want, this is a string and treat it like a blasted string, dangit!"

like image 697
Janet Avatar asked Aug 07 '14 14:08

Janet


2 Answers

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.

like image 50
Adam Cameron Avatar answered Nov 10 '22 14:11

Adam Cameron


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 ));
like image 3
Henry Avatar answered Nov 10 '22 14:11

Henry