I鈥檝e tried to find out how to call the std::vector::reserve in the basic json via get_ref but I can鈥檛 figure out how.
Can you please implement a reserve and resize function for the json?
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?
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);
@abrownsword This is exactly what I was asking; std::vector has a reserve(n) function, this json library does not
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.
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.
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?
I agree that doing it via pointer or reference is the best way.
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.