|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-03-28 05:01 UTC] 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) PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
The patch submitted fixes the problem. json_ecode() has been incorrectly documented all along. Instead of json_encode() returning false it has been gladly returning a string containing "null" to replace invalid data. Now, "null" is valid json, but the problem is the user never ends up knowing that json_encode() silently failed unless they explicitly check json_last_error() every single time they call json_encode(). Clearly this was not the documented behavior. It should be the case that when the user tries... <?php if (!(json_encode() = $json)) { /* Then we have valid json to work with and there is no error */ } else { /* json_encode() failed and we should check json_last_error() in order to know why */ ?> I also submitted a patch to fix the problem not realizing there was another patch already there which fixes it too and allows it throw the error. https://github.com/srgoogleguy/php- src/commit/ba181fce117a3a7d1c7668cf30744b3d7cd7cdbf However, now there is a test that's going to fail ext/json/tests/bug43941.phpt which is testing for undocumented behavior. It was in response to this older bug report: https://bugs.php.net/bug.php?id=43941 However, that fix never really fixed the problem. It just made it worse. In my opinion we should remove this test. I just wanted aharvey@php.net to also look at in case they feel the test should remain or be modified to comply with the spec. Thanks.