note 86181 added to function.json-decode
| From: | steven at acko dot net | Date: | Tue, 07 Oct 2008 07:49:55 +0000 |
| Subject: | note 86181 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145113@lists.php.net to get a copy of this message | ||
json_decode()'s handling of invalid JSON is very flaky, and it is very hard to reliably
determine if the decoding succeeded or not. Observe the following examples, none of which contain
valid JSON:
The following each returns NULL, as you might expect:
var_dump(json_decode('[')); // unmatched bracket
var_dump(json_decode('{')); // unmatched brace
var_dump(json_decode('{}}')); // unmatched brace
var_dump(json_decode('{error error}')); // invalid object key/value
notation
var_dump(json_decode('["\"]')); // unclosed string
var_dump(json_decode('[" \x "]')); // invalid escape code
Yet the following each returns the literal string you passed to it:
var_dump(json_decode(' [')); // unmatched bracket
var_dump(json_decode(' {')); // unmatched brace
var_dump(json_decode(' {}}')); // unmatched brace
var_dump(json_decode(' {error error}')); // invalid object key/value notation
var_dump(json_decode('"\"')); // unclosed string
var_dump(json_decode('" \x "')); // invalid escape code
(this is on PHP 5.2.6)
Reported as a bug, but oddly enough, it was closed as not a bug:
http://bugs.php.net/bug.php?id=45989
----
Server IP: 66.207.199.35
Probable Submitter: 216.19.186.195
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/86181
Del: integrated -- https://master.php.net/note/delete/86181/integrated
Del: useless -- https://master.php.net/note/delete/86181/useless
Del: bad code -- https://master.php.net/note/delete/86181/bad+code
Del: spam -- https://master.php.net/note/delete/86181/spam
Del: non-english -- https://master.php.net/note/delete/86181/non-english
Del: in docs -- https://master.php.net/note/delete/86181/in+docs
Del: other reasons-- https://master.php.net/note/delete/86181
Reject -- https://master.php.net/note/reject/86181
Search -- https://master.php.net/manage/user-notes.php