php - JSON_BIGINT_AS_STRING 在 php 5.5 中被删除?

标签 php json

在我看来,常量 JSON_BIGINT_AS_STRING 已从 json_decode() 中删除。在 PHP 5.5 中。

我使用 PHP“5.5.3-1ubuntu2”(Ubuntu 13.10),自 PHP 5.4 (Ubuntu 13.04) 更新以来出现此错误:

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

有任何证据表明该内容已被删除吗?

<小时/>

编辑:

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

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 );

最佳答案

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

感谢firebase/php-jwt,我找到了解决方法项目的目的是检查 pecl-json-c 设置的 JSON_C_VERSION 常量,而不是 USE_JSON_BIGINT_AS_STRING。 (因为 USE_JSON_BIGINT_AS_STRING 已定义,但未实现)。

这是来自 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);
}

关于php - JSON_BIGINT_AS_STRING 在 php 5.5 中被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520487/

相关文章:

javascript - 从弹出窗口获取 JSON 结果。 Angular .JS

java - 消除 Java Spring 多对多关系中的循环 JSON

php - 检查json网址工程file_get_contents

jquery - JSON Post 调用错误函数

smtp - 我的服务器和 php mail() 函数将电子邮件发送到垃圾邮件文件夹

php - 如何在 PHP 中实现基于属性的访问控制 (ABAC)

php - 在结果中附加 mysql 数据

jquery - Bootstrap Carousel JSON 加载缓慢

php - 复合外键原则 Symfony 2 的重叠非主键

php - 在数据库中找到的 Set product new from date 在哪里?