Req #68567 [Dup->Opn]: JSON_PARTIAL_OUTPUT_ON_ERROR can result in JSON with null key

From: Date: Sun, 04 Sep 2016 10:56:36 +0000
Subject: Req #68567 [Dup->Opn]: JSON_PARTIAL_OUTPUT_ON_ERROR can result in JSON with null key
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203783@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68567&edit=1 ID: 68567 Updated by: bukka@php.net Reported by: johnhax at gmail dot com Summary: JSON_PARTIAL_OUTPUT_ON_ERROR can result in JSON with null key -Status: Duplicate +Status: Open Type: Feature/Change Request Package: JSON related PHP Version: 5.6.3 Block user comment: N Private report: N Previous Comments: ------------------------------------------------------------------------ [2014-12-08 12:57:47] johnhax at gmail dot com 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(). ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68567&edit=1

« previous php.bugs (#203783) next »