首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php5.5中删除了JSON_BIGINT_AS_STRING?

问在php5.5中删除了JSON_BIGINT_AS_STRING?
EN

Stack Overflow用户
提问于 2013-10-22 14:20:46
回答 2查看 7.1K关注 0票数 12

在我看来,PHP5.5中的常量JSON_BIGINT_AS_STRING已从json_decode()中删除。

我使用PHP "5.5.3-1ubuntu2“(Ubuntu13.10),并从PHP5.4(Ubuntu13.04)更新后获得了此错误:

警告: json_decode():选项JSON_BIGINT_AS_STRING未在.

有证据证明这件事被删除了吗?

编辑:

我不需要这个函数,所以我添加了一个常量:

代码语言:javascript
复制
define('USE_JSON_BIGINT_AS_STRING',(!version_compare(PHP_VERSION,'5.5', '>=') and defined('JSON_BIGINT_AS_STRING')));

无论在何处使用json_decode(),我都使用以下内容:

代码语言:javascript
复制
if(USE_JSON_BIGINT_AS_STRING) $j= json_decode($json ,true, 512, JSON_BIGINT_AS_STRING );
else $j=  json_decode($json,true );
EN

回答 2

Stack Overflow用户

发布于 2015-01-12 20:06:03

如前所述,此错误似乎来自于一个buggy版本的pecl c,由于许可证问题,Ubuntu将其打包为php5-json的别名。

感谢firebase/php-jwt项目,我发现的一项工作是检查由project c而不是USE_JSON_BIGINT_AS_STRING设置的JSON_C_VERSION常量。(因为USE_JSON_BIGINT_AS_STRING是定义的,但没有实现)。

下面是来自JWT项目的代码

代码语言:javascript
复制
<?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);
}
票数 9
EN

Stack Overflow用户

发布于 2013-10-22 15:07:31

看起来,由于许可证问题,对于某些Linux发行版来说,这是介绍,而且您使用的是受影响的json-c PECL模块。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19520487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档