coldfusion serializejson problem

1,198 views
Skip to first unread message

jb...@fielddiagnostics.com

unread,
Jan 22, 2013, 1:30:58 AM1/22/13
to ang...@googlegroups.com
Quick question,

Coldfusion has a problem when serializing numbers as json. It treats anything that 'looks' like a number as a number. This causes problems in 2 scenarios when using coldfusion json in Angular:

1) Fringe case: If one has, for example, a phone number as a string of seven zeroes, coldfusion would serialize it as:
   {phone: 0000000}, rather than {phone: "0000000"}. 

   According to the json spec, the first is invalid json. AngularJS does indeed throw a syntax error when it tries to deserialize 0000000 as a number, as it should.

2) Not as fringeful case: Coldfusion will, helpfully enough, render strings that look like numbers which are larger than javascript's max number as numbers too. This doesn't work very well as parts of the string are lost.

I guess my question is: what's the best way to make Angular work with coldfusion's lousy json serializer?

Dave Merrill

unread,
Jan 22, 2013, 1:45:00 PM1/22/13
to ang...@googlegroups.com
I don't deny the limitations of CF's JSON serialization, but as far as I can tell:

- Numbers are a valid datatype for JSON, and are/can be represented without quotes. See both the Wikipedia page and json.org.

- The first thing that's genuinely invalid is that the field name (phone) isn't quoted. That's required. OTOH, no version of CF I tried generated that using serializeJSON, field names are always quoted. Not sure where you got your sample.

- The second thing that's genuinely wrong is that 0000000000 isn't valid as a number. OTOH, that's not a likely phone number either. OTOOH, 1234567890, a more reasonable phone number, gets converted to a float, losing precision, in CF8, but not in CF10. Using javaCast("int", "1234567890") prevents that, but having to do that everywhere there might be an int sucks.

So, the upshot of all this is that CF's JSON handling leaves a lot to be desired, but if you can use CF 10 you'll be better off. (It also formats dates "for you", even if you have a standard, valid internal representation you've already applied. I could go on...)

Beyond that, you could look at the Jackson JSON library for Java, but you'll have a fair amount of workto do to get what CF gives for free, albeit "imperfectly".

Dave Merrill

jb...@fielddiagnostics.com

unread,
Jan 22, 2013, 8:17:10 PM1/22/13
to ang...@googlegroups.com
Thank you for the response!

I was a doofus left out the quotation marks for the field in the example. The proper example is: {"phone":0000000}. Must be an unconscious desire to put more blame on poor CF than it deserves =).

The Jackson JSON library looks interesting. We're developing a web site using CFWheels framework on CF 9 for the backend, and AngularJS for the frontend. It would be interesting to tinker with CFWheels/CF's guts and hook another JSON library into it, but I should probably resist that urge.

The only other workaround I've found is to force CF to convert the variable to a string by inserting an alphabetic or punctuation character into the string before serializing. Then it will be a string, but with a character you have to remember to strip off each time. And I'll forget...

Dave Merrill

unread,
Jan 22, 2013, 10:47:56 PM1/22/13
to ang...@googlegroups.com
Not sure what exactly you're doing, but one of the big awkwardness I find with serializeJSON is the format of query results. It's very efficient in that the data is smaller than the more usual object-per-row format, but it's ugly to bind to. You can deal with that on the server (convert to object-per-row format before sending to the client, with the size increase that causes), or on the client (unpack into object-per-row in javascript), which is what I prefer. 

Your questions prompted me to push the library I wrote for this up to GitHub, here: https://github.com/davemerrill/CFMLQuery.js. It's open source, let me know if you find it useful.

The other big issue, as you've seen, is type handling. To fix that, you have to replace CF's native serializeJSON with something more type-aware and/or controllable. I haven't used it myself, but JSONUtil looks like it might help, if you don't want to do the whole Jackson thing. Jackson does look attractive, and probably at least somewhat faster than anything written in CFML, but you'd have to build out query serialization, since ColdFusion queries aren't the Java objects it's used to working with.

Dave Merrill
Reply all
Reply to author
Forward
0 new messages