在php 5.5中删除了JSON_BIGINT_AS_STRING? [英] JSON_BIGINT_AS_STRING removed in php 5.5?
问题描述
在我看来,常量JSON_BIGINT_AS_STRING
已从 json_decode()
中删除PHP 5.5.
It seems to me that the constant JSON_BIGINT_AS_STRING
is removed from json_decode()
in PHP 5.5.
我使用PHP"5.5.3-1ubuntu2"(Ubuntu 13.10),并从PHP 5.4(Ubuntu 13.04)更新以来收到此错误:
I use PHP "5.5.3-1ubuntu2" (Ubuntu 13.10) and got this error since the update from PHP 5.4 (Ubuntu 13.04):
警告:json_decode():JSON_BIGINT_AS_STRING选项未在...中实现
Warning: json_decode(): option JSON_BIGINT_AS_STRING not implemented in ...
是否有证据表明已将其删除?
Is there any evidence that this is removed?
我不需要该函数,所以我添加了此常量:
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')));
在我使用json_decode()的任何地方,我都使用这个:
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 );
推荐答案
正如此处已提到的,此错误似乎来自pecl-json-c的错误版本,由于以下原因,Ubuntu将其打包为php5-json的别名:许可问题.
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.
由于 firebase/php-jwt 项目,我发现的解决方法是检查由pecl-json-c设置的JSON_C_VERSION
常量,而不是USE_JSON_BIGINT_AS_STRING
. (由于定义了USE_JSON_BIGINT_AS_STRING
,但未实现).
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).
这是来自 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);
}
这篇关于在php 5.5中删除了JSON_BIGINT_AS_STRING?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!