views:

82

answers:

4

I'm storing color values as HEX in my database, which is mapped via ORM settings in CF9. When my color values are entirely numeric (e.g. 000000), ColdFusion is serializing them as numbers (e.g. 0.0) when returned from my CFC as JSON. Is there a way to force these columns/properties to be serialized as strings?

A: 

A hacky quick fix would be simply to armor your values with, say, a trailing non-numeric character prior to serialization and transit. It's ugly, but 000000Z will not be implicitly converted to a numeric by CF. Trim before using, and then figure out a purer solution to CF's aggressive "helpfulness" at your leisure.

Ken Redler
+1  A: 

1st option

You could try this:

<cfset finalValue = " " & yourValue >

OR

<cfset finalValue = " #yourValue#" >

javaCast doesn't work, adding trailing space doesn't work.

http://www.mischefamily.com/nathan/index.cfm/2008/10/22/ColdFire-1295100-and-a-CF-to-JSON-Gotcha

http://www.ghidinelli.com/2008/12/19/tricking-serializejson-to-treat-numbers-as-strings

2nd option

Using custom method instead of serializeJSON, there's one on Ben Nadel's site which you could adjust to your needs http://www.bennadel.com/blog/100--CF-JSON-My-Own-ColdFusion-Version-For-AJAX.htm .

zarko.susnjar
A: 

If these are colours, stick a hash on the front?

<cfset Value = "##" & Value />
Peter Boughton
+1  A: 

If you're not afraid of a little java (~100 loc), you can pass your query (a coldfusion.sql.QueryTable -- do a google search) out to a java class, and let Jackson convert it to json for you. This is very fast, and keeps your data types the same as what came from your database. So if you have a varchar with a 0 as the value, you get '0' back. If you have an int, you get an int. Null's are nulls, and empty strings are empty strings, (although you can override this if you want). Totally worth using java to get around all these CF json issues.

Mark