ZF-11167: Have Zend_Json::encode check for a toArray() method as well

Issue Type: Improvement Created: 2011-03-12T20:32:42.000+0000 Last Updated: 2012-05-12T19:04:00.000+0000 Status: Resolved Fix version(s): - 1.12.0 (27/Aug/12)

  • Next Major Release ()

Reporter: Dolf Schimmel (Freeaqingme) (freak) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Json

  • zf-crteam-padraic
  • zf-crteam-priority
  • zf-crteam-review

Related issues: Attachments: - ZF-11167_adamlundrigan.patch


Currently Zend_Json checks for the presence of a toJson() method when an object is involved:

<pre class="highlight">ublic static function encode($valueToEncode, $cycleCheck = false, $options = array())
        if (is_object($valueToEncode) && method_exists($valueToEncode, 'toJson')) {
            return $valueToEncode->toJson();

My suggestion would be to alternatively also check for the presence of a toArray() object since these exist more often then tojson() methods and can be encoded just as well.


Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T03:16:13.000+0000

Attached patch for implementation + unit tests. Will commit to SVN once sufficient feedback has been received and (any) necessary modifications made.

Posted by Pádraic Brady (padraic) on 2011-08-28T13:43:06.000+0000

I've reviewed the patch and confirm it operates as intended. Recommend committing to trunk (need to check with Matthew on adding to release branch as it adds to the API). Please ensure a ZF2 git pull request including matching changes is issued for ZF2 before resolving.

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T15:31:13.000+0000

Committed to trunk r24419

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-28T20:06:08.000+0000

Opened pull request on zendframework:zf2 branch master

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.