ZF-11932: Zend_Json::encode checks for method "toJson" on objects - Zend_Json_Encoder does not.

Issue Type: Bug Created: 2011-12-09T23:04:15.000+0000 Last Updated: 2012-02-26T16:12:38.000+0000 Status: Closed Fix version(s): Reporter: Eric Thompson (etoreo) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Json

  • View_Helper

Related issues: - ZF-9521



Zend_Json::encode checks for and uses the method "toJson" on the passed object (if its an object and the method exists). This is a very handy trick. However, when used from the Json view helper, the Zend_Json_Encoder::_encodeValue() method does not make the same check.

Here is my fix (added first two lines to function):

<pre class="highlight">
    protected function _encodeValue(&$value)
        if (is_object($value) && method_exists($value, 'toJson')) {
            return $value->toJson();
        } else if (is_object($value)) {
            return $this->_encodeObject($value);
        } else if (is_array($value)) {
            return $this->_encodeArray($value);

        return $this->_encodeDatum($value);


Posted by Adam Lundrigan (adamlundrigan) on 2012-02-26T16:12:38.000+0000

This is fixed in SVN trunk and will (hopefully) be included in the next release. For more details see ZF-9521

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.