Over a million developers have joined DZone.

Bug in SerializeJSON

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

I've spent the last few hours trying to track down an intermittent bug and it seems to be caused by SerializeJSON on CF9.01 patched with the Cumulative Hotfix 1 (CHF1) for ColdFusion 9.0.1. Here's a quick example:
<cfset foo = {telephone="123456789"}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone="+123456789"}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone=" +123456789"}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone="+123456789 "}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone="+ 123456789"}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone=["+123456789"]}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone=[" +123456789 "]}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

<cfset foo = {telephone=["+123456789 "]}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#IsJson(bar)#">

The output I get when I run the above code returns:


YES NO YES NO YES NO YES NO

Looking a bit closer at the string that SerializeJSON is creating I noticed that ColdFusion treats "+123456789" as a numeric value and therefore does not wrap it in quotes when generating the JSON. To demonstrate here is another bit of test code.

<cfset foo = {telephone1="+123456789",telephone2=" +123456789",telephone3="+123456789 "}>
<cfset bar = SerializeJSON(foo)>
<cfdump var="#bar#">
<cfdump var="#IsJson(bar)#">
<cfdump var="#DeSerializeJSON(bar)#">

When I run this I get:

{"TELEPHONE1":+123456789,"TELEPHONE2":" +123456789","TELEPHONE3":+123456789 } NO

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator. 

Error Occurred While Processing Request

JSON parsing failure at character 15:'+' in {"TELEPHONE1":+123456789,"TELEPHONE2":" +123456789","TELEPHONE3":+123456789 }
 
The error occurred in C:\xampp\htdocs\scratchpad\index.cfm: line 12
10 : <cfdump var="#bar#">
11 : <cfdump var="#IsJson(bar)#">
12 : <cfdump var="#DeSerializeJSON(bar)#">

 

So there you go, looks like a bug to me and I'll be adding it to the bug tracker unless anyone else has already reported it.

Update: I tried removing chf9010001.jar from my lib\updates folder and restarting ColdFusion and everything works as expected, so this bug seems to have been introduced in the HotFix.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}