ColdFusion 10 serializeJSON将yes/no字符串转换为布尔值——如何停止?

时间:2021-06-04 10:00:58

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:

我有一个存储过程,MS SQL Server,其中一个列返回的是字符串,“是”或“不是”。到目前为止还好。我正在ColdFusion 10中创建一个JSON字符串,并最终将其踢给jQuery/Bootstrap,并将其放入表中。如果我调用writeOutput(“SP合适的文本:”& spResults.rg_suitable_text[I]);在SP的输出上(这是一个计算值,而不是在TSQL中具有实际数据类型的实际列),它会写它应该写的内容(是或否)。然而,在构造一个序列化为JSON的数组时,rg_suitable_text=spResults。rg_suitable_text[i]等等,如果我为谷歌或Firefox使用一个REST客户端,只查看ColdFusion的原始JSON输出,它会显示true/false。我甚至尝试创建一个新的变量并进行硬编码:

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!"

把它加到我的数组中,但同样的事情发生了。看起来ColdFusion 11支持解决这个问题的方法,但是这是一个工作项目,所以这不是一个选项。是否有一个编辑我可以对上面的块说,“ColdFusion 10,我不管你想要什么,这是一个字符串,把它当作一个该死的字符串,丹吉特!”

3 个解决方案

#1


3  

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.

你可以把数据弄乱,这样ColdFusion就会认为它是一个字符串而不是一个布尔值,但这不是一个很好的方法。

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.

您基本上需要使用ColdFusion以外的东西来创建JSON字符串。ColdFusion一直充斥着JSON错误,几乎要求它的JSON提供不适合的目的。我认为发现的大多数bug都已经在CF11中修复,正如您所注意到的。

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.

我没有在prod中使用它,但是我在使用谷歌的GSON API从CFML数据构建JSON时取得了相当大的概念验证成功。

#2


3  

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

JsonSerializer。github上的cfc可以处理你的“这是一个字符串,把它当作一个被炸坏的字符串,当当!”请求。

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

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 ));

#3


0  

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:

我遇到了同样的问题,并决定修改我的SQL语句,它提取了“No”字符串值。一些类似的东西:

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.

然后从JSON中处理字符串值“NON”,就像处理“NO”一样。

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

这不是最优雅的解决方案,但在我的应用程序中似乎行得通。

#1


3  

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.

你可以把数据弄乱,这样ColdFusion就会认为它是一个字符串而不是一个布尔值,但这不是一个很好的方法。

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.

您基本上需要使用ColdFusion以外的东西来创建JSON字符串。ColdFusion一直充斥着JSON错误,几乎要求它的JSON提供不适合的目的。我认为发现的大多数bug都已经在CF11中修复,正如您所注意到的。

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.

我没有在prod中使用它,但是我在使用谷歌的GSON API从CFML数据构建JSON时取得了相当大的概念验证成功。

#2


3  

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

JsonSerializer。github上的cfc可以处理你的“这是一个字符串,把它当作一个被炸坏的字符串,当当!”请求。

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

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 ));

#3


0  

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:

我遇到了同样的问题,并决定修改我的SQL语句,它提取了“No”字符串值。一些类似的东西:

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.

然后从JSON中处理字符串值“NON”,就像处理“NO”一样。

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

这不是最优雅的解决方案,但在我的应用程序中似乎行得通。