Edit report at https://bugs.php.net/bug.php?id=63004&edit=1

 ID:                 63004
 Comment by:         david at grudl dot com
 Reported by:        juzna dot cz at gmail dot com
 Summary:            errors json_encode do NOT call error handler
 Status:             Not a bug
 Type:               Bug
 Package:            JSON related
 Operating System:   Ubuntu 10.04
 PHP Version:        5.4.6
 Block user comment: N
 Private report:     N

 New Comment:

Common way to avoid warnings in PHP is to use shut-up operator:

$handle = @fopen($file, 'r');

It is not ideal solution, but it is used in whole PHP. Standard. With just one 
exception:

$old = ini_set('display_errors', 1); 
$json = json_encode($args);
ini_set('display_errors', $old);


Why json_encode() is exception?


Previous Comments:
------------------------------------------------------------------------
[2012-09-04 06:01:05] ni...@php.net

By the way, in PHP 5.5 the behavior here changes and there is no warning at 
all. The error will be available via json_last_error() and a second function 
which returns a human readable string instead of an error code.

------------------------------------------------------------------------
[2012-09-04 03:28:46] ras...@php.net

It isn't a new mechanism for PHP. We have had things like mysql_error(), 
socket_last_error(), oci_error(), ldap_error(), pg_last_error(), 
libxml_get_errors(), preg_last_error(), curl_error() and many money for a very 
long time.

The main reason to not surface a warning here is that the only way to avoid it 
would be to call iconv('utf-8','utf-8',$str) on all strings to be encoded. This 
is a huge hassle to do, it is slow, and this is something we actually do 
internally in json_encode() to validate utf-8 anyway, so it would be entirely 
redundant. And since in many cases you end up passing user data or at least 3rd-
party data directly to json_encode() you would have to always add this 
redundant 
check.

------------------------------------------------------------------------
[2012-09-04 03:17:44] david at grudl dot com

This is the only one function in whole PHP with this behaviour. So is it a new 
way of error handling or bug?

------------------------------------------------------------------------
[2012-09-03 23:36:20] ras...@php.net

json_encode() now checks for valid utf-8. It makes no sense to generate 
warnings 
for core functionality of the function. You can check json_last_error() for 
JSON_ERROR_UTF8 if you want to programmatically catch invalid utf-8.

------------------------------------------------------------------------
[2012-09-03 22:15:55] juzna dot cz at gmail dot com

Actually, a similar bug (52397) has been known for more than 2 years. In latest 
snapshot of PHP 5.4 it just got worse :/

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=63004


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63004&edit=1

Reply via email to