How to make the property of an object output as a string when returning as JSON

I store color values ​​as HEX in my database, which are displayed in the ORM settings in CF9. When my color values ​​are completely numeric (e.g. 000000), ColdFusion serializes them as numbers (e.g. 0.0) when returned from my CFC as JSON. Is there a way to make these columns / properties serialize as strings?

+4
source share
4 answers

1st option

You can try the following:

<cfset finalValue = " " & yourValue > 

OR

 <cfset finalValue = " #yourValue#" > 

javaCast does not work, adding trailing space does not 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

Second option

Using your own method instead of serializeJSON, there is one on the Ben Nadel website that you can tailor to your needs http://www.bennadel.com/blog/100--CF-JSON-My-Own-ColdFusion-Version-For-AJAX .htm .

+1
source

If you are not afraid of a little java (~ 100 loc), you can pass your query (coldfusion.sql.QueryTable - do a google search) to the java class and let Jackson convert it to json for you. It is very fast and keeps your data types the same as your databases. Therefore, if you have varchar with 0 as the value, you will get "0" back. If you have an int, you get an int. Null is zeros, and empty lines are empty lines (although you can override this if you want). It is totally worth using java to get around all these CF json issues.

+1
source

A hacky quick fix will simply book your values ​​with, say, a trailing non-numeric character before serialization and transit. This is ugly, but 000000Z will not be implicitly converted to a numerical value using CF. Trim before use, and then figure out a cleaner solution for the aggressive β€œutility” of CF at your leisure.

0
source

If it's colors, hold the hash on the front panel?

 <cfset Value = "##" & Value /> 
0
source

Source: https://habr.com/ru/post/1310428/


All Articles