ID: 45989
User updated by: steven at acko dot net
Reported By: steven at acko dot net
Status: Bogus
Bug Type: JSON related
Operating System: Mac OS X
PHP Version: 5.2.6
New Comment:
Please clarify the bogus classification.
The following each returns NULL, as expected:
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 argument as a string:
var_dump(json_decode(' ['));
var_dump(json_decode(' {'));
var_dump(json_decode(' {}}'));
var_dump(json_decode(' {error error}'));
var_dump(json_decode('"\"'));
var_dump(json_decode('" \x "'));
Please examine the examples closely: they are all meaningless, invalid
JSON. Even under the
most widely stretched definition of JSON, the above is not JSON encoded
data. Yet
json_decode() arbitarily returns /some of it/ as a string... and in a
way that looks
suspiciously like a bad parser implementation.
If this was merely a case of json_decode() returning /all/ invalid json
as is, then it could
be classified as an implementation quirk. But because of how
inconsistent it is now, you
can't say that it is by design or following any kind of spec.
E.g. how would you currently see if json_decode() succeeded or not?
Previous Comments:
------------------------------------------------------------------------
[2008-09-10 00:38:09] [EMAIL PROTECTED]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
.
------------------------------------------------------------------------
[2008-09-04 00:32:20] steven at acko dot net
Description:
------------
When json_decode() is given certain invalid JSON strings, it will
return
the literal string as the result, rather than returning NULL.
Note: in #38680, the decision was made to allow json_decode() to accept
literal basic types (strings, ints, ...) even though this is not
allowed
by RFC 4627 (which only allows objects/arrays). This bug report is
different because even under the PHP interpretation of JSON, these
strings can not be considered valid, and trivial variations on them do
in fact throw an error as expected.
(The non-standard behaviour introduced in #38680 is not documented at
all by the way, which is kind of ironic given the numerous issues that
have 'go read the spec' as the answer)
Reproduce code:
---------------
var_dump(json_decode("'invalid json'"));
var_dump(json_decode('invalid json'));
var_dump(json_decode(' {'));
var_dump(json_decode(' ['));
Expected result:
----------------
NULL
NULL
NULL
NULL
Actual result:
--------------
string(14) "'invalid json'"
string(12) "invalid json"
string(2) " {"
string(2) " ["
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45989&edit=1