在PHP 5.5中删除了JSON_BIGINT_AS_STRING?

rub*_*o77 12 php json

在我看来,常量JSON_BIGINT_AS_STRINGjson_decode()PHP 5.5中删除.

我使用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')));
Run Code Online (Sandbox Code Playgroud)

无论我在哪里使用json_decode(),我都用这个:

if(USE_JSON_BIGINT_AS_STRING) $j= json_decode($json ,true, 512, JSON_BIGINT_AS_STRING );
else $j=  json_decode($json,true );
Run Code Online (Sandbox Code Playgroud)

tha*_*smt 9

正如这里已经提到的,这个错误似乎来自于pecl-json-c的一个错误版本,由于许可问题,Ubuntu作为php5-json的别名打包.

由于firebase/php-jwt项目,我找到的解决方法是检查JSON_C_VERSION由pecl-json-c设置的常量,而不是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);
}
Run Code Online (Sandbox Code Playgroud)

  • 不会在大字符串里面给出错误,文本里面有大量的数字吗?正则表达式应该只是仍然在qoutes"之外"的数字 (2认同)

And*_*ndy 4

由于许可证问题,某些 Linux 发行版似乎引入了此功能,并且您正在使用受影响的json-c PECL 模块

我的建议是使用该模块的较新版本。