Is there any way to prevent serializeJSON() from converting long numbers into scientific notation? It would be great if there was a way to specify that the numbers be stored in String format within JSON rather than number format.
I ran into this issue attempting to preserve numbers with leading zeros. SerializeJSON would strip the zeros from the number, converting them to decimal format (0001 became 1.0, for example). I never found anything to prevent SerializeJSON from forcing these conversions, so I hurriedly wrote a function to handle it for me. It was a quick and dirty solution, by no means ideal.
<cffunction name="forceJSONStrings" output="false" access="remote" returntype="string" hint="Forces JSON to output string values"> <cfargument name="serializedJSON" type="string" required="true"/> <cfargument name="propName" type="string" required="true" /> <cfargument name="propVal" type="string" required="true" /> <cfscript> var ret = ""; var key = Trim(arguments.propName); var value = Trim(arguments.propVal); ret = ReReplaceNoCase(Trim(arguments.serializedJSON),'"#key#":[0-9]*\.[0-9]*','"#key#":"#value#"',"ALL"); </cfscript> <cfreturn ret /> </cffunction>
Just pass the function your serialized JSON, the name of the field (struct key), and the correct value, which is expected to be a string but you can change as needed. I think the function has the basic elements, ones you can tweak as required to suit your application (my regular expression does not account for numbers with commas or any other punctuation marks, save for periods).
I hope it might help