• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

serializeJSON() converts long numbers into scientific notation

New Here ,
Jul 11, 2009 Jul 11, 2009

Copy link to clipboard

Copied

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.

Views

3.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 11, 2009 Jul 11, 2009

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation