Arun Pati
Arun Pati

Reputation: 125

Coldfusion SerializeJSON and deSerializeJSON is converting a string to number

ColdFusion is converting a string to number when passing to JS via a SerializeJSON and deSerializeJSON.

This is only happening when an 'E' is used between two set of numbers. like 3E6, 65E3, 56e45 etc. This is the code inside cfscript.

x = "2e9";
writedump(SerializeJSON(x));
writedump(deSerializeJSON(SerializeJSON(x)));

Output:
2.0E9 2000000000 

Please suggest, if is there any other way for such issues.

Upvotes: 1

Views: 1818

Answers (2)

narinder singh kanwar
narinder singh kanwar

Reputation: 11

I have resolved this using below solution

let's say ItemUnit = 12E45

stcReturn.firstname = "Yes";
stcReturn.lastname = "Man";
stcReturn.ItemUnit = "12E45"

error output after deSerializeJSON stcReturn.ItemUnit = 12e+46

<cfset stcReturn.ItemUnit  = ItemUnit />    
<cfset StructSetMetaData(stcReturn.ItemUnit, {ItemUnit : 
{"type":"string","name":"ItemUnit"}})/>

deSerializeJSON(stcReturn)

Correct Output:12E45

Upvotes: 1

Adam Cameron
Adam Cameron

Reputation: 29870

It is this: https://bugbase.adobe.com/index.cfm?event=bug&id=3695627: "SerializeJSON turns strings that look like scientific notation into floats."

It's a known bug in CF9, and it's fixed in CF10.

In the meantime, you will just have to pad the string with something to force ColdFusion to not see it as a number in scientific notation.

Or upgrade to CF10 (CF9 is end of life next month, btw). Or to Railo.

Upvotes: 8

Related Questions