Java to Coldfusion HashMap

As some of you know I do web application development. The languages of choice for my current employer is coldfusion and java with some flex thrown in there for good measure. Last Friday I began experience a very odd bug with the two so decided I would share my findings with the world.

The problem was I was returning a object of type List with a collection of Result objects. Result was a object I created that extended HashMap. For those who don’t know coldfusion automatically maps a List object to an Array in coldfusion and a Map (or HashMap) object to a Struct. This was working on Thrusday but for some reason stopped on Friday.

The issue I was expereiencing was when I was trying to retreive information out of the Result object in coldfusion I was not able to use the dot notation:

currentResult.someElement

Not wanting to rewrite my code I attempted several fixes to get around this. First I discovered that if I did a IsStruct the object was of type struct. Next I checked the list of keys using StructKeyList. This too showed me that “someElement” was a part of my structure. Finally I tried a StructFind(currentResult, “someElement”) which is essentially the same as the above code but for some reason coldfusion found the value this time.

So I went back to the drawing board. I didn’t want to rewrite all my existing code to use StructFind, because eventually someone would come along (probably me) and try to revert it to the more clean syntax using the dot notation. So I attempted to cast the Result object to a Map before I put it into the List on the java side. This didn’t work either.

Finally I found a solution that seemed to work. I used the coldfusion Duplicate method to do a deep copy of the structure to itself:

currentResult = Duplicate(currentResult)

This seems to be working and now I just have to explain why I did one crazy thing instead of a million.

This entry was posted in programming and tagged , , . Bookmark the permalink.

2 Responses to Java to Coldfusion HashMap

  1. This is because CF uppercases struct keys that are accessed using dot notation.

    map = createObject(“java”,”java.util.HashMap”).init();
    map.put(“foo”,1);
    map.put(“BAR”,1);

    // this won’t work
    map.foo;

    // this will
    map[“foo”]

    // this will too, since we inserted it in upper case
    map.bar

  2. Shane says:

    Thanks for posting this! Saved me some headache…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>