Managed Hosting



Project Home Blog Known Issues Contact Project

JSONUtil Issue: IsDefined("_data.#arKeys[i]#") doesn't work when the struct keys are numeric

Name: IsDefined("_data.#arKeys[i]#") doesn't work when the struct keys are numeric
ID: 15
Project: JSONUtil
Type: Bug
Area: Code
Severity: Normal
Status: Open
Related URL: https://gist.github.com/machristie/14e85ba1ab798bcd3cfa/revisions
Creator: Marcus Christie
Created: 12/04/14 1:13 PM
Updated: 12/04/14 1:13 PM
Description: I'm working with version 1.5 of JSONUtil.cfc

I'm using ColdFusion 7.02. I've run into a problem serializing a struct to JSON when the struct has numeric keys. I'm getting this error:

Parameter 1 of function IsDefined, which is now "_data.10680875", must be a syntactically valid variable

I've been able to fix this by switching to StructKeyExists, which I believe is equivalent.

I tried to test and verify that StructKeyExists would work as well as IsDefined. In the following example code, IsDefined and StructKeyExists return the same answer, NO:

<!--- returnNull: http://www.bennadel.com/blog/1654-learning-coldfusion-9-isnull-and-working-with-null-values.htm --->
<cffunction name="returnNull" access="public" returntype="void" output="false">
<cfreturn />
<cfset s2 = structnew()>
<cfset s2.a = returnNull()>
<cfdump var="#s2#">
<p>#StructKeyExists(s2, "a")#</p>

Here's the patch for this change:

diff --git a/JSONUtil.cfc b/JSONUtil.cfc
index 620fe87..0d9bbd2 100644
--- a/JSONUtil.cfc
+++ b/JSONUtil.cfc
@@ -397,7 +397,7 @@ limitations under the License.
         <cfset dJSONString = ArrayNew(1) />
         <cfset arKeys = StructKeyArray(_data) />
         <cfloop from="1" to="#ArrayLen(arKeys)#" index="i">
-            <cfif IsDefined("_data.#arKeys[i]#") >
+            <cfif StructKeyExists(_data, arKeys[i])>
               <cfset tempVal = serializeToJSON(_data[ arKeys[i] ], arguments.serializeQueryByColumns, arguments.strictMapping ) />
               <cfset tempVal = "null" />

And I've included this fix in this GitHub Gist: https://gist.github.com/machristie/14e85ba1ab798bcd3cfa/revisions

History: Created by machristie (Marcus Christie) : 12/04/14 1:13 PM

To add a comment to this bug, please login using the link above.