Chloe Chloe - 27 days ago 6
JSON Question

How do I ignore PHP Notice: json_decode(): integer overflow detected in Yii?

I'm trying to decode a long integer in JSON, but it always crashes and gives an error in Yii. I already use the

JSON_BIGINT_AS_STRING
option. How do I bypass this error or ignore it?

php > var_dump( json_decode('[66933258,"B009GQ034C",281441845828]', false, 512, JSON_BIGINT_AS_STRING));
PHP Notice: json_decode(): integer overflow detected in php shell code on line 1
array(3) {
[0]=>
int(66933258)
[1]=>
string(10) "B009GQ034C"
[2]=>
string(19) "9223372036854775807"
}


In the app, it gives


PHP Notice – yii\base\ErrorException
json_decode(): integer overflow detected

Answer Source

Just tried your code and on my machine it works perfectly, perhaps it has something to do with your php version or so?

The only thing I can think of is (if you don't need the values as a number, but just the values) to use preg_replace to "escape" the numbers as strings first:

$json = '[66933258,"B009GQ034C",281441845828]'; 
var_dump(json_decode(preg_replace('/(\W)(\d+)(\W)/', '\\1"\\2"\\3', $json)));

Will yield this:

array (size=3)
  0 => string '66933258' (length=8)
  1 => string 'B009GQ034C' (length=10)
  2 => string '281441845828' (length=12)

Edit:

Now that I'm looking a bit closer: You are actually getting a value on the command line. So the JSON_BIGINT_AS_STRING is working correctly. The problem is that the json_decode()-function seems to generate a notice before it switches to this behavior. Yii's error handler captures notices by default and converts them into an exception.

In this case the solution might be as simple as:

var_dump(@json_decode('[66933258,"B009GQ034C",281441845828]', false, 512, JSON_BIGINT_AS_STRING));

I would normally advise against using the silence operator because it obscures errors, but in this case it beats the alternative (disabling capturing notices and possibly missing other errors)