Coldfusion SerializeJSON and deSerializeJSON is converting a string to number

江枫思渺然 提交于 2019-12-11 06:39:37

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/27015059/coldfusion-serializejson-and-deserializejson-is-converting-a-string-to-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!