Issues

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

Description

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

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.

Comments

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

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.

Committed to trunk r24419

Opened pull request on {{zendframework:zf2}} branch {{master}} https://github.com/zendframework/zf2/pull/362