Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON_BIGINT_AS_STRING removed in php 5.5?

Tags:

json

php

It seems to me that the constant JSON_BIGINT_AS_STRING is removed from json_decode() in PHP 5.5.

I use PHP "5.5.3-1ubuntu2" (Ubuntu 13.10) and got this error since the update from PHP 5.4 (Ubuntu 13.04):

Warning: json_decode(): option JSON_BIGINT_AS_STRING not implemented in ...

Is there any evidence that this is removed?


EDIT:

I don't need that function so I added this constant:

define('USE_JSON_BIGINT_AS_STRING',(!version_compare(PHP_VERSION,'5.5', '>=') and defined('JSON_BIGINT_AS_STRING')));

and wherever I use json_decode(), I use this:

if(USE_JSON_BIGINT_AS_STRING) $j= json_decode($json ,true, 512, JSON_BIGINT_AS_STRING );
else $j=  json_decode($json,true );
like image 502
rubo77 Avatar asked Oct 22 '13 14:10

rubo77


2 Answers

As mentioned here already, this error seems to come from a buggy version of pecl-json-c, which Ubuntu packages as an alias for php5-json because of licensing issues.

A work around that I found, thanks to the firebase/php-jwt project, is to check for the JSON_C_VERSION constant that is set by pecl-json-c, instead of USE_JSON_BIGINT_AS_STRING. (Since USE_JSON_BIGINT_AS_STRING is defined, but not implemented).

Here is the code from the JWT project:

<?php
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
    /** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
     * to specify that large ints (like Steam Transaction IDs) should be treated as
     * strings, rather than the PHP default behaviour of converting them to floats.
     */
    $obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
} else {
    /** Not all servers will support that, however, so for older versions we must
     * manually detect large ints in the JSON string and quote them (thus converting
     *them to strings) before decoding, hence the preg_replace() call.
     */
    $max_int_length = strlen((string) PHP_INT_MAX) - 1;
    $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
    $obj = json_decode($json_without_bigints);
}
like image 191
thaddeusmt Avatar answered Oct 22 '22 16:10

thaddeusmt


It looks like this was introduced to some Linux distributions due to LICENSE concerns and you are using an affected json-c PECL module.

My suggestion would be to use an newer version of the module.

like image 24
Andy Avatar answered Oct 22 '22 15:10

Andy