Copy link to clipboard
Copied
component {
VARIABLES.obj = {};
VARIABLES.obj.foo = "bar";
VARIABLES.obj['foo2'] = "bart";
remote function func1() {
return VARIABLES.obj;
}
}
The response from the URL “/test.cfc?method=func1&returnformat=json” looks like this:
{foo2: "bart",
FOO: "bar"
}
Note that if you declare the property of a structure with dot notation, it is returned in uppercase. Using array like declarations returns it as you specified.
This is still apparent when dumping a structure in a page, but is more of a problem when accessing the results via JSON in a Javascript appliation, as this is case sensitive.
Pete
Copy link to clipboard
Copied
Why do Coldfusion structure key cases change depending on declaration style?
I think it is just a quirk of ColdFusion.
Copy link to clipboard
Copied
Being OCD-ish and a Data Nazi, it kind of gets me too, but it's nice to know CF sees those keys as case insensitive, and really the only time you see the bad casing is when you dump the structure.
Still, I am pushing for properly cased structure keys for COLDFUSION 11!!!
I think someone said if you use structInsert( structName, keyName, value, true), it will maintain the casing.
Copy link to clipboard
Copied
That makes sense, using structInsert specifies a string for the key, in the same way "VARIABLES.obj['foo2'] = "bart";" does.
But surely it'd still make more sense if dot notation created lower case key names?
Is there some underlying Java reason behind it?
Copy link to clipboard
Copied
But surely it'd still make more sense if dot notation created lower case key names?
Is there some underlying Java reason behind it?
Uppercase, lowercase, random-recasing... it's not important in the context of CF, as CF is case-insenstive.
As you suspect, it's tied into the underlying Java, which is case-sensitive. CF doesn't see "foo.bar" and "foo.BAR" as different keys, but java would, so CF needed to pick a method of ensuring a Java would see them as the same. So it decided to upper-case them (it has to do something).
However with associative array notation or structInsert(), one is able to stipulate the case, and this is maintained. CF still sees foo["bar"] and foo["Bar"] as the same variable though... I'm not sure how it deals with that when passing it to Java (I've never checked).
I also don't know if one does this:
foo.bar = true;
foo["Bar"] = true;
foo["bar"] = true;
Whether they key stays as "BAR" (from when it was first set), or "bar" (how it was last set). I can see arguments both ways. Again, I've never checked.
Bottom line: it doesn't matter unless the key name is being used in data interchange with a system that is case-sensitive. Which 99% of the time is not the case.
--
Adam
Copy link to clipboard
Copied
I also don't know if one does this:
foo.bar = true;
foo["Bar"] = true;
foo["bar"] = true;
Whether they key stays as "BAR"
That's a good point... I'm going to test that.
And I agree 99% of the time it doesn't matter, but when using CFCs to build JSON responses to feed to Javascript, it can be a real pain.
Copy link to clipboard
Copied
<cfscript>
foo.bar = 'First';
foo['bar'] = 'Second';
writeDump(foo);
</cfscript>
struct | |
---|---|
BAR | Second |
<cfscript>
foo['bar'] = 'State 1';
foo.bar = 'State 2';
writeDump(foo);
</cfscript>
struct | |
---|---|
bar | State 2 |
They key doesn't change when you switch notation types