Index: tests/Zend/JsonTest.php =================================================================== --- tests/Zend/JsonTest.php (revision 25028) +++ tests/Zend/JsonTest.php (working copy) @@ -754,10 +754,30 @@ public function testEncodeObjectImplementingIterator() { - $this->markTestIncomplete('Test is not yet finished.'); + $iterator = new ArrayIterator(array( + 'foo' => 'bar', + 'baz' => 5 + )); + $target = '{"__className":"ArrayIterator","foo":"bar","baz":5}'; + + Zend_Json::$useBuiltinEncoderDecoder = true; + $this->assertEquals($target, Zend_Json::encode($iterator)); } /** + * @group ZF-12347 + */ + public function testEncodeObjectImplementingIteratorAggregate() + { + $iterator = new ZF12347_IteratorAggregate(); + $target = '{"__className":"ZF12347_IteratorAggregate","foo":"bar","baz":5}'; + + Zend_Json::$useBuiltinEncoderDecoder = true; + $this->assertEquals($target, Zend_Json::encode($iterator)); + } + + + /** * @group ZF-8663 */ public function testNativeJsonEncoderWillProperlyEncodeSolidusInStringValues() @@ -1012,3 +1032,18 @@ return Zend_Json::encode($data, false, array('enableJsonExprFinder' => true)); } } + +/** + * @see ZF-12347 + */ +class ZF12347_IteratorAggregate implements IteratorAggregate +{ + protected $array = array( + 'foo' => 'bar', + 'baz' => 5 + ); + + public function getIterator() { + return new ArrayIterator($this->array); + } +} Index: library/Zend/Json/Encoder.php =================================================================== --- library/Zend/Json/Encoder.php (revision 25028) +++ library/Zend/Json/Encoder.php (working copy) @@ -74,7 +74,6 @@ public static function encode($value, $cycleCheck = false, $options = array()) { $encoder = new self(($cycleCheck) ? true : false, $options); - return $encoder->_encodeValue($value); } @@ -138,7 +137,9 @@ if (method_exists($value, 'toJson')) { $props =',' . preg_replace("/^\{(.*)\}$/","\\1",$value->toJson()); } else { - if ($value instanceof Iterator) { + if ($value instanceof IteratorAggregate) { + $propCollection = $value->getIterator(); + } elseif ($value instanceof Iterator) { $propCollection = $value; } else { $propCollection = get_object_vars($value);