Merge branch 'PHP-5.4'

* PHP-5.4:
  - Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
This commit is contained in:
Felipe Pena 2012-05-08 21:06:15 -03:00
commit 4dc9545aaa
2 changed files with 60 additions and 0 deletions

View File

@ -513,6 +513,19 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio
{
zend_class_entry *ce = Z_OBJCE_P(val);
zval *retval = NULL, fname;
HashTable* myht;
if (Z_TYPE_P(val) == IS_ARRAY) {
myht = HASH_OF(val);
} else {
myht = Z_OBJPROP_P(val);
}
if (myht && myht->nApplyCount > 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
smart_str_appendl(buf, "null", 4);
return;
}
ZVAL_STRING(&fname, "jsonSerialize", 0);

View File

@ -0,0 +1,47 @@
--TEST--
Bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
--SKIPIF--
<?php if (!extension_loaded("json")) print "skip"; ?>
--FILE--
<?php
class JsonTest1 {
public $test;
public $me;
public function __construct() {
$this->test = '123';
$this->me = $this;
}
}
class JsonTest2 implements JsonSerializable {
public $test;
public function __construct() {
$this->test = '123';
}
public function jsonSerialize() {
return array(
'test' => $this->test,
'me' => $this
);
}
}
$obj1 = new JsonTest1();
var_dump(json_encode($obj1));
echo "\n==\n";
$obj2 = new JsonTest2();
var_dump(json_encode($obj2));
?>
--EXPECTF--
Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"
==
Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"