JSONUtil 1.1

Last year I put together JSONUtil as a proof of concept solution to the issue of ColdFusion's implicit type conversion during JSON serialization. There were some issues with the 1.0 release, and I've had a few people submit patches, but because I had considered the project an experiment I didn't really take the time to update the official release.

Well, last week I was working on an AJAX project where I was making XHR requests to ColdSpring remote proxies using ColdFusion's built in JSON return format when I began running into issues with implicit type conversion. Specifically I had a method that should have returned a query with string values, however numeric strings were getting converted to decimal numbers. (For example the string "001" was being converted to number 1.0.) Because I was already using ColdSpring to generate the proxies I decided to put together a JSONUtil based ColdSpring advice component to handle the serialization. The advice turned out being so handy I thought I'd package it up and include it in the JSONUtil release.

In addition to adding the ColdSpring advice, I made a few other changes to the library:

  • I renamed the main methods from serialize/deserialize to serializeToJSON/deserializeFromJSON. I also added aliases so these methods can be called using the serialzeJSON/deserialzeJSON or serialize/deserialize method names. (Railo had a problem with the serialize/deserialize method names used in the original version of JSONUtil. Adam Tuttle reported this issue and submitted a patch ages ago, I just never fully tested it. JSONUtil v1.1 was tested on Railo 3.1.1 and it seems to work fine now.)
  • I made a few changes in serializeToJSON to provide "better" results when not using the strictMapping option. By better I mean the results better match ColdFusion's native SerializeJSON method.
  • Updated the unit tests to remove dependencies on ColdFusion's example data sources.
  • Added a redme.txt with a sample ColdSpring configuration.

I'll just point out that there will be a few differences when running JSONUtil on ColdFusion vs. Railo. First, when using QueryNew Railo stores both Integer and Decimal columns in Java Double variables. ColdFusion stores Integer columns as Java Integers and Decimal columns as BigDecimals. I've tried to correct for this with Railo specific code on query serialization so that serializing a query on Railo should produce results similar to ColdFusion, but they may not be exactly the same.

For the second know issue, consider the following code:

<cfset x = 123 />

In ColdFusion x is actually a Java String, while Railo stores the variable in Java Double. This means you will get different results if you serialize the above using strictMapping. In ColdFusion the value will serialize as "123" (a string) while Railo will serialize the value as 123.0 (a number).

I'm sure there are a lot of other ColdFusion/Railo differences, but those are the two big ones I ran into in my testing.

Please be sure to report any issues on the JSONUtil project page.

Comments
Tim's Gravatar Found your JSONUtil script to use on a CF7 instance we have. Thanks for the script. One issue -- you spelled "deserializeJSON" wrong -- line 20 says <cfset this.deserialzeJSON = deserializeFromJSON />

Thought youd like to know.
# Posted By Tim | 4/15/10 12:30 PM
Andrew's Gravatar I have really liked the JSONUtil. I think I ran into a problem though. When I was serializing a query and then later deserializing, it would return the object mapping instead of the query even though I had the second parameter as false. I looked at the query data and it was deserializing correctly until it hit a column that has "[" or "]" in the query cell value.
Once I converted the characters to parenthesis to test, it worked correctly. In the end, I could use serializeToJSON, but to deserialize it correctly, I had to use the built-in CF deserializeJSON function.

I am not sure if this is a bug or not.
# Posted By Andrew | 3/8/11 4:51 PM
Nathan Mische's Gravatar Thanks Andrew, I will have to take a look at this.
# Posted By Nathan Mische | 3/23/11 11:23 AM
Nathan Mische's Gravatar @Andrew - JSONUtil 1.2 has been release to address this issue. Please get the latest from RIAForge.
# Posted By Nathan Mische | 3/24/11 4:25 PM
essay corrector's Gravatar The coldfusion server will automatically generate documentation for a component if you navigate to its url and insert the appropriate code within the component's declarations.
http://www.essaycorrector.com/ essay corrector
# Posted By essay corrector | 7/16/13 4:44 PM
Mary Jo Sminkey's Gravatar This is a nice piece of work but I did find it unusable in situations where you have a lot of data to loop over and serialize. I was finding CF took 15+ minutes to loop over about 2000 rows of data and serialize using it, versus the CF function taking just milliseconds. Ultimately I had to stick with hacks to correct the couple of fields that CF doesn't serialize properly (typically numbers appearing in a string field). Just wanted to post a warning to anyone looking to use this that it's only appropriate for small amounts of data and you should always test custom libraries for execution time issues.
# Posted By Mary Jo Sminkey | 4/28/16 12:37 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.