Skip to content

Single Pass JSON emitter #34633

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

Closed
wants to merge 4 commits into from
Closed

Single Pass JSON emitter #34633

wants to merge 4 commits into from

Conversation

tenderlove
Copy link
Member

This switches the "as_json" pattern to use a visitor instead of monkey patching core classes. Since we are using a visitor it means we can switch implementations when dumping JSON and still get escaped string support in one pass of the object graph.

Refs: #34578

This commit switches our `as_json` monkey patches to a visitor class
Now we get escaped strings in one pass
@tenderlove
Copy link
Member Author

I should say, the total passes on the object graph in this case should be 2 if you count the traversal that the JSON gem does. The master branch does one more pass.

if AsJSONEncoder.internal_as_json?(object)
visit object
else
encode object.as_json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first thought is we might as well save the direct recursion and move this up as:

object = object.as_json until AsJSONEncoder.internal_as_json?(object)

... but that does mean def as_json; self; end goes from being a stack overflow to an iloop, so maybe it's better as is. 🤷‍♂️

@simi simi mentioned this pull request Apr 26, 2019
@rails-bot
Copy link

rails-bot bot commented Dec 18, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 18, 2019
@rails-bot rails-bot bot closed this Dec 25, 2019
@rafaelfranca rafaelfranca deleted the json-visitor branch May 15, 2024 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants