JSON and Hex/Octal Numbers

1,385 views
Skip to first unread message

Matt Woodward

unread,
Nov 24, 2010, 11:57:22 AM11/24/10
to CFML Conventional Wisdom
This issue came up in a comment thread on Gary Gilbert's blog:
http://www.garyrgilbert.com/blog/index.cfm/2010/11/24/SerializeJson-Sucks-II

And a ticket was submitted to OpenBD:
http://code.google.com/p/openbluedragon/issues/detail?id=289

Specifically the issue is this. Consider the following code (example
given by Nathan Mische):
<cfset x = 0010 />
<cfset p = { x = x } />
<cfset s = SerializeJSON(p)/>
<cfoutput>#s#</cfoutput>

The output is:
{"x":0010}

This isn't valid JSON, and if you get the value of x from JavaScript,
it comes back as 8 as opposed to 0010 since it's assuming octal.

The question is, what's the appropriate way to handle this?

One approach would be to automatically convert any potentially
problematic data (i.e. hex or octal) to strings, but of course this
would only work if we can consistently identify this type of data.

Another approach would be to throw an invalid JSON error when
SerializeJSON is called if the data being serialized contains invalid
data.

A third approach would be to leave things as is and put it on the user
to worry about data that might result in invalid JSON.

I wanted to bring this up on this list to see what the consensus is
and what makes sense to everyone.
Reply all
Reply to author
Forward
0 new messages