Bug #54513 [Csd->Bgs]: json_decode skips value where json_encode includes it

From: Date: Thu, 14 Apr 2011 08:14:34 +0000
Subject: Bug #54513 [Csd->Bgs]: json_decode skips value where json_encode includes it
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-160159@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54513&edit=1

 ID:                 54513
 Updated by:         cataphract@php.net
 Reported by:        samu dot voutilainen at gmail dot com
 Summary:            json_decode skips value where json_encode includes
                     it
-Status:             Closed
+Status:             Bogus
 Type:               Bug
 Package:            JSON related
 Operating System:   Gentoo Linux x86
 PHP Version:        5.3.6
 Assigned To:        cataphract
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[2011-04-13 11:44:49] samu dot voutilainen at gmail dot com

Hmph, sorry, I’m not able to reproduce it either...

------------------------------------------------------------------------
[2011-04-12 17:23:31] scottmac@php.net

I need a reproduce script.

I just used:
<?php

$test = new StdClass;

$test->a = 1;
$test->b = array();
$test->c = 'foo';

$json = json_encode($test);
var_dump($test);
var_dump(json_decode($json));

And I got the output:
object(stdClass)#1 (3) {
  ["a"]=>
  int(1)
  ["b"]=>
  array(0) {
  }
  ["c"]=>
  string(3) "foo"
}
object(stdClass)#2 (3) {
  ["a"]=>
  int(1)
  ["b"]=>
  array(0) {
  }
  ["c"]=>
  string(3) "foo"
}

Nothing missing there.

------------------------------------------------------------------------
[2011-04-12 13:26:41] samu dot voutilainen at gmail dot com

Description:
------------
If you have empty array inside object, json_encode() does encode the
value into JSON format, but when the data is decoded with json_decode(),
this empty JSON array is omitted from output.

Consider following structure:

 ["timetable"]=>
  object(pizzaonline\model\Timetable)#30 (9) {
    ["id"]=>
    string(3) "774"
    ["timetable"]=>
    array(0) {
    }
    ["opening_hours"]=>
    ..

Corresponding json_encode would be:

"timetable":{
    "id":"774",
    "timetable":[

    ],
    "opening_hours":[
  ..

This is all right, but I have a loop that removes this empty array with
unset(). So PHP structure from JSON is encoded is like:

["timetable"]=>
  object(pizzaonline\model\Timetable)#30 (8) {
    ["id"]=>
    string(3) "774"
    ["opening_hours"]=>

But using json_encode() returns same result that is in snippet 2.

Just ask if any questions, I’ll do example script later...



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



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


Thread (5 messages)

« previous php.bugs (#160159) next »