Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON recursion when first object in root list is empty #61

Closed
kriswallsmith opened this issue Mar 18, 2013 · 4 comments
Closed

JSON recursion when first object in root list is empty #61

kriswallsmith opened this issue Mar 18, 2013 · 4 comments

Comments

@kriswallsmith
Copy link
Contributor

To reproduce:

$serializer->serialize(array((object) array()), 'json');

The error:

json_encode(): recursion detected

@tyler-sommer
Copy link
Contributor

I think I've run into the same issue. For some reason the Visitor is replacing the reference to the empty object with a reference to the root node. Or something. I'm working on figuring out exactly what's going on but here is what I'm doing:

$context = new SerializationContext();
$context->setSerializeNull(true);

$serializer = $this->get('jms_serializer');

return new Response($serializer->serialize(array(
    'routes' => array(new \stdClass()),
), 'json', $context));

And this is a screenshot of the root node right before json_encode is called:

screen shot 2013-06-19 at 8 43 51 am

@tyler-sommer
Copy link
Contributor

After some more debugging, I've found that line 64 of JsonSerializationVisitor as setting the root object to the same ArrayObject as the child node's $rs. When $rs is returned from the child visit, the parent node sets $rs[$k] = $v where $v === $rs, causing the recursion.

Admittedly, I'm in a bit over my head here. Adding a simple clone keyword to line 64 (see tyler-sommer/serializer@ccff7fb ) seems to solve the issue, but I'm unsure what side effects I might have caused.

I would submit a PR for this, but I wonder if I'm just fixing the side effect and not addressing the real problem.

Advice?

@schmittjoh
Copy link
Owner

Could you send a PR where you add a test-case in a first commit, and your proposed fix in a second commit?

@parnas
Copy link

parnas commented Jan 9, 2014

You don't even need to do "clone" in this case. Just "$this->setRoot(new \ArrayObject());". I was going to submit that and they I have found your pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants