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

Issue Type: Bug Created: 2011-05-10T16:25:08.000+0000 Last Updated: 2011-09-01T00:25:46.000+0000 Status: Resolved Fix version(s): - 1.11.8 (07/Jul/11)

Reporter: Martin Prebio (bountin) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Json

Related issues: Attachments: - ZF-11356.patch


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.


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:

<pre class="highlight">
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?

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

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.