12

PHP 5.5 で定数JSON_BIGINT_AS_STRINGが削除されたようです。json_decode()

PHP "5.5.3-1ubuntu2" (Ubuntu 13.10) を使用していますが、PHP 5.4 (Ubuntu 13.04) からの更新以降、次のエラーが発生しました。

警告: json_decode(): オプション JSON_BIGINT_AS_STRING が実装されていません ...

これが削除されたという証拠はありますか?


編集:

その関数は必要ないので、次の定数を追加しました。

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

json_decode() を使用する場合は常に、これを使用します。

if(USE_JSON_BIGINT_AS_STRING) $j= json_decode($json ,true, 512, JSON_BIGINT_AS_STRING );
else $j=  json_decode($json,true );
4

2 に答える 2

9

ここで既に述べたように、このエラーは、ライセンスの問題により、Ubuntu が php5-json のエイリアスとしてパッケージ化している pecl-json-c のバグのあるバージョンから発生しているようです。

firebase/php-jwtプロジェクトのおかげで私が見つけたJSON_C_VERSION回避策は、USE_JSON_BIGINT_AS_STRING. (USE_JSON_BIGINT_AS_STRING定義されているため、実装されていません)。

JWT プロジェクトのコードは次のとおりです。

<?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);
}
于 2015-01-12T20:06:03.400 に答える