Jump to content
skaterdav85

json_decode

Recommended Posts

On my local MAMP stack (PHP V 5.3.6), json_decode is taking my JSON and converting long ints to strings. On my production server (PHP V 5.3.2), json_decode is converting long ints to floats. My long ints are Facebook page ID's that looks something like: 755509932534027 I found a workaround that tells Facebook to send me long ints encoded as strings, but how can json_decode behave differently on production? Is it possible to alter the default behavior of this function? Is this common? Thanks!

Share this post


Link to post
Share on other sites

Yeah, the built in JSON functions are sketchy like that.In PHP 5.4.0, they introduce an options parameter that lets you explicitly specify if you want big ints as floats or as strings, with the default (in THAT version) being conversion to floats. Now that there is an option, things are unlikely to change again.The most cross-version way (if you're after that) is to manually check the output of json_decode with is_numeric, and attempt to cast to float if possible. But that's kind'a useless in a loosely typed language like PHP.

Share this post


Link to post
Share on other sites

Ya I saw that about PHP 5.4. This was one tricky bug to find out. Luckily there was an extra query string parameter that I could pass to Facebook to encode large ints as strings, which is what I wanted. All the other solutions I saw used tricky regular expressions which looked nasty.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...