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

Make serializer dump() non-recursive #4439

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

vii
Copy link

@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

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
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
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
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
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
Contributor

t-b commented Aug 24, 2024

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

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

Successfully merging this pull request may close these issues.

4 participants