Introducing JSONUtil

After getting into another discussion last week about the problems with Serialize/DeserializeJSON and ColdFusion's implicit type conversion I decided to try to do something about it. Over the weekend I put together a proof of concept which I've released as JSONUtil. From the RIAForge project site:

The JSONUtil project has two goals: to provide JSON serialization and deserialization methods for CFMX 7 which are compatible with ColdFusion 8's Serialize/DeserializeJSON functions and to provide better mapping between CF/Java/SQL data types and JavaScript data types than ColdFusion 8's SerializeJSON function.

The JSONUtil project currently consists of the JSONUtil component. This component has two public methods, serialize and deserialize, which have method signatures similar to ColdFuison 8's built in Serialize/DeserializeJSON functions. The JSONUtil deserialize method takes the same parameters as DeserializeJSON, JSONVar and strictMapping, and should produce the same results as that function. The JSONUtil serialize method takes the var and serializeQueryByColumns parameters, just as ColdFusion 8's built in SerializeJSON function, however this method also takes an optional third parameter, strictMapping. If the strictMapping parameter is ignored the serialize method should produce the same results as ColdFusion 8's built in SerializeJSON function. If the Boolean strictMapping parameter is set to true then the serialize method attempts to convert the ColdFusion data to a JSON string using underlying Java/SQL data types as opposed to using ColdFusion's implicit type conversion conventions.

The code for the JSONUtil component is based on that of the json.cfc from the CFJSON project, http://www.epiphantastic.com/cfjson. This project probably would not have happened were it not for CFJSON. Thank you to Jehiah Czebotar, Thomas Messier, and everyone who has contributed to the CFJSON project.

To expand on the RIAForge description a bit further, the strictMapping attribute I've added to the serialize method forces the CF data to be serialized based on it's underlying Java data type. For example the following code:

<cfset JSONUtil = CreateObject("component","JSONUtil") />
<cfset x = "123" />
<cfoutput> #JSONUtil.serialize(var=x, strictMapping=true)# </cfoutput>

will produce the following JSON string:

"123"

SerializeJSON or JSONUtil.serialize without the strictMapping attribute will produce the following JSON string:

123

Note the lack of quotes, indicating a JavaScript number as opposed to a string.

The strictMapping attribute also applies to query data as well. If you have a varchar column, its data will always be serialized as string data, even if the string is a numeric or Boolean value in ColdFusion. The strictMapping option also allows you to use JavaCast to help ensure your data values are serialized properly.

There is, however, one caveat to the strictMapping option. In ColdFusion most values are stored as strings. This means that both x and y below:

<cfset x = "123" />
<cfset y = 123 />

will be serialized as:

"123"

If you want numeric values to be serialized as such when using strictMapping you will need to use the Val or JavaCast functions:

<cfset x = Val(123) />
<cfset y = JavaCast("int",123) />

(Note: you do not need to do this for query values as query columns are automatically cast to the appropriate Java type by JSONUtil.)

So head over to RIAForge and check it out, and be sure to report any issues via the bug tracker.

Thanks again to the CFJSON project, and thanks to Todd Sharp for pointing out that you can get column data types from a CF query using GetMetadata().

Comments
Chris Dawes's Gravatar Awesome project dude... can you turn on the zip download for the riaforge site.

Cheers,
Dawesi
# Posted By Chris Dawes | 12/16/08 12:19 AM
Nathan Mische's Gravatar The zip download should be back up on the RIAForge site. It was temporarily down while SVN was being set up for the project.

I should have the SVN repo set up in the next day or so.
# Posted By Nathan Mische | 12/16/08 12:49 PM
Mary Jo Sminkey's Gravatar Just FYI, looks like the RiaForge page still doesn't have a download link.
# Posted By Mary Jo Sminkey | 4/26/16 12:32 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.