Re: json_encode() behavior for incorrectly encoded strings

From: Date: Sat, 23 Jun 2012 22:50:13 +0000
Subject: Re: json_encode() behavior for incorrectly encoded strings
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-60951@lists.php.net to get a copy of this message
On Sat, 23 Jun 2012 21:53:01 +0200, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
Hi!
The warning for invalid UTF-8 stays intact and is thrown also with display_errors = On. If this behavior is undesired this can be remedied later.
Must we discuss it 1000 times anew? There was a reason why it didn't throw warning when display_errors is on. The reason is that it is very easy to feed the server configured with display_errors = on wrong JSON and thus force it to reveal information, and this is largely beyond the control of application writer.
I find this argument unconvincing. If json_encode() is not supposed to receive badly encoded data, then it's the application writer's responsibility to make sure that the data is correctly encoded. If, on the other hand, you think that's actually not a error on the part of the caller to call json_encode() with badly encoded data (i.e., just like htmlspecialchars() -- from which I removed the warning removed in 5.4 --, json_encode() also accumulates the responsibility validating some aspects of the input), then there's no reason to have a warning at all. In other words, if it's a precondition of json_encode() that the input is correctly encoded (and the check is just a form of safe programming), then there's indeed an error on the part of the application writer, if it isn't, then there's no reason for any sort of warning. (By the way, I'd prefer no warning at all, both for this and for inf/NAN. We already can get the error from json_last_error() if we want to) -- Gustavo Lopes

« previous php.internals (#60951) next »