ID:               38945
 Comment by:       grebenshikov dot n at gmail dot com
 Reported By:      RQuadling at GMail dot com
 Status:           Open
 Bug Type:         Documentation problem
 Operating System: Windows XP SP2
 PHP Version:      Irrelevant
 New Comment:

json_decode also has limits on object depth.

class C2 {
        public $text = 'Hello!';
}

class C1 {
        public $next;
        public $objs = array();
        
        public function __construct($next) {
                $this->next = $next;
                for ($i=1; $i<=10; $i++) {
                        array_push($this->objs, new C2());
                }
        }
}
$first = new C1(false);
$obj = $first;

for ($i=1; $i<=17; $i++) {
        $obj->next = new C1(false);
        $obj = $obj->next;      
}
print (json_encode($first));
print_r(json_decode(json_encode($first)));


Previous Comments:
------------------------------------------------------------------------

[2006-09-25 07:42:12] RQuadling at GMail dot com

Description:
------------
Massively nested arrays are decoded as null by json_decode.

Admittedly, an array this deep has its own concerns, but with no
warnings or notices, documentation of the limit would be advisable.

The following code has 2 arrays, the first is 18 levels, the second is
19. The first decodes correctly, the second decodes as null.

Whilst an array MAY not be this deep, it is possible that objects
containing objects with arrays COULD produce a depth of 19 levels.
Sure, not exactly LEAN, but that's why this is just a minor doc issue
and not a more important code issue.

The issue is with json_decode as json_encode quite happily encodes the
larger array - an inconsistency maybe as the json_decode dox does say
any json_encode'd output (though I am awaiting the changes due to
bug#38680).

It also makes no difference if you use the 'assoc' option
(json_decode's second paramater of True) to produce an associative
array. The limit is still reached at 19 levels.

Reproduce code:
---------------
<?php
$a_array = 
        array('One' => array('Two' => array('Three' => array('Four' => 
        array('Five' => array('Six' => array('Seven' => array('Eight' => 
        array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => 
        array('Thirteen' => array('Fourteen' => array('Fifteen' => 
        array('Sixteen' => array('Seventeen' => array('Eighteen' => 
        array(1,2,3)))))))))))))))))));
var_dump(json_encode($a_array));
var_dump(json_decode(json_encode($a_array)));

$a_array = 
        array('One' => array('Two' => array('Three' => array('Four' => 
        array('Five' => array('Six' => array('Seven' => array('Eight' => 
        array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => 
        array('Thirteen' => array('Fourteen' => array('Fifteen' => 
        array('Sixteen' => array('Seventeen' => array('Eighteen' => 
        array('Nineteen' => array(1,2,3))))))))))))))))))));
var_dump(json_encode($a_array));
var_dump(json_decode(json_encode($a_array)));
?>

Expected result:
----------------
string(195)
"{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    array(3) {
                                      [0]=>
                                      int(1)
                                      [1]=>
                                      int(2)
                                      [2]=>
                                      int(3)
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
string(208)
"{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    object(stdClass)#19 (1) {
                                      ["Nineteen"] =>
                                        array(3) {
                                        [0]=>
                                        int(1)
                                        [1]=>
                                        int(2)
                                        [2]=>
                                        int(3)
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Actual result:
--------------
string(195)
"{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    array(3) {
                                      [0]=>
                                      int(1)
                                      [1]=>
                                      int(2)
                                      [2]=>
                                      int(3)
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
string(208)
"{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}"
NULL


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


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

Reply via email to