#46600 [Ver]: "_empty_" key in objects (see #41504)

From: Date: Thu, 04 Dec 2008 20:39:13 +0000
Subject: #46600 [Ver]: "_empty_" key in objects (see #41504)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-131603@lists.php.net to get a copy of this message
 ID:               46600
 User updated by:  Matt at mpcm dot com
 Reported By:      Matt at mpcm dot com
 Status:           Verified
 Bug Type:         JSON related
 Operating System: *
 PHP Version:      5CVS, 6CVS (2008-11-18)
 New Comment:

Thanx for the reply magicaltux, Feature is an interesting word
considering the possible key collision.

There are other ways to get things that are not strictly objects to
behave that way. Overloading (like example #1 at
http://us.php.net/manual/en/language.oop5.overloading.php).
It works
well enough as long as you also make it iterate correctly.

What I am suggesting is that it is better to fail in decoding into an
object, than to silently cause a key collision. 

Or alternatively, produce an overloaded object which can have these
keys (by default, or passed optional flag) from json_decode. It's an
opinion, but a wrapper gets me where I need to be for now, and I'm
pretty sure this is an edge case.

$a = '{"":"a","_empty_":"b"}';
echo json_encode(json_decode($a)); 
echo json_encode(json_decode($a, true));

output:
{"_empty_":"b"}
{"":"a","_empty_":"b"}

for some values:
$a != json_encode(json_decode($a));


Previous Comments:
------------------------------------------------------------------------

[2008-12-04 10:44:29] magicaltux@php.net

I believe this is not a bug, but a feature.

An object *can't* have an empty property (while an array can).

If you want to use json_decode() with json containing empty key, either
access those empty key using special keyword "_empty_", or put the
optionnal $assoc parameter of json_decode() to true to get result as an
array.

If you want objects to support empty keys, I believe this is not going
to happen soon, as this is enforced by a specific error message.

Fatal error: Cannot access empty property in php shell code on line 1

Please note that a property name starting with NULL character won't
work either.

------------------------------------------------------------------------

[2008-11-18 17:35:30] matt at mpcm dot com

The language seems to create a key that cannot be reached, so even if
this bug is fixed, we am still facing a broader issue it seems.

<?
$key = "";
$o = (object) array($key=>4,"example"=>8);
var_dump($o);
print 'blank key test:' . (isset($o->$key)?'true':'false');
print $o->{$key};
var_dump($o->$key);
?>

output:
object(stdClass)#1 (2) {
  [""]=>
  int(4)
  ["example"]=>
  int(8)
}
blank key test:false<br />
<b>Fatal error</b>:  Cannot access empty property in
<b>PHPDocument1</b> on line <b>8</b><br />
All throws Notice: line 4 - Illegal member variable name

------------------------------------------------------------------------

[2008-11-18 15:43:05] matt at mpcm dot com

All the work arounds I am looking at are throwing Error Text: Illegal
member variable name when I convert/cast an object with a blank
property.

Is this json_decode bug a result of a larger object mechanism
limitation inside of php's object handling?

------------------------------------------------------------------------

[2008-11-18 03:13:51] Matt at mpcm dot com

Description:
------------
json_decode() treats empty property name as "_empty_" not "". This was
fixed in #41504 for arrays, but not for objects. (seems to happen in PHP
Version 5.2.4-2ubuntu5.3 and 5.3.0alpha2.

Reproduce code:
---------------
<?
$s = '{"":"test"}';
var_dump(json_decode($s));
?>


Expected result:
----------------
object(stdClass)#1 (1) { [""]=>  string(4) "test" }

Actual result:
--------------
object(stdClass)#2 (1) { ["_empty_"]=>  string(4) "test" }


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=46600&edit=1



Thread (24 messages)

« previous php.bugs (#131603) next »