The title probably wouldn’t make a great deal of sense under normal circumstances… but I am reffering to Coldfusion struct keys and maintaing their case.
In some circumstances it is important to make sure that the case that you enter your struct keys with is not altered, and you are probably thinking that surely whatever case you use it is maintained, but this is only in true under some circumstances. One of my amazing colleagues in our development studio has a situation where the case must be maintained at lower case converting a struct to a JSON packet to be consumed by a facebook application, but because of the way she was building the struct, CF forces the keys to upper case. Check out the example below:
<cfset stPerson = structNew()>
<cfset stPerson.name = "Sandy">
<cfset stPerson.address = "12 Amazing Street">
<cfset stPerson.suburb = "Amazingville">
<cfset stPerson.state = "SA">
<cfset stPerson.phone = "54321234">
<cfdump var="#stPerson#">
and the result:
struct |
ADDRESS |
12 Amazing Street |
NAME |
Sandy |
PHONE |
54321234 |
STATE |
SA |
SUBURB |
Amazingville |
Check out the keys, they are all forced to upper case! Why? , well I found this on another CF developer blog and it sounded pretty good – “ColdFusion internally represents structure keys in uppercase when the keys are created using dot notation. Dot notation is typically how programmers write their code and is considered to be best practice when working with structures.”
So there you go…. but how can we maintain case you ask? well, quite easy, don’t create your structs using dot notation, explicitly set your key names by wrapping them in single quotes, check out this example:
<cfset stPerson = structNew()>
<cfset stPerson['name'] = "Sandy">
<cfset stPerson['address'] = "12 Amazing Street">
<cfset stPerson['suburb'] = "Amazingville">
<cfset stPerson['state'] = "SA">
<cfset stPerson['phone'] = "54321234">
<cfdump var="#stPerson#">
and the result:
struct |
address |
12 Amazing Street |
name |
Sandy |
phone |
54321234 |
state |
SA |
suburb |
Amazingville |
See!! the case of the keys has been maintained!!!