Bug #61537 [Com]: json_encode() incorrectly truncates/discards information
| From: | yohgaki@php.net | Date: | Wed, 28 Mar 2012 08:11:39 +0000 |
| Subject: | Bug #61537 [Com]: json_encode() incorrectly truncates/discards information | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-168904@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61537&edit=1
ID: 61537
Comment by: yohgaki@php.net
Reported by: joey@php.net
Summary: json_encode() incorrectly truncates/discards
information
Status: Open
Type: Bug
Package: JSON related
Operating System: all
PHP Version: 5.4.0
Block user comment: N
Private report: N
New Comment:
I prefer option 2.
It's an error condition anyway.
Previous Comments:
------------------------------------------------------------------------
[2012-03-28 05:12:08] aharvey@php.net
I think we've got two options here:
1. Change the documented behaviour to note that invalid UTF-8 strings are
encoded as NULL, rather than causing json_encode() to return false, and advise
users to check json_last_error() if they're concerned about the output.
2. Change the json_encode() behaviour to match the documentation. Quick and
dirty POC patch which adds a switch to fall back to the current behaviour:
https://gist.github.com/7735daabc56fb38eb511
Option 2 has a BC concern: we've never advertised json_encode() as not returning
false in this case, but people may rely on it.
Either way, I think we should probably drop the if (PG(display_errors)) { ... }
around the warning in json_escape_string().
Anyone else have thoughts?
------------------------------------------------------------------------
[2012-03-28 05:01:34] joey@php.net
Per documentation:
Return Values: Returns a JSON encoded string on success or FALSE on failure.
------------------------------------------------------------------------
[2012-03-28 05:01:01] joey@php.net
Description:
------------
When json_encode() cannot correctly encode its argument into JSON, the expected
behaviour is that it will return false, and people can check json_last_error() to
see why it failed. Currently, it will replace all data it could not encode with
the string 'null'. This can lead to what appears to be silently discarding data.
Test script:
---------------
<?php
var_dump(json_encode(chr(215)), json_last_error());
Expected result:
----------------
bool(false)
int(5)
Actual result:
--------------
string(4) 'null'
int(5)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61537&edit=1