Skip navigation
russellspivey
Currently Being Moderated

serializeJSON() converts long numbers into scientific notation

Jul 11, 2009 2:26 PM

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.

 
Replies
  • Currently Being Moderated
    Jul 11, 2009 4:23 PM   in reply to russellspivey

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points