Skip to content

Json Encoding fails on recursion when replaceNodes is disabled #1076

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

Open
dhaeckel opened this issue Mar 11, 2025 · 2 comments
Open

Json Encoding fails on recursion when replaceNodes is disabled #1076

dhaeckel opened this issue Mar 11, 2025 · 2 comments

Comments

@dhaeckel
Copy link

Hi @nikic,

I'm unable to dump a class AST in JSON when using the NameResolver and passing replaceNodes false, because of recursive refs. You can see an example at https://github.com/dhaeckel/php-parser-issue-repro, you have to execute vendor/bin/php-parse -N -j yaf.php and change the NameResolver options in vendor/nikic/php-parser/bin/php-parse to see the problem. A possible mitigation could be to clone already resolved names before returning to break the cyclic refs. See #1075 for a possible solution. Let me know, if you need anything from me.

Thanks

@theofidry
Copy link
Contributor

I'm unable to dump a class AST in JSON when using the NameResolver and passing replaceNodes false, because of recursive refs

I may misunderstand but that sounds expected from the design?

Also cloning would likely mean a higher memory usage, which may not be desired.

@dhaeckel
Copy link
Author

Hey Theo,

serializing the AST to JSON is explicitly listed in the list of features, so I think this should be possible, regardless of the NameResolver Options. I do understand the concern about increased memory usage, that's why I propose to hide the cloning solution behind an option, so users can explicitly opt in to use this solution (see the mentioned draft). I'm open for discussion, if this should be treated as an issue and if the proposed solution is the right one.

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

2 participants