Created
March 22, 2012 19:42
-
-
Save bcantoni/2162791 to your computer and use it in GitHub Desktop.
Demo of PHP workaround for large integers in JSON data responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Demonstration of problem with large integers in JSON, and | |
a couple options for dealing with them as strings instead. | |
ref: | |
* http://php.net/manual/en/function.json-decode.php | |
* http://stackoverflow.com/questions/2907806/handling-big-user-ids-returned-by-fql-in-php | |
*/ | |
$json = <<<EOT | |
{ | |
"foo" : "bar", | |
"small" : "123456", | |
"large" : 200000000000009093302 | |
} | |
EOT; | |
print "Original JSON data:\n$json\n"; | |
// try normal JSON decode | |
$obj = json_decode ($json); | |
print "\nOriginal JSON decode results:\n"; | |
var_dump ($obj); | |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { | |
// if on PHP 5.4 or newer, use JSON "bigint" option | |
$obj = json_decode ($json, false, 512, JSON_BIGINT_AS_STRING); | |
print "\nJSON BIGINT decode (PHP 5.4+): \n"; | |
var_dump ($obj); | |
} else { | |
// otherwise try workaround (convert number to string first) | |
$json2 = fixJson ($json); | |
$obj = json_decode ($json2); | |
print "\nFixed JSON data:\n$json2\n"; | |
print "\nFixed JSON decode: \n"; | |
var_dump ($obj); | |
} | |
/* fixJson - work around large integer issue by adding quotes around | |
any large numeric values - causes them to be interpreted as strings | |
*/ | |
function fixJson ($json) { | |
return (preg_replace ('/:\s?(\d{14,})/', ': "${1}"', $json)); | |
} |
- The workaround for PHP 5 should not be needed nowadays.
- Your approach completely disregards arrays of integer or integers in arrays.
But since a major oversight on PHP's part is that browsers don't choke on integers starting with BigInt (64 bits), but before that (53 bits). So my code is trying to remedy that.
Trying to use regular expression functions on JSON strings is almost as futile as using them on HTML. So my approach is to assume PHP is current and handle the decoded array:
function fix_large_int(&$value)
{
if (is_int($value) && $value > 9007199254740991)
$value = strval($value);
}
$json_arr = json_decode($json_str, flags: JSON_BIGINT_AS_STRING | JSON_OBJECT_AS_ARRAY);
echo('before: ' . json_encode($json_arr) . '<br />' . PHP_EOL);
array_walk_recursive($json_arr, 'fix_large_int');
echo('after: ' . json_encode($json_arr) . '<br />' . PHP_EOL);
The number 9007199254740991 is what JavaScript has as its Number.MAX_SAFE_INTEGER
value. Read about that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! I modified your regular expression to this:
So it can handle json strings such as.
I'm not good at regular expressions so it could be better.