Coldfusion JSON序列化不一致

发布于 2024-12-23 08:28:06 字数 527 浏览 3 评论 0原文

我编写了以下组件:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

埖埖迣鎅 2024-12-30 08:28:06

看来这是CF9中的错误。不确定您如何从本地/生产版本中获得不同的结果。有关详细讨论,请参阅此博客文章:

http://coldfusion.tcs.de /adobe-please-fix-coldfusion-serializejson/

看起来您有四个选择。

  1. 修改代码以期望字符串而不是数字
  2. 将 CF 服务器回滚到 9.0
  3. 使用 Railo 而不是 Adob​​e CF
  4. 切换到不同的 JSON 序列化程序。显然有两个名为 CFJSON: http://cfjson.riaforge.org/ 和 Dan Roberts 的建议 http://www.epiphantastic.com/cfjson/

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.

  1. Modify your code to expect strings instead of numbers
  2. Roll back your CF server to 9.0
  3. Use Railo instead of Adobe CF
  4. Switch to a different JSON serializer. Apparently there are two named CFJSON: http://cfjson.riaforge.org/ and Dan Roberts' suggestion http://www.epiphantastic.com/cfjson/
宁愿没拥抱 2024-12-30 08:28:06

我以前注意到过类似的问题,但从未关注过机器/环境之间的确切结果。看起来我们在尝试将弱类型语言转换为强类型语言时遇到了麻烦。

需要检查的一些事项

  1. CF 服务器的补丁或修补程序有什么差异吗?

  2. 每次运行的结果是否一致,或者即使在同一台机器上,结果也会偶尔发生变化吗?例如,如果您重新启动 CF,稍微更改文件(以强制重新编译),然后运行多次,第一次运行的结果与以后运行的结果是否不同?

  3. 如果您 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

  1. Any differences in patch or hotfixes for the CF servers?

  2. 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?

  3. 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).

陈年往事 2024-12-30 08:28:06

如果您使用 cfscript 而不是 cfset 设置 Struct 的成员,您会得到不同的结果吗?例如:

<cfscript>
str.a = "hello";
str.b = 23;
</script>

您也可以尝试使用 cfset 作为:

<cfset str.b = Int(23)>

我还没有看到所描述的行为,可能这些方法之一会有所帮助。


另一种铸造方法。尝试一下怎么样:

<cfset str.b = JavaCast("int", 23)>

If you set the members of the Struct using cfscript instead of cfset do you get a different result? e.g.:

<cfscript>
str.a = "hello";
str.b = 23;
</script>

You might also try your cfset as:

<cfset str.b = Int(23)>

I've not seen the behavior described, possibly one of these approaches will help.


Yet another casting approach. How about trying:

<cfset str.b = JavaCast("int", 23)>
美胚控场 2024-12-30 08:28:06

看看这个。

任何尾随 D 或 F 后跟空格的完全数字字符串。

<cfscript>
struct = { "wtf" = "4D " };
string = serializeJSON( struct );

string == {"wtf":4D }
</cfscript>

尝试反序列化它。

<cfscript>
struct = deserializeJSON( '{"wtf":4D }' );
</cfscript>

JSON 解析失败于字符 9:'D' in {"wtf":4D }

嗯什么?

解析器修剪并检查最后一个字符是否是 D 或 F(双精度或浮点)并将其剥离。如果其余字符是数字,则不添加引号。

如果你没有尾随空格,它就可以完美工作。数字、D|F 和空格的特定组合会使其出错。

(我已将其提交给 Adob​​e cfbugs)

Check this one out.

Any entirely numeric string with trailing D or F followed by whitespace.

<cfscript>
struct = { "wtf" = "4D " };
string = serializeJSON( struct );

string == {"wtf":4D }
</cfscript>

Try deserializing it.

<cfscript>
struct = deserializeJSON( '{"wtf":4D }' );
</cfscript>

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文