note 86181 added to function.json-decode

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.