|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-05-15 15:07 UTC] bukka@php.net
-Package: json
+Package: JSON related
[2016-09-04 10:55 UTC] bukka@php.net
-Summary: fix json_encode()
+Summary: JSON_PARTIAL_OUTPUT_ON_ERROR can result in JSON with
null key
-Status: Open
+Status: Duplicate
[2016-09-04 10:56 UTC] bukka@php.net
-Status: Duplicate
+Status: Open
[2016-09-04 10:57 UTC] bukka@php.net
-Type: Feature/Change Request
+Type: Bug
[2017-10-15 18:37 UTC] bukka@php.net
[2017-10-15 18:37 UTC] bukka@php.net
-Status: Open
+Status: Closed
[2017-10-15 18:38 UTC] bukka@php.net
[2017-10-15 18:43 UTC] bukka@php.net
-Assigned To:
+Assigned To: bukka
[2017-10-15 18:43 UTC] bukka@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
Description: ------------ json_encode(array("\x80" => 1)) returns "{null: 1}" in PHP5.3 and PHP5.4. It's not a valid JSON, but at least we could use json_last_error() to catch it. Encoding recursion, INF/NAN or unsupported type results in a VALID json so that json_last_error() returns 0 (no error) and only emit E_WARNING. In fact, all JSON_ERROR_* constants are only for json_decode() until 5.3.3. From PHP 5.5, json_encode() does not emit E_WARNING anymore, JSON_ERROR_RECURSION/INF_OR_NAN/UNSUPPORTED_TYPE are added and json_last_error() may return them after json_encode(). (And it also possible return JSON_ERROR_DEPTH for the new third param of json_encode.) The problem is, there is NO way to be tolerant to these cases in PHP 5.5+! The undocumented option JSON_PARTIAL_OUTPUT_ON_ERROR is USELESS because json_last_error() only returns the last error, means there may be utf-8 error which results in invalid json. There are two possible solution: 1. When json_encode with JSON_PARTIAL_OUTPUT_ON_ERROR, encode array("\x80" => 1) to {"": 1} or {} (just drop the node) to ensure valid json output. 2. Implement new options like https://bugs.php.net/bug.php?id=65082 , and add more options to allow indicate how to treat recursion, Inf, NaN, unsupported type respectively. We'd better implement both, 1 for backward compatibility and 2 for drop unreliable json_last_error().