Tuesday, August 24, 2010

Coldfusion Structure and StructInsert()

I've been doing a lot of Ajax driven features lately and, since so much of the older server-side I'm working with is in Coldfusion, I've been building a lot of private services in CF which return data in JSON. When I first started creating the CF services, I'd do a bit of massaging of data in CF and then loop over the data building a manually coded JSON string for output.

When I discovered Coldfusion's serializeJSON() function, I was very happy and I had a lot less typing to do. The first problem arose when I converted some of my CF code using native CF arrays and structures and ultimately pushed into the serializeJSON() function--my Javascripts, which consumed the JSON data, broke.

After doing some searching on the web, I learned that most everyone else using CF had encountered this problem--CF Struct keys are serialized to ALL-CAPS. I tend to use basic-camel case for all my variable names and this helps me keep track of things between CF (non-case sensitive) and Javascript (case sensitive). As soon as I started using serializeJSON(), my Javascripts broke and, upon closer inspection, I discovered the letter case problem. So, for a while, I started setting all my JSON key values in Javascript to ALL-CAPS whenever the JSON was to come from my CF services. Yesterday, all of this came full circle and I am back to normalcy.

The second problem that came up yesterday was when I was consuming two different CF services in the same front-end page. The first was generating JSON from a CF structure that a colleague had build and the second I had built. I first built all of my Javascript around my own CF-to-JSON service and everything was working fine. I reviewed the code and made sure that the JSON structure/signature output by my colleague's service and my own were identical. As soon as I began consuming  my colleague's service, my Javascripts broke again. When I inspected the JSON output, all the keys were in camel case!

It took a while but I finally discovered the difference between my colleague's JSON service and my own. My colleague initialized keys into the CF structure using structInsert() whereas I had never used that function--I have always enjoyed using CF's short-hand (magic--always avoid magic in coding!) for adding keys and values to a structure.

--using short-hand, magic:
myStruct.firstMember = "value";
Using the above technique, the letter casing of the keyName WILL NOT be preserved during serialization.

--using structInsert(structReference, keyName, value, allowOverwrite):
myStruct = structNew();
structInsert(myStruct, "firstMember", "value", true);
Using the above technique, the letter casing of the keyName will be preserved during serialization.

Also note that, using the structInsert() technique, if you set the allowOverwrite param to true, you can revert back to the short-hand, dot-notation (NOT magic) for updating structure member values after they are already initialized. This WILL NOT degrade preservation of your letter casing during serialization.

Finally, my JSON object keys in my Ajax call-back functions are readable again!!