Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Integers are converting to scientific notation - ColdFusion

I am trying to pass a struct through a function, but the integers in it are converting to scientific notation.

Before deSerialization :

{"businessUnitValidList":2003051509034372557922
  , "shortMessage":"Success"
  , "longMessage":"Request Completed Successfully."
  , "status":20001
} 

After deSerialization:

 businessUnitValidList  2.00305150903E+021 

I have tried converting it into a string but it still gives me the same output. Any ideas?

Note: If I have more than one value in my businessUnitValidList, the numbers show up the way they are supposed to.

EDIT

This is the current code iteration:

<cfloop array="#businessUnitArray#" index="i">

   <cfquery name="validatebusinessUnit" datasource="dbproduction">
       select doctorid from survey.dbo.clientLocationMap
       where clientbrandid = '#arguments.clientBrandid#'
       and clientLocation = '#i#'
   </cfquery>

   <cfif validatebusinessUnit.recordcount gt 0>
       <cfset businessUnitValidList = listAppend(businessUnitValidList,toString(validatebusinessUnit.doctorid),",")>
   <cfelse>
       <cfset businessUnitInValidList = listAppend(businessUnitInValidList,i,",")>
   </cfif>

</cfloop>

<cfif businessUnitInValidList neq ''>
    <cfset ResponseStruct['BusinessUnitCodes']['businessUnitMixResponse']['businessUnitInValidList'] = "#businessUnitInValidList#">
    <cfset ResponseStruct['BusinessUnitCodes']['businessUnitMixResponse']['businessUnitValidList'] = "#businessUnitValidList#">
    <cfreturn serializeJSON(ResponseStruct['BusinessUnitCodes']['businessUnitMixResponse'])>
<cfelse>
    <cfset ResponseStruct['BusinessUnitCodes']['businessUnitSuccess']['businessUnitValidList'] = "#businessUnitValidList#">
    <cfreturn serializeJSON(ResponseStruct['BusinessUnitCodes']['businessUnitSuccess'])>
</cfif>
like image 847
Geo Avatar asked Mar 17 '16 11:03

Geo


Video Answer


1 Answers

ColdFusion's JSON serialization has issues and can vary between versions and even hotfixes. As Jedihomer Townend mentioned in the comments a leading space should force CF to treat it a string and not cast it.

I've just tried this on CF10, 11 and 2016 and preserves the input.

<cfscript>
a = {
  "businessUnitValidList":" 2003051509034372557922",
  "shortMessage":"Success",
  "longMessage":"Request Completed Successfully.",
  "status":20001
};
json = serializeJSON(a);
b = deserializeJSON(json);

writeDump(b);
</cfscript>

You can try it here:

http://trycf.com/gist/70b86fbb57f752125f35/acf?theme=monokai

like image 95
John Whish Avatar answered Sep 27 '22 22:09

John Whish