Bug #73991 [Asn->Csd]: JSON_OBJECT_AS_ARRAY flag does not function.
| From: | pollita@php.net | Date: | Fri, 17 Mar 2017 22:39:52 +0000 |
| Subject: | Bug #73991 [Asn->Csd]: JSON_OBJECT_AS_ARRAY flag does not function. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-207918@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73991&edit=1
ID: 73991
Updated by: pollita@php.net
Reported by: kruithne at gmail dot com
Summary: JSON_OBJECT_AS_ARRAY flag does not function.
-Status: Assigned
+Status: Closed
Type: Bug
Package: JSON related
Operating System: All
PHP Version: 7.1.1
Assigned To: pollita
Block user comment: N
Private report: N
New Comment:
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2017-03-17 22:26:42] pollita@php.net
Best as I can recall, the thinking was that internally, options would replace the usage of the
separate (zend_bool assoc) flag. Externally.... I'm not sure why we actually exposed the
constant :/
As a BC-safe compromise, I'll parse the bool $assoc param as nullable, json_decode($json, null,
$options) will then respect the use of JSON_OBJECT_AS_ARRAY in $options. An explicit true/false
however will still override the $options integer.
------------------------------------------------------------------------
[2017-01-25 14:39:12] kruithne at gmail dot com
While a very minor thing, I feel it would make more sense to have the associative behavior activated
based on EITHER the $assoc (second parameter) or the $options flag (JSON_OBJECT_AS_ARRAY) being
true, rather than having the $assoc take undocumented precedence over the activation.
------------------------------------------------------------------------
[2017-01-25 12:20:26] requinix@php.net
$assoc always overrides the OBJECT_AS_ARRAY flag, so in that fourth test PHP deliberately unsets it
because you passed $assoc=false.
I don't know the reasoning behind that (which came from @pollita back in 2010 and PHP 5.4.0)
but while removing the unsetting behavior makes sense to me, I'd be wary of changing anything
now.
------------------------------------------------------------------------
[2017-01-25 11:38:55] kruithne at gmail dot com
Description:
------------
The documentation states that the fourth parameter for json_decode() is a bit-mask value, it goes on
to list two possible flags: JSON_OBJECT_AS_ARRAY and JSON_BIGINT_AS_STRING.
Despite being redundant (superseded by the second parameter), the JSON_OBJECT_AS_ARRAY flag has no
effect on the decoded result when supplied.
Test script:
---------------
<?php
$json = '{"test": "From there to here, and here to there, funny things are
everywhere.", "number": 12345678901234567890}';
// Prove json_decode is working by default.
// Expected: object(stdClass) (2) {["test"] => string(67) ["number"]
=> float()}
// Result: object(stdClass) (2) {["test"] => string(67) ["number"] =>
float()}
$decoded = json_decode($json);
var_dump($decoded);
// Prove that passing bit-mask options works.
// Expected: object(stdClass) (2) {["test"] => string(67) ["number"]
=> string(20)}
// Result: object(stdClass) (2) {["test"] => string(67) ["number"] =>
string(20)}
$decoded = json_decode($json, false, 512, JSON_BIGINT_AS_STRING);
var_dump($decoded);
// Prove that the second parameter works for associative conversion.
// Expected: array(2) {["test"] => string(67) ["number"] =>
string(20)}
// Result: array(2) {["test"] => string(67) ["number"] => string(20)}
$decoded = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
var_dump($decoded);
// Attempt to use JSON_OBJECT_AS_ARRAY for associative conversion fails.
// Expected: array(2) {["test"] => string(67) ["number"] =>
string(20)}
// Result: object(stdClass) (2) {["test"] => string(67) ["number"] =>
string(20)}
$decoded = json_decode($json, false, 512, JSON_OBJECT_AS_ARRAY);
var_dump($decoded);
Expected result:
----------------
object(stdClass) (2) {["test"] => string(67) ["number"] => float()}
object(stdClass) (2) {["test"] => string(67) ["number"] => string(20)}
array(2) {["test"] => string(67) ["number"] => string(20)}
array(2) {["test"] => string(67) ["number"] => string(20)}
Actual result:
--------------
object(stdClass) (2) {["test"] => string(67) ["number"] => float()}
object(stdClass) (2) {["test"] => string(67) ["number"] => string(20)}
array(2) {["test"] => string(67) ["number"] => string(20)}
object(stdClass) (2) {["test"] => string(67) ["number"] => string(20)}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73991&edit=1