Orange is my favorite color

Quick tip for anyone using ColdFusion’s serializeJSON routine for Javascript applications. The type conversion from CF to JSON is finicky and there are times when you need more finely grained control. Specifically using an Ext combobox, we ran into an issue where an <option> value of 0 was treated as false preventing selection of that item.

SerializeJSON encodes numeric values as floats so 0 becomes 0.0, 1 becomes 1.0, and so on. The solution is to pass the combobox a string (‘0′) instead of a number (0). While JavaCast and other techniques failed, it is possible by putting a leading space on your numeric value:

<cfset records["someValue"] = ' 0' />
<cfreturn serializeJSON(records) />

The result is {“someValue”: ” 0″} which Javascript (or at least Ext’s combobox) handles just fine. Nice little trick to have in the toolbox when you need to treat numeric data like strings.

Comments are closed.