Edit report at https://bugs.php.net/bug.php?id=46600&edit=1
ID: 46600
Comment by: ofbeaton at gmail dot com
Reported by: Matt at mpcm dot com
Summary: "_empty_" key in objects (see #41504)
Status: No Feedback
Type: Bug
Package: JSON related
Operating System: *
PHP Version: 5CVS, 6CVS (2008-11-18)
Block user comment: N
Private report: N
New Comment:
So I just ran into this problem while making a satis repository script for composer. I don't
see a good solution.
If I $composerJson = json_decode($composerJson, true) into associative arrays,
manipulate it, then json_encode($composerJson) then my
"require-dev": {}
turns into
"require-dev": []
which composer chokes on. So obviously I can't read the composer file as associative arrays!
That isn't this bug, I just want to show upfront that using the associative array parameter on
my json_decode cannot solve my problem.
So I do: $composerJson = json_decode($composerJson, false) and turn it into objects
like this thread talks about. Except the most common composer.json contains something like this:
"autoload": {
"psr-4": {
"": "src/"
}
}
it reads it in fine, but when I do json_encode($composerJson) again... I get:
"autoload": {
"psr-4": {
"_empty_": "src/"
}
}
which composer then chokes on because of course "_empty_" has a different meaning than
"".
So I'm stuck doing a json_encode then using a hack, $composerJson =
str_replace('"_empty_":', '"":', $composerJson)
before writing it to file again.
The _empty_ key does not seem to me to be the right answer here. Should composer change the way
composer files for virtually all projects be written? Is this a composer bug?
Or should json_encode and json_decode objects reproduce a blank key?
I hope the answer is the later, but I cannot say for sure.
Previous Comments:
------------------------------------------------------------------------
[2016-01-25 14:56:31] ajf@php.net
This is arguably not a bug since you can't have objects with empty property names, so this is
necessary to allow decoding JSON objects with empty keys. However, that's a rather arbitrary
restriction. If we removed it, we could fix this.
------------------------------------------------------------------------
[2014-01-14 18:50:41] matt at mpcm dot com
Last post was a bad example, this is the actual output:
as object: {"_empty_":"c"}
as array: {"_empty_":"a","":"c"}
------------------------------------------------------------------------
[2014-01-14 18:48:23] matt at mpcm dot com
With an output of:
as object: {"_empty_":"c"}
as array: {"_empty_":"b","":"c"}
------------------------------------------------------------------------
[2014-01-14 18:47:27] matt at mpcm dot com
To illustrate the current collision state, as this came up on the JSMentors list recently.
<?php
$a =
'{"_empty_":"a","":"b","":"c"}';
echo "as object: " . json_encode(json_decode($a));
echo "\n";
echo "as array: " . json_encode(json_decode($a, true));
PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
------------------------------------------------------------------------
[2009-08-12 22:49:59] scottmac@php.net
Works for me here with 5.2-dev and 5.3-dev
object(stdClass)#1 (2) {
["test2"]=>
int(5)
["_empty_"]=>
int(6)
}
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=46600
--
Edit this bug report at https://bugs.php.net/bug.php?id=46600&edit=1