March 18, 2013
JSON backward Compatibility: Flag to enable CF8 behavior
Comments
(18)
March 18, 2013
JSON backward Compatibility: Flag to enable CF8 behavior
Newbie 8 posts
Followers: 0 people
(18)

JSON serialization was
introduced in CF8 for the first time. It was used heavily to serialize CFC and
other CF data objects. In “CumulativeHotfix1 for ColdFusion 9.0.1”, some of its
outstanding issues were fixed. 
Few of issues addressed in hotfix were that of string to number
conversion, and number getting converted to double.

The change in behavior
of JSON serialization, which fixed some of the basic problems of CF developer
at one hand, also had a side effect of breaking old application built on CF8.
Migration from CF8 to later version might require making few changes in the developer’s
code.

To achieve backward
compatibility and to address a scenario where old behavior of JSON
serialization has to be retained, a JVM flag had been introduced at the same
time. If enabled, it would bypass the latest changes for JSON serialization and
fall back to CF8 behavior.

Here I am describing the JVM flag and the way to
enable it. This will bring back the old behavior where a string with leading zero like ‘000’ will be output as 0.0 rather than 000, an invalid number in javascript. Similarly all the number will be retained in double/float format. A number like 1.23E4 will be converted to 12340 and so on. So you can make use of this flag if you have got stuck upgrading to latest CF server.

To achieve the CF8 behavior, one can add a
JVM system property 
json.numberasdouble=true to jvm.config.
It is applicable to  CF “CumulativeHotfix1 for ColdFusion 9.0.1” and
later .

jvm.config file is located at  $CF_HOME/cfusion/bin/jvm.config.
Open the file and you will find “-Djava.awt.headless=true” as one of the entry
in java.args parameters.

Please add the suggested
flag just after former headless entry as  –Djson.numberasdouble=true.

 

 

18 Comments
Mar 23, 2013
Mar 23, 2013

Hi Ryan,

Thanks very much for that tip – I will check that out!

-Aaron

Like
()
Edit
Mar 23, 2013
Mar 23, 2013

myJSONOptions = {addSecureJSONPrefix=false, …

could just be

myJSONOptions = {addPrefix=false, …

Like
()
Edit
Mar 23, 2013
Mar 23, 2013

@Aaron

We actually ended up implementing JSONUtil.cfc and replacing the native serialization. You can then cast your values to appropriate Java objects (ie. java.lang.String) and have you types preserved on serialization. It’s a bit more work, but was the best way we could fine to achieve proper control over JSON types coming out of CF

Like
()
Edit
Add Comment