Bug #52829 [Opn->Ana]: json_decode looses data
| From: | aharvey@php.net | Date: | Tue, 14 Sep 2010 05:48:57 +0000 |
| Subject: | Bug #52829 [Opn->Ana]: json_decode looses data | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-154319@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52829&edit=1
ID: 52829
Updated by: aharvey@php.net
Reported by: pzbowen at gmail dot com
Summary: json_decode looses data
-Status: Open
+Status: Analyzed
Type: Bug
Package: JSON related
Operating System: Linux
PHP Version: 5.2.14
Block user comment: N
New Comment:
The attached patch against trunk would cause the JSON parser to emit
warnings for duplicate keys. The behaviour otherwise would be unchanged;
the last value would be the one in the returned object/array.
I'm holding off on committing it, though, for two reasons:
1. I'm not thrilled about emitting warnings, since they're difficult to
handle. We could return false from json_decode() instead and add a
return value to json_last_error() along the lines of
JSON_ERROR_DUPLICATE_NAME, but that doesn't seem any better to me: I
don't think this should stop decoding if the file is otherwise valid.
2. There is a performance impact from doing the extra checks. Cursory
benchmarking would suggest it's on the order of a 5% slowdown in
decoding speed.
So, if we decide we can live with the speed hit, I'd still like to hear
any bright ideas people might have for signalling these warnings in a
way that's easy for users to handle and still allows decoding to succeed
where possible.
Previous Comments:
------------------------------------------------------------------------
[2010-09-14 07:48:42] aharvey@php.net
The following patch has been added/updated:
Patch Name: json-decode-warnings
Revision: 1284443322
URL:
http://bugs.php.net/patch-display.php?bug=52829&patch=json-decode-warnings&revision=1284443322
------------------------------------------------------------------------
[2010-09-13 22:03:03] pzbowen at gmail dot com
Agreed, no easy way to handle it, so it should return an error when this
situation is encountered.
------------------------------------------------------------------------
[2010-09-13 21:49:13] kalle@php.net
I can't see any really easy way to achieve this since properties and
array keys are unique in PHP. Perhaps some overloading with objects but
that would be dirty/hackish
------------------------------------------------------------------------
[2010-09-13 20:31:49] pzbowen at gmail dot com
Description:
------------
According to RFC 4627, section 2.2, "The names within an object SHOULD
be unique." This is only a SHOULD not a MUST, so the following is
valid
{"foo":2,"foo":3}
Unfortunately in PHP this only returns the last object member.
Test script:
---------------
var_dump(json_decode('{"foo":2,"foo":3}'));
Expected result:
----------------
an object with a way of accessing both members or an error raised by
json_decode
Actual result:
--------------
object(stdClass)#1 (1) {
["foo"]=>
int(3)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52829&edit=1