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
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.