-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
NaN-like comparison behavior of discarded is inconvenient #1988
Comments
I find this to be very unintuitive: auto json = nlohmann::json::parse(str, nullptr, false);
if (json == nlohmann::json::value_t::discarded) // ALWAYS false
if (json.is_discarded()) // actual proper test, different behavior than operator== especially when https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a265a473e939184aa42655c9ccdf34e58.html#a265a473e939184aa42655c9ccdf34e58 literally says "Returns Also, the documentation on https://nlohmann.github.io/json/classnlohmann_1_1basic__json_aabe623bc8304c2ba92d96d91f390fab4.html#aabe623bc8304c2ba92d96d91f390fab4 - "This function returns true if and only if the JSON value was discarded during parsing with a callback function" is incorrect because |
I'm afraid the behavior of |
How can I then parse with |
Yes, |
I suspect that few users rely on the behavior of operator==, so it might still be feasible to change it when other breaking changes are also made, e.g. to remove implicit conversion from json to other types. Alternatively, it might be useful to provide an additional comparison function (same?, identical?) that behaves like operator== but also has discarded == discarded. |
The documentation of |
The special
discarded
value has NaN-like comparison behavior, i.e. discarded != any value, including discarded, but I think it would be better for it to have normal comparison behavior, where discarded == discarded, discarded != any other value.I have found discarded to be convenient for representing a missing object member in code that parses an ::nlohmann::json value into a custom type, but the NaN comparison behavior makes that usage more complicated and more error prone.
It also potentially breaks std::map<::nlohmann::json, T>.
I think in practice the NaN comparison behavior is unlikely to be useful, and likely to be confusing.
The text was updated successfully, but these errors were encountered: