Blog

20
March 2015

Gavin Pickin

ColdFusion JSON serialization failure - Unable to serialize binary data to JSON

CFML Language, CFML Server, Migrating to Railo, Techie Gotchas

Recently we finally migrated some big legacy websites off of some ColdFusion 8 servers. Yes, 8, we were still running one last server, and to be honest the only things running on it were BIG higher traffic websites, that we had not got the time to migrate. We have been migrating from Windows to Linux, and CF8 to CF9 and Railo / Lucee. Baby steps, but with our legacy code not having test coverage, it became a long painful process. You would think moving from ColdFusion 8 to 9 is a breeze, and for the most part it is, but I ran into a strange issue, which I am sharing today.

We have a series of CFCs (thankfully this code uses some) which we use for ajax requests. In these CFCs we have functions, with access = remote, and return format = JSON. We return the query, and ColdFusion serializes the data for us, and thats really all there is.

When we lit the site up on our dev server in CF9, we noticed some of our ajax calls were failing. The error returned was this:

JSON serialization failure. Unable to serialize binary data to JSON.

I googled a while, and found a few people having the issue, but not many solutions… hence, I am writing this now. I finally tracked it down to an issue with one field in my data. My CFCs were returning address data for my mapping system, and the code was erring out on the HTMLDATA field, which was a concatenation of the street number, street name, city state zip, phone and website url. So how was this concatenation of strings, becoming binary, and freaking out the JSON serializer?

The issue was, some of the fields, like Zip and Street number in the MySQL db were not strings. The concat function was actually joining strings and ints together, and since they were not all strings, it thought it should be binary (but only in CF9, CF8 has no issue with this approach).

The solution?

Cast those ints into strings, using the MySQL function cast( integer-field as char).
Remember this concat was long, has lots of fields, all over my cfc, I wish there was an easier way.
Finally it clicked, duh, I can just cast the concat… so only use 1 cast, which has to be quicker too, and easier to update.

So my code when from looking like this

concat(streetnumber, streetnumbermodifier, ' ', streetdirection, ' ', streetname, ' ', streetpostdirection, ' ', streetpostdirection, ' ', unit) as theaddress,

#to 

cast( concat(streetnumber, streetnumbermodifier, ' ', streetdirection, ' ', streetname, ' ', streetpostdirection, ' ', streetpostdirection, ' ', unit) as char) as theaddress,

 

Now, ColdFusion sees the concatenated information as a string, and can easily serialize that into JSON.

I have come across this myself a couple of times, if you are reading this, I hope this helps you too.
Keep an eye out for more migration tips, as I am making a big push to shut that server down, ASAP.

Thanks for reading
 

by Phillip Senn
08/04/2015 01:43:47 PM

Thanks Gavin.

Blog Search