php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54513 json_decode skips value where json_encode includes it
Submitted: 2011-04-12 13:26 UTC Modified: 2011-04-14 10:15 UTC
From: samu dot voutilainen at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.6 OS: Gentoo Linux x86
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: samu dot voutilainen at gmail dot com
New email:
PHP Version: OS:

 

 [2011-04-12 13:26 UTC] 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...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-12 17:23 UTC] scottmac@php.net
-Status: Open +Status: Feedback
 [2011-04-12 17:23 UTC] 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-13 11:44 UTC] samu dot voutilainen at gmail dot com
-Status: Feedback +Status: Closed
 [2011-04-13 11:44 UTC] samu dot voutilainen at gmail dot com
Hmph, sorry, I’m not able to reproduce it either...
 [2011-04-14 10:14 UTC] cataphract@php.net
-Assigned To: +Assigned To: cataphract
 [2011-04-14 10:14 UTC] cataphract@php.net
-Status: Closed +Status: Bogus
 [2011-04-14 10:15 UTC] cataphract@php.net
-Assigned To: cataphract +Assigned To:
 [2011-04-14 10:15 UTC] cataphract@php.net
Submit to login had assigned it to me; removing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 14 12:01:27 2024 UTC