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

Reserve and resize function for basic json #1935

Closed
aaangeletakis opened this issue Feb 11, 2020 · 8 comments
Closed

Reserve and resize function for basic json #1935

aaangeletakis opened this issue Feb 11, 2020 · 8 comments
Labels
kind: question state: needs more info the author of the issue needs to provide more details

Comments

@aaangeletakis
Copy link

I’ve tried to find out how to call the std::vector::reserve in the basic json via get_ref but I can’t figure out how.

Can you please implement a reserve and resize function for the json?

@nlohmann
Copy link
Owner

In order to asses your issue, we need the following information:

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Feb 14, 2020
@abrownsword
Copy link

abrownsword commented Mar 6, 2020

I just came to ask the same question... I have a json array that I'm going to insert some known (or approximate) number of elements into, and I'm observing in the profile that the vector resize operation is showing up as a significant portion of the time. Normally I would call vector::reserve() in order to avoid this re-allocation and copying (and possibly use resize() after finishing the inserts when the final size is known although this is usually much less important), however, these don't seem to be exposed on the json array.

Ah, I figured it out:

jsonarray.get_ptr<json::array_t*>()->reserve(N);

@aaangeletakis
Copy link
Author

@abrownsword This is exactly what I was asking; std::vector has a reserve(n) function, this json library does not

@dota17
Copy link
Contributor

dota17 commented Mar 12, 2020

Can you please implement a reserve and resize function for the json?

@boredbored
You mean that we should add reserve and resize functions into basic_json rather than calling reserve
in the basic json via get_ref?

We can do more things when we get the ref or pointer. If we add reserve and resize, the other functions of std::vector should be added too.

@nlohmann
Copy link
Owner

I do not think adding this functions would be too intuitive, as it could not do anything if the stored JSON value was a number/bool/null/object, etc.

@abrownsword
Copy link

It also relies on knowing that the underlying container has a reserve method. I gave the syntax above by which the goal can be accomplished. That ought to be sufficient?

@nlohmann
Copy link
Owner

I agree that doing it via pointer or reference is the best way.

@dota17
Copy link
Contributor

dota17 commented Mar 16, 2020

That ought to be sufficient?

I think it is enough.

We can do more things when we get the ref or pointer. If we add reserve and resize, the other functions of std::vector should be added too.

Just as i said, there will be endless works if we add reserve and resize.

This issue can be closed, i think. The example is figured out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

4 participants