Skip to content

Instantly share code, notes, and snippets.

@bcantoni
Created March 22, 2012 19:42
Show Gist options
  • Save bcantoni/2162791 to your computer and use it in GitHub Desktop.
Save bcantoni/2162791 to your computer and use it in GitHub Desktop.
Demo of PHP workaround for large integers in JSON data responses
<?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));
}
@joseadrian
Copy link

Hi! I modified your regular expression to this:

preg_replace('/\"([a-zA-Z0-9_]+)\":\s?(\d{14,})/', '"${1}":"${2}"', $json);
// what I made before modifying yours was this \:([0-9]+)

So it can handle json strings such as.

$json = <<<EOT
{
   "foo" : "bar",
   "small" : "123456",
   "large" : 200000000000009093302,
   "text" : "Example ratio 1000000000000000:1"
}
EOT;

I'm not good at regular expressions so it could be better.

@Alien426
Copy link

  1. The workaround for PHP 5 should not be needed nowadays.
  2. 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