|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-09-28 10:31 UTC] taligahack at gmail dot com
Description: ------------ --- From manual page: http://www.php.net/function.json-decode --- All php/json versions are affected. The reason i'm writing this bug report/feature request is because the failed conversion of the empty string to array is falls back to string output, but not resulting NULL or an empty array. There's also no way to find out the conversion is failed while the optional parameter should force the method to do the conversion (which was obviously impossible). "Return values" foot note in the json_decode() documentation says: "NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit." Which is also misleading. While the json actually decoded, the given assoc=true flag should invalidate this result, since the decoded content never made it to an object but a string. Therefore, the expected result would be NULL, not the string. It is also important to note here, that the json_last_error() unfortunately fails to report the conversion issue, therefore json_decode() fails completely silent. The json_last_error() shall report an illegal object/type conversion error, because the json_decode() is forced to convert an illegal type. Test script: --------------- var_dump( json_decode( json_encode( "" ) , true ) ); var_dump( json_last_error( ) ); Expected result: ---------------- null int ? > 0 Actual result: -------------- string '' (length=0) int 0 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 11:00:01 2025 UTC |
i think it's easy to fix this bug at first. however, there's some embarrassment behavior in decode and encode. if you decode "", there's empty in the string. so var_dump(json_decode("")) will get null. then, i try to test your case json_encode(""); it will get """" , because you want to convert php string to json string. the outer quot will read by php, the inner one will read by json. so, json_encode return string(2) """" if you decode """", it convert to "" naturally. that's embrrass but not a wrong bahavior.set assoc = true means a json object would convert to php associative array. there's only json empty string, so it won't return any array. if you want to return php associative array, at least have "{" and "}", it will convert to php associative array when you set assoc = true (if set assoc = false, it return php object)