Skip to main content
Inspiring
August 23, 2013
Answered

serializeJSON is converting ints to floats

  • August 23, 2013
  • 1 reply
  • 517 views

CF10 update 11 Win7 x64

I'm running into a problem that I just noticed with serializeJSON()... ints are being converted to floats. Here's the code:

<cfset r = {

    "x": 0,

    "y": 1

}>

<cfoutput><pre>#serializeJSON(r)#</pre></cfoutput>

<cfset r.x = int(Val("1"))>

<cfset r.y = ceiling(100/10)>

<cfoutput><pre>#serializeJSON(r)#</pre></cfoutput>

Here's the output:

{"y":1,"x":0}

{"y":10.0,"x":1.0}

Is this messed up or what?

This topic has been closed for replies.
Correct answer redtopia-dev

Also found out that a simple addition operation creates a float:

<cfset r["z"] = 1 + 2> will output 3.0

Workaround:

<cfset r.x = javaCast("int", Val("1"))>

<cfset r.y = javaCast("int", ceiling(100/10))>



1 reply

redtopia-devAuthorCorrect answer
Inspiring
August 23, 2013

Also found out that a simple addition operation creates a float:

<cfset r["z"] = 1 + 2> will output 3.0

Workaround:

<cfset r.x = javaCast("int", Val("1"))>

<cfset r.y = javaCast("int", ceiling(100/10))>