Jump to content

json_decode


skaterdav85

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!

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...