Skip to content

Make serializer dump() non-recursive#4439

Open
vii wants to merge 1 commit into
nlohmann:developfrom
vii:develop
Open

Make serializer dump() non-recursive#4439
vii wants to merge 1 commit into
nlohmann:developfrom
vii:develop

Conversation

@vii
Copy link
Copy Markdown

@vii vii commented Aug 23, 2024

This allows printing very deep trees without running out of stack.

@vii vii requested a review from nlohmann as a code owner August 23, 2024 02:32
@github-actions github-actions Bot added the L label Aug 23, 2024
@coveralls
Copy link
Copy Markdown

coveralls commented Aug 23, 2024

Coverage Status

coverage: 100.0%. remained the same
when pulling ccbafb4 on vii:develop
into 960b763 on nlohmann:develop.

@vii
Copy link
Copy Markdown
Author

vii commented Aug 23, 2024

Unfortunately has some compile errors on MSVC etc. Can update and fix if you like this direction -

error C7555: use of designated initializers requires at least '/std:c++20'

Copy link
Copy Markdown
Contributor

@gregmarr gregmarr left a comment

Choose a reason for hiding this comment

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

I'm not saying whether this is something that should be pursued or not, but in case it is, a couple initial observations.

if (pretty_print)
while (!stack.empty())
{
const dump_state& state = stack.back();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

By taking a reference here, you end up with a dangling reference when you do pop_back(). Even if you don't use state again after the pop_back(), you are referring to the BasicJsonType inside it with val. It's probably better to store a BasicJsonType * in the state, so you don't have to worry about referenceds to references, and then you can do this:

const BasicJsonType* val = stack.back().val;
const unsigned int current_indent = stack.back().new_indent;
const unsigned int offset = stack.back().offset;
auto iterator = stack.back().iterator;
stack.pop_back();

const unsigned int offset;
const decltype(start_val.m_data.m_value.object->cbegin()) iterator;
};
std::vector<dump_state> stack;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A std::stack would probably be a better choice.

@vii vii force-pushed the develop branch 3 times, most recently from 9459221 to 6fdba5b Compare August 24, 2024 03:13
@t-b
Copy link
Copy Markdown
Contributor

t-b commented Aug 24, 2024

I very much like this fix here! It was on my todo list as well.

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!

@github-actions github-actions Bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants