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

Inconsistency between operator[] and push_back #203

Closed
nlohmann opened this issue Feb 6, 2016 · 2 comments
Closed

Inconsistency between operator[] and push_back #203

nlohmann opened this issue Feb 6, 2016 · 2 comments
Assignees
Labels
solution: invalid the issue is not related to the library
Milestone

Comments

@nlohmann
Copy link
Owner

nlohmann commented Feb 6, 2016

Situation

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:

json j = json::array();
j[2] = 17;
std::cout << j << '\n';

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?

@nlohmann nlohmann added kind: enhancement/improvement state: please discuss please discuss the issue or vote for your favorite option labels Feb 6, 2016
@nlohmann nlohmann self-assigned this Feb 6, 2016
@nlohmann nlohmann added this to the Release 2.0.0 milestone Feb 6, 2016
@nlohmann nlohmann changed the title Inconsistency between operator[] and push_back Inconsistency between operator[] and push_back Feb 6, 2016
@gregmarr
Copy link
Contributor

gregmarr commented Feb 6, 2016

To be clear, do you mean changing this only for integer arguments?

@nlohmann
Copy link
Owner Author

nlohmann commented Feb 7, 2016

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...).

Nothing to see here, carry on :-)

@nlohmann nlohmann closed this as completed Feb 7, 2016
@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: enhancement/improvement state: please discuss please discuss the issue or vote for your favorite option labels Feb 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants