PHP5 json_decode bug
With my latest project TweSMS I decided to go the json route with the twitter API. Now if you have worked with the twitter API before you would know that twitter has some serious big id values thus it was important to store these values in big int fields in the database.
I first discovered the bug when I had to get mention ids from twitter and saw that every time I get the same id returned, the id returned was 2147483647 the maximum value a 32 bit integer can store. After some investigation I found that the php5 json_decode function converted big int values to a normal 32bit integer. With the help of some regex I soon came up with a fix for this, the fix is to convert the integer to a string.
function jsonIntToStr($json){
$pattern = “/\”id\”:([0-9]+),/”;
$replace = “\”id\”:\”$1\”,”;
$new_json = preg_replace($pattern, $replace, $json);
return $new_json;
}
To use this replace id with the json fields you are trying to convert to string and call it like this json_decode(jsonIntToStr($jsonstring))



