The amazing adventures of Doug Hughes

ColdFusion 8 (Scorpio) is right around the corner and for me, the greatest thing about this release is that there is nothing really big in it. The whole release is little stuff tailored to the developer allowing us to better plug ColdFusion into virtually any scenario. One of these features is the addition of native JSON support. This is all of about three new functions, but it could be a very big time saver.

What is JSON?

The Wikipedia definition of JSON is a “lightweight computer data interchange format. It is a text-based, human-readable format for representing objects and other data structures and is mainly used to transmit such structured data over a network connection (in a process called serialization).”

In other words, JSON is a data format, like XML, that is used to send data from one system to another. The difference is that JSON is a data only format whereas XML is a document and data format. This means that XML is much more complete, but at the same time, much more verbose and complex. For example, take a look at the following XML snippet that might describe an individual contact in a contact book.

        <street>123 Somewhere</street>
        <city>San Jose</city>

If you read through that, there is a lot of text and information there just to describe a simple contact record. Now, take a look at the JSON example that includes the exact same information.

    "contact": {
            "firstname": "John",
            "lastname": "Doe",
            "address": {
                    "street": "123 Somewhwere",
                    "city": "San Jose",
                    "state": "California""zipcode": "12345"
            "phonenumbers": ["123-123-1234", "234-234-2345"]

The JSON string is easier to read, much shorter (about 80 characters), and has an even bigger benefit. When you are dealing web services, you must be able to decipher the XML data received from a web service on the client-side. This typically requires invoking some sort of parser and then using very long and complex DOM of xPath strings to access individual pieces of data. With JSON, none of that is needed. JSON is JavaScript syntax, so you just perform a quick “evaluate” on it and you have a set of nested structures and arrays.

What does this do for ColdFusion?

With todays emphasis on rich internet applications, ColdFusion development is shifting more and more into the middle tier of the application. We are writing remote ColdFusion components that will exchange data with rich user interfaces now instead of writing CFML pages that display HTML. This means more web services (remote access cfcs) and more data exchange, which in the past meant XML. However, many of the AJAX and RIA libraries out there support JSON data as well. With ColdFusion 7 and before though, this meant having some sort of middle man interface that could take ColdFusion data structures and parse them into a JSON string and vice versa. It worked, but now we have something much simpler.

ColdFusion 8 is introducing two new functions . serializeJSON and deserializeJSON. All these functions do is translate ColdFusion data types into JSON data and back again . extremely simple. Take a look at an example:

It is as simple as that. The deserializeJSON function is just as easy give it a string containing JSON data and out you get a ColdFusion data structure that you can work with as usual. One thing to take note of, there is a bit of confusion regarding the case of the keys generated by the JSON functions. The documentation states that all keys are converted to upper case as JavaScript is case sensitive, which is what you see above. However, in playing a bit, it looks like keys in the lower levels of a nested structure dont get converted, so you may have to do some investigation here once the final release of ColdFusion 8 comes out to see what things look like.

The second part of the new JSON functionality comes into play with remote function calls. Previously with CFCs, there is a returnType attribute, which as is expected, specifies the data type of the value being returned from the function. With ColdFusion 8, there is a new attribute called returnFormat which only applies to remote access components and functions and works in conjunction with the returnType attribute. This attribute takes three different values:

  • Plain
  • WDDX
  • JSON

The “Plain” value returns the result as a plain text string. This obviously only works if the returnType is a simple value that ColdFusion can represent as a string . a boolean, date, guid, number, string, uuid, or XML. The “WDDX” value returns the result in WDDX format and the “JSON” value returns the result in JSON format.

What this means is that now we can have AJAX calls made to remote CFCs which can then return JSON formatted data directly without any middle conversion layer.

Wrap Up

This has been a pretty quick overview of this functionality which is not exactly real complex. The real benefit lies in the fact that it makes life as a developer that much easier which is what I like so much about this release.

Comments on: "JSON Support in Scorpio" (1)

  1. Michael Sharman said:

    Hi Jeff,

    Unfortunately your examples aren’t showing on FF Mac.


Comments are closed.

Tag Cloud

%d bloggers like this: