ZF-11356: Zend_Json_Encoder fails to encode namespaced class names properly
Description
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.
Comments
Posted by Adam Lundrigan (adamlundrigan) on 2011-06-01T11:43:08.000+0000
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:
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?
Posted by Adam Lundrigan (adamlundrigan) on 2011-06-24T15:23:24.000+0000
Fixed in trunk r24151 Merged to release-1.11 in r24152
Posted by Adam Lundrigan (adamlundrigan) on 2011-09-01T00:25:45.000+0000
Issued pull request against {{zendframework/zf2}} branch {{master}} https://github.com/zendframework/zf2/pull/371