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?
我有一个以JSON格式存储的数据结构,使用serializeJSON函数进行转换。我遇到的问题是,可以在CF中使用布尔值的字符串,如Yes、No、True和False,会被转换为JSON作为布尔值。下面是示例代码。有什么预防的办法吗?
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
6 个解决方案
#1
4
I believe that your or any similar "string forcing" workaround is the only possible way to prevent such behavior in Adobe CF for now.
我相信您或任何类似的“字符串强制”解决方案是目前在Adobe CF中防止此类行为的唯一可能方法。
BTW, Railo works as expected with your example. Here is the output:
顺便说一句,Railo在您的示例中工作的很好。这是输出:
Yes
{"STR":"Yes"}
Yes
It is also works same way for the numbers with trailing zeros.
它同样适用于末尾为0的数字。
#2
4
Adding an extra space in the string to prevent it from being converted to boolean, then trim at a later stage.
在字符串中添加一个额外的空间,以防止它被转换成布尔值,然后在稍后的阶段进行修剪。
#3
1
it's hacky, but if you conditionally output yes and no as "_yes_" and "_no_" (using a switch statement and then after serialising the JSON to a string, do a search and replace, it works.
这很简单,但是如果您有条件地输出yes和no作为“_yes_”和“_no_”(使用一个switch语句,然后将JSON序列化为字符串后,进行搜索和替换,它就可以工作了。
raw_json=serializeJSON(object);
raw_json=ReplaceNoCase(raw_json,':"_Yes_"',':"Yes"',"ALL");
raw_json=ReplaceNoCase(raw_json,':"_No_"',':"No"',"ALL");
at least CF is consistantly frustrating with this, true & false get converted to yes no when you round trip the data
至少CF对这一点感到非常沮丧,当您往返数据时,真与假转换为yes no
#5
0
I know this answer would not have worked when the question was asked, but as this seems to be the one that people are finding when researching this issue, I thought it would be good to update with a new fix.
我知道当有人问这个问题的时候,这个答案是行不通的,但是当人们在研究这个问题的时候,我觉得这是一个很好的更新。
For those on CF2016, Adobe has implemented a new function to help fix this issue. This would be preferable to adding a space to the front of strings, though that would still need to be the work around for releases before CF2016 as described by Sergii.
对于CF2016的用户,Adobe已经实现了一个新的功能来帮助解决这个问题。这将比在字符串前面添加空格要好,尽管这仍然需要在Sergii所描述的CF2016之前完成发布工作。
在CF2016为结构体:
Use the struct function, setMetadata, to specify the metadata.
使用struct函数setMetadata来指定元数据。
The metadata is a struct where each key is struct key and the value of each key specifies the information on how to serialize in JSON.
元数据是一个结构体,其中每个键都是结构键,每个键的值指定如何在JSON中序列化的信息。
The value of the key can be a string or a struct.
键的值可以是字符串或结构。
Value as string
值作为字符串
metadata = {firstname: "string"}};
元数据= { firstname:“字符串”} };
Value as struct
值作为结构
metadata = {firstname: {type: "string"}};
元数据= {firstname: {type: "string"};
Example:
例子:
<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// changing the default serialization by specifying the type of "firstname" as string
metadata = {firstname: {type:"string"}};
example.setMetadata(metadata);
writeoutput(SerializeJSON(example));
</cfscript>
For Queries in CF11+: Adobe reports that they've fixed this issue.
对于CF11+中的查询:Adobe报告他们已经修复了这个问题。
#6
-2
I'd try javacasting it: key = javacast("string", "yes")
. That should force CF to recognize it as a string rather than as a boolean.
我将尝试javacasting它:key = javacast(“string”,“yes”)。这将迫使CF将其识别为字符串,而不是布尔值。
#1
4
I believe that your or any similar "string forcing" workaround is the only possible way to prevent such behavior in Adobe CF for now.
我相信您或任何类似的“字符串强制”解决方案是目前在Adobe CF中防止此类行为的唯一可能方法。
BTW, Railo works as expected with your example. Here is the output:
顺便说一句,Railo在您的示例中工作的很好。这是输出:
Yes
{"STR":"Yes"}
Yes
It is also works same way for the numbers with trailing zeros.
它同样适用于末尾为0的数字。
#2
4
Adding an extra space in the string to prevent it from being converted to boolean, then trim at a later stage.
在字符串中添加一个额外的空间,以防止它被转换成布尔值,然后在稍后的阶段进行修剪。
#3
1
it's hacky, but if you conditionally output yes and no as "_yes_" and "_no_" (using a switch statement and then after serialising the JSON to a string, do a search and replace, it works.
这很简单,但是如果您有条件地输出yes和no作为“_yes_”和“_no_”(使用一个switch语句,然后将JSON序列化为字符串后,进行搜索和替换,它就可以工作了。
raw_json=serializeJSON(object);
raw_json=ReplaceNoCase(raw_json,':"_Yes_"',':"Yes"',"ALL");
raw_json=ReplaceNoCase(raw_json,':"_No_"',':"No"',"ALL");
at least CF is consistantly frustrating with this, true & false get converted to yes no when you round trip the data
至少CF对这一点感到非常沮丧,当您往返数据时,真与假转换为yes no
#4
#5
0
I know this answer would not have worked when the question was asked, but as this seems to be the one that people are finding when researching this issue, I thought it would be good to update with a new fix.
我知道当有人问这个问题的时候,这个答案是行不通的,但是当人们在研究这个问题的时候,我觉得这是一个很好的更新。
For those on CF2016, Adobe has implemented a new function to help fix this issue. This would be preferable to adding a space to the front of strings, though that would still need to be the work around for releases before CF2016 as described by Sergii.
对于CF2016的用户,Adobe已经实现了一个新的功能来帮助解决这个问题。这将比在字符串前面添加空格要好,尽管这仍然需要在Sergii所描述的CF2016之前完成发布工作。
在CF2016为结构体:
Use the struct function, setMetadata, to specify the metadata.
使用struct函数setMetadata来指定元数据。
The metadata is a struct where each key is struct key and the value of each key specifies the information on how to serialize in JSON.
元数据是一个结构体,其中每个键都是结构键,每个键的值指定如何在JSON中序列化的信息。
The value of the key can be a string or a struct.
键的值可以是字符串或结构。
Value as string
值作为字符串
metadata = {firstname: "string"}};
元数据= { firstname:“字符串”} };
Value as struct
值作为结构
metadata = {firstname: {type: "string"}};
元数据= {firstname: {type: "string"};
Example:
例子:
<cfscript>
example = structnew();
example.firstname = "Yes";
example.lastname = "Man";
// changing the default serialization by specifying the type of "firstname" as string
metadata = {firstname: {type:"string"}};
example.setMetadata(metadata);
writeoutput(SerializeJSON(example));
</cfscript>
For Queries in CF11+: Adobe reports that they've fixed this issue.
对于CF11+中的查询:Adobe报告他们已经修复了这个问题。
#6
-2
I'd try javacasting it: key = javacast("string", "yes")
. That should force CF to recognize it as a string rather than as a boolean.
我将尝试javacasting它:key = javacast(“string”,“yes”)。这将迫使CF将其识别为字符串,而不是布尔值。