Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/ZF-9521' of https://github.com/adamlundrigan/zf2
Browse files Browse the repository at this point in the history
…into hotfix/zf-9521

Conflicts:
	tests/Zend/Json/JsonTest.php
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,22 @@ protected function _encodeObject(&$value)

$props = '';

if ($value instanceof \Iterator) {
$propCollection = $value;
if (method_exists($value, 'toJson')) {
$props =',' . preg_replace("/^\{(.*)\}$/","\\1",$value->toJson());
} else {
$propCollection = get_object_vars($value);
}
if ($value instanceof \Iterator) {
$propCollection = $value;
} else {
$propCollection = get_object_vars($value);
}

foreach ($propCollection as $name => $propValue) {
if (isset($propValue)) {
$props .= ','
. $this->_encodeValue($name)
. ':'
. $this->_encodeValue($propValue);
foreach ($propCollection as $name => $propValue) {
if (isset($propValue)) {
$props .= ','
. $this->_encodeValue($name)
. ':'
. $this->_encodeValue($propValue);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,19 @@ public function testEncodeWillUseToJsonWhenBothToJsonAndToArrayMethodsAreAvailab
$this->assertNotSame($objJson, $arrJson);
}

/**
* @group ZF-9521
*/
public function testWillEncodeArrayOfObjectsEachWithToJsonMethod()
{
$array = array('one'=>new ToJsonClass());
$expected = '{"one":{"__className":"ZendTest\\\\Json\\\\ToJSONClass","firstName":"John","lastName":"Doe","email":"john@doe.com"}}';

Json\Json::$useBuiltinEncoderDecoder = true;
$json = Json\Encoder::encode($array);
$this->assertEquals($expected, $json);
}

}

/**
Expand Down

0 comments on commit 252cf0c

Please sign in to comment.