Bug #73991 [Opn]: JSON_OBJECT_AS_ARRAY flag does not function.

From: Date: Wed, 25 Jan 2017 12:20:29 +0000
Subject: Bug #73991 [Opn]: JSON_OBJECT_AS_ARRAY flag does not function.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206943@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:         requinix@php.net
 Reported by:        kruithne at gmail dot com
 Summary:            JSON_OBJECT_AS_ARRAY flag does not function.
 Status:             Open
 Type:               Bug
 Package:            JSON related
 Operating System:   All
 PHP Version:        7.1.1
-Assigned To:        
+Assigned To:        pollita
 Block user comment: N
 Private report:     N

 New Comment:

$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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (5 messages)

« previous php.bugs (#206943) next »