You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When operator[] is called on a null value with a string_t argument, the value is silently converted to an object. When operator[] is called on a null value with an integer argument, an exception is thrown.
This feels inconsistent compared to push_back: When push_back is called on a null value, the value is silently converted to an object (if the argument was a string_t/json-pair) or an array (if the argument was a json value).
Proposal
I would propose changing operator[] to always convert a null value to an array. Thereby, the following code would be possible:
json j;
j[2] = 17;
std::cout << j << '\n';
And the output would be [null,null,17].
Rationale
This would then behave exactly like the following code:
This code already works as described: Calling operator[] on an array, but with an integer argument out of bounds extends the array accordingly with null values.
What do you think?
The text was updated successfully, but these errors were encountered:
I was wrong: the non-const versions of operator[] both convert null values to array/object, whereas the const versions throw when applied to a null value. That is, the behavior is OK, and apart from some copy/paste errors in the documentation, everything is fine.
When I opened this issue, I was entirely working with the documentation, got confused, but did not check the source code (which I should have done in the first place...).
Situation
When
operator[]
is called on a null value with astring_t
argument, the value is silently converted to an object. Whenoperator[]
is called on a null value with an integer argument, an exception is thrown.This feels inconsistent compared to
push_back
: Whenpush_back
is called on a null value, the value is silently converted to an object (if the argument was astring_t
/json
-pair) or an array (if the argument was ajson
value).Proposal
I would propose changing
operator[]
to always convert a null value to an array. Thereby, the following code would be possible:And the output would be
[null,null,17]
.Rationale
This would then behave exactly like the following code:
This code already works as described: Calling
operator[]
on an array, but with an integer argument out of bounds extends the array accordingly with null values.What do you think?
The text was updated successfully, but these errors were encountered: