ZF-11356: Zend_Json_Encoder fails to encode namespaced class names properly


I'm using ZF with Doctrine which is using PHP 5.3 Namespaces. I want to encode an object using Zend_Json's embedded encoder. This encoder adds a '__classname' to every object which contains the get_class() value of the object. This results in malformed JSON because the backslashes aren't encoded properly.

Example: Encoding a class A\Entity\Foo is saved as {"__classname":"A\Entity\Foo"} without encoded backslashes. The correct encoding is {"__classname":"A\Entity\Foo"}.

My local workaround is a str_replace('\', '\\', get_class($value)) around the get_class() call.


I've attached a fix implementation and unit test to cover it.

I wasn't sure what the proper procedure was for using namespaced classes inside of ZF. What I did was create a new PHP file tests/Zend/Json/_files/Zf11356-NamespacedClass.php with this code:

namespace Zend\JsonTest\Zf11356;
class NamespacedClass extends \ArrayIterator

Then tested the PHP version in the unit test, and if it was >=5.3 require_once'd the above file and carried on with the test.

Is this a good approach?

Fixed in trunk r24151 Merged to release-1.11 in r24152

Issued pull request against {{zendframework/zf2}} branch {{master}}