Coldfusion JSON序列化不一致
我编写了以下组件:
<cfcomponent>
<cffunction name="test" returntype="struct" access="remote" returnformat="json">
<cfset local.str = structNew()>
<cfset str.a = "hello">
<cfset str.b = 23>
<cfreturn local.str>
</cffunction>
</cfcomponent>
当我在开发环境中运行此组件时,我得到以下结果:
{"A":"hello","B":"23"}
在生产中,我得到此组件:
{"A":"hello","B":23}
相同的代码,相同的 CF 版本,相同的 JVM,不同的结果。有人知道为什么吗?
I wrote the following component:
<cfcomponent>
<cffunction name="test" returntype="struct" access="remote" returnformat="json">
<cfset local.str = structNew()>
<cfset str.a = "hello">
<cfset str.b = 23>
<cfreturn local.str>
</cffunction>
</cfcomponent>
When I run this in my dev environment I get the following:
{"A":"hello","B":"23"}
In production, I get this:
{"A":"hello","B":23}
Same code, same CF version, same JVM, different results. Anybody know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来这是CF9中的错误。不确定您如何从本地/生产版本中获得不同的结果。有关详细讨论,请参阅此博客文章:
http://coldfusion.tcs.de /adobe-please-fix-coldfusion-serializejson/
看起来您有四个选择。
It appears that this is bug in CF9. Not sure how you are getting different results from your local / production version. See this blog post for a detailed discussion:
http://coldfusion.tcs.de/adobe-please-fix-coldfusion-serializejson/
It looks like you have four options.
我以前注意到过类似的问题,但从未关注过机器/环境之间的确切结果。看起来我们在尝试将弱类型语言转换为强类型语言时遇到了麻烦。
需要检查的一些事项
CF 服务器的补丁或修补程序有什么差异吗?
每次运行的结果是否一致,或者即使在同一台机器上,结果也会偶尔发生变化吗?例如,如果您重新启动 CF,稍微更改文件(以强制重新编译),然后运行多次,第一次运行的结果与以后运行的结果是否不同?
如果您 var 范围 local.str 会发生什么变化吗?
另一个需要考虑的选择是使用 CFJSON 来序列化然后输出字符串,我认为它更加一致。我们使用它作为我们的主要应用程序(当前的维护者实际上是一位为我们做了一些工作的顾问)。
I've noticed similar issues before but never paid attention to the exact results between machine/environments. Just seems like we are asking for trouble trying to convert a weakly typed language to a strongly typed language.
A few things to check
Any differences in patch or hotfixes for the CF servers?
Are your results consistent on every run or do they change occasionally even on the same machine? For example if you restart CF, change the file slightly (to force recompile) then run it multiple times are the results different on the first run than some later runs?
Does anything change if you var scope local.str?
Another option to consider is using CFJSON, which I believe is much more consistent, to serialize then output the string. We use it our primary app (the current maintainer was actually a consultant that did some work for us).
如果您使用
cfscript
而不是cfset
设置Struct
的成员,您会得到不同的结果吗?例如:您也可以尝试使用 cfset 作为:
我还没有看到所描述的行为,可能这些方法之一会有所帮助。
另一种铸造方法。尝试一下怎么样:
If you set the members of the
Struct
usingcfscript
instead ofcfset
do you get a different result? e.g.:You might also try your
cfset
as:I've not seen the behavior described, possibly one of these approaches will help.
Yet another casting approach. How about trying:
看看这个。
任何尾随 D 或 F 后跟空格的完全数字字符串。
尝试反序列化它。
JSON 解析失败于字符 9:'D' in {"wtf":4D }
嗯什么?
解析器修剪并检查最后一个字符是否是 D 或 F(双精度或浮点)并将其剥离。如果其余字符是数字,则不添加引号。
如果你没有尾随空格,它就可以完美工作。数字、D|F 和空格的特定组合会使其出错。
(我已将其提交给 Adobe cfbugs)
Check this one out.
Any entirely numeric string with trailing D or F followed by whitespace.
Try deserializing it.
JSON parsing failure at character 9:'D' in {"wtf":4D }
Umm what??
The parser trims and checks if the last character is D or F (double or float) and strips it. If the remaining characters are numeric, no quotes are added.
If you dont have trailing whitespace, it works perfectly. The specific combo of number, D|F and spaces trips it up.
(I've submitted it to Adobe cfbugs)